我有一个System.Windows.Forms.DataGridView
,其中一列只有ComboBoxes。当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的行索引?
答案 0 :(得分:1)
您应该将其转换为DataGridViewComboBoxEditingControl
并访问EditingControlRowIndex
以获取如下的行索引:
var comboBox = (DataGridViewComboBoxEditingControl)sender;
int rowIndex = comboBox.EditingControlRowIndex;
答案 1 :(得分:0)
您可以通过ComboBox的SelectedIndex属性 - http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindex.aspx
获取它var index = comboBox.SelectedIndex;