【发布时间】:2013-09-24 14:50:36
【问题描述】:
我有一个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 的行索引?
【问题讨论】:
-
var index = comboBox.SelectedIndex;
-
您能否更清楚地说明
row index或item index?当您说row index时,它确实意味着包含当前组合框的DataGridViewRow的index -
对,所以我不是指ComboBox中选中项的索引,我需要
DataGridView中ComboBox的行索引。 -
@Yellow 现在很清楚,请参阅我的解决方案答案
标签: c# winforms datagridview combobox