【问题标题】:DataGridView ComboBox EditingControlShowing eventsDataGridView 组合框编辑控件显示事件
【发布时间】:2011-10-05 20:58:46
【问题描述】:

我有一个基本的 WinForms 应用程序,上面有 d DataGridView(名为 dgvHardware)控件,绑定到此控件的是以下代码:

    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 选择更改行中的其他单元格。第二个函数中的 result 会返回正确的值。

如果不清楚或者您需要任何其他信息,请告诉我。

谢谢,
安德鲁

【问题讨论】:

    标签: c# winforms datagridview datagridviewcombobox


    【解决方案1】:

    像这样使用DataGridView.CurrentCell Property

    int row = dgvHardware.CurrentCell.RowIndex;
    

    【讨论】:

    • 宾果游戏!这是 99% 正确的,DataGridView 被命名为 dgvHardware,所以完全正确的行是:int row = dgvHardware.CurrentCell.RowIndex;
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-10-15
      • 2013-12-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多