You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C#:跨Form获取DataGridView当前行索引时出现索引越界问题

解决Form2获取Form1 DataGridView选中行索引的问题

错误原因

你在Form2中创建了全新的Form1实例(Form1 firstform = new Form1();),这个新实例的DataGridView没有任何选中行,所以访问SelectedRows[0]会直接抛出索引越界异常。你需要引用的是已经打开的主窗体实例,而不是新建一个。

可行解决方案

需求完全可以实现,以下是两种常用的实现方式:

方法1:通过构造函数传递选中索引

  1. 修改Form2的构造函数,添加索引参数:
public partial class Form2 : Form
{
    private int _selectedRowIndex;

    public Form2(int selectedIndex)
    {
        InitializeComponent();
        _selectedRowIndex = selectedIndex;
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        // 使用传递过来的索引绑定数据
        Textbox1.DataBindings.Add(new Binding("Text", SomeClass.SomeBindingList[_selectedRowIndex], "Field2"));
        Textbox2.DataBindings.Add(new Binding("Text", SomeClass.SomeBindingList[_selectedRowIndex], "Field2"));
    }
}
  1. 在Form1的按钮点击事件中,获取选中索引并传入Form2:
private void btnOpenForm2_Click(object sender, EventArgs e)
{
    // 先判断是否有选中行,避免空引用
    if (dataGridView1.SelectedRows.Count == 0)
    {
        MessageBox.Show("请先选择一行数据");
        return;
    }

    int selectedIndex = dataGridView1.SelectedRows[0].Index;
    Form2 form2 = new Form2(selectedIndex);
    form2.Show();
}

方法2:通过公共属性传递索引

  1. 在Form2中定义公共属性:
public partial class Form2 : Form
{
    public int SelectedRowIndex { get; set; }

    private void Form2_Load(object sender, EventArgs e)
    {
        Textbox1.DataBindings.Add(new Binding("Text", SomeClass.SomeBindingList[SelectedRowIndex], "Field2"));
        Textbox2.DataBindings.Add(new Binding("Text", SomeClass.SomeBindingList[SelectedRowIndex], "Field2"));
    }
}
  1. 在Form1中打开Form2时设置属性:
private void btnOpenForm2_Click(object sender, EventArgs e)
{
    if (dataGridView1.SelectedRows.Count == 0)
    {
        MessageBox.Show("请先选择一行数据");
        return;
    }

    int selectedIndex = dataGridView1.SelectedRows[0].Index;
    Form2 form2 = new Form2();
    form2.SelectedRowIndex = selectedIndex;
    form2.Show();
}

更优方案:直接传递选中实体(推荐)

如果你的BindingList存储的是实体对象,直接传递选中的实体比传递索引更可靠(避免BindingList顺序变化导致索引失效):

  1. Form2构造函数接收实体:
public partial class Form2 : Form
{
    private YourEntityType _selectedEntity;

    public Form2(YourEntityType selectedEntity)
    {
        InitializeComponent();
        _selectedEntity = selectedEntity;
    }

    private void Form2_Load(object sender, EventArgs e)
    {
        Textbox1.DataBindings.Add(new Binding("Text", _selectedEntity, "Field2"));
        Textbox2.DataBindings.Add(new Binding("Text", _selectedEntity, "Field2"));
    }
}
  1. Form1中获取选中实体并传入:
private void btnOpenForm2_Click(object sender, EventArgs e)
{
    if (dataGridView1.SelectedRows.Count == 0)
    {
        MessageBox.Show("请先选择一行数据");
        return;
    }

    YourEntityType selectedEntity = (YourEntityType)dataGridView1.SelectedRows[0].DataBoundItem;
    Form2 form2 = new Form2(selectedEntity);
    form2.Show();
}

(注意替换YourEntityType为你实际的实体类名称)


内容的提问来源于stack exchange,提问作者Saturn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 08:05:15