0

我有一个System.Windows.Forms.DataGridView其中一列只有组合框。当 ComboBox 发生变化时,我需要知道新项目是什么,以及发生事件的 ComboBox 的行索引。后者给我带来了麻烦。我有以下内容:

class MyForm : Form
{
    private System.Windows.Forms.DataGridView m_GridView;
    private System.Windows.Forms.DataGridViewComboBoxColumn m_ComboBoxColumn;

    public MyForm () 
    {
      /* ... lots of initialisation stuf...*/
      this.m_GridView.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
                                       this.m_ComboBoxColumn});
    }

    private void m_GridView_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
    {
        ComboBox comboBox = e.Control as ComboBox;
        if (comboBox != null)
        {
            comboBox.SelectedIndexChanged -= new EventHandler(m_ComboBoxColumn_SelectedIndexChanged);
            comboBox.SelectedIndexChanged += new EventHandler(m_ComboBoxColumn_SelectedIndexChanged);
        }
    }

    private void m_ComboBoxColumn_SelectedIndexChanged(object sender, EventArgs e)
    {
        ComboBox comboBox = (ComboBox) sender;
        string item = comboBox.Text;
        if (item != null)
        {
            System.Diagnostics.Debug.WriteLine(item); // This is the new item text
            // Now I need the row index of this ComboBox in 'm_GridView' (or 'm_ComboBoxColumn')
        }
    }
}

如何DataGridViewComboBoxColumn在最后一种方法中找到 ComboBox 的行索引?

4

2 回答 2

2

您应该将其转换为DataGridViewComboBoxEditingControl并访问EditingControlRowIndex以获取行索引,如下所示:

var comboBox = (DataGridViewComboBoxEditingControl)sender;
int rowIndex = comboBox.EditingControlRowIndex;
于 2013-09-24T15:08:30.683 回答
0

您可以通过 ComboBox 的 SelectedIndex 属性获取它 - http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindex.aspx

var index = comboBox.SelectedIndex;
于 2013-09-24T14:53:56.267 回答