DataGridView ComboBox 编辑控件显示事件

3

我有一个基本的WinForms应用程序,其中包含一个名为dgvHardware的DataGridView控件。这个控件绑定了以下代码:

    private void dgvHardware_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        if (e.Control is ComboBox)
        {
            ((ComboBox)e.Control).SelectionChangeCommitted -= new EventHandler(cboDgvHardware_SelectionChanged);
            ((ComboBox)e.Control).SelectionChangeCommitted += new EventHandler(cboDgvHardware_SelectionChanged);
        }
    }

    private void cboDgvHardware_SelectionChanged(object sender, EventArgs e)
    {
        // string result is the selected text of the combo box
        // int row is the row index that the selected combo box lives
        string result = ((ComboBox)sender).SelectedItem.ToString();
        // int row = ????
    }

长话短说,我需要确定事件正在发生的行,以便根据ComboBox选择更改行中的其他单元格。第二个函数中的结果返回正确的值,这也是值得注意的。
如果不清楚或需要任何其他信息,请告诉我。
谢谢, 安德鲁
2个回答

6

太棒了!这是99%正确的,DataGridView的名称为dgvHardware,因此完全正确的代码行是:int row = dgvHardware.CurrentCell.RowIndex; - Fuginator

1
你可能会对 dgvHardware.CurrentRow 感兴趣,它可以让你访问整行数据,并且你可以使用 dgvHardware.CurrentRow.Cells[index 或 columnName] 导航到不同的列并更改其他单元格的值。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接