我有一个DataGridView
,里面有几个ComboBoxColumns
。有没有办法创建一个事件,以便每次输入ComboBoxColumn
单元格并选择项目时,都会触发事件?到目前为止,我能弄清楚的就是:
private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
MessageBox.Show("Amanda");
}
}
这什么都没做。
您可能正在寻找EditingControlShowing
事件。
有关类似的问题,请参阅此处:数据网格视图上的组合框列中的"选定索引已更改"事件
试试这个。
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
ComboBox nameComboBox = e.Control as ComboBox;
if (dataGridView1.CurrentCell.ColumnIndex == 0)
{
if (nameComboBox != null)
{
((ComboBox)e.Control).DropDownStyle = ComboBoxStyle.DropDown;
((ComboBox)e.Control).AutoCompleteSource = AutoCompleteSource.ListItems;
((ComboBox)e.Control).AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
nameComboBox .SelectedIndexChanged -= (nameComboBox _SelectedIndexChanged);
nameComboBox .SelectedIndexChanged += (nameComboBox _SelectedIndexChanged);
}
}
}
private void nameComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (dataGridView1.CurrentCell.ColumnIndex == 1)
{
var rowindex = dataGridView1.CurrentCell.RowIndex;
if (dataGridView1[1, rowindex].EditedFormattedValue != null)
{
Consol.WriteLine(dataGridView1[1, rowindex].EditedFormattedValue.ToString());
}
else
{
//No value in cell
}
}
}