C#DataGridView中DataGridViewComboBoxCell下拉列表选择事件
C# DataGridView 中DataGridViewComboBoxCell 下拉列表选择事件
问题:由于DataGridView控件没有SelectedIndexChanged选择事件,DataGridView需要加上EditingControlShowing事件进⾏转换,将需要数据⽤下拉框DataGridViewComboBoxCell显⽰,根据相应CurrentCell触发ComboBox_SelectedIndexChanged事件。
代码如下,根据当前需要进⾏代码的更换,有详细注释。
/// <summary>
/// ⾸先给这个DataGridView加上EditingControlShowing事件
/// </summary>
/// <param name="nder"></param>
/// <param name="e"></param>
public void DataGridView_attribute_EditingControlShowing(object nder, DataGridViewEditingControl
ShowingEventArgs e)
{
//判断相应的列
if (dataGridView_attribute.CurrentCell== L0_Cell && dataGridView_attribute.CurrentCell.RowIndex != -1)
{
//给这个DataGridViewComboBoxCell加上下拉事件
(e.Control as ComboBox).SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);
}
新年祝福语四字if (dataGridView_attribute.CurrentCell == L0_Cell_1 && dataGridView_attribute.CurrentCell.RowIndex != -1)
{
//给这个DataGridViewComboBoxCell加上下拉事件
(e.Control as ComboBox).SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);
}
if (dataGridView_attribute.CurrentCell == L0_Cell_2 && dataGridView_attribute.CurrentCell.RowIndex != -1)
父亲婚礼致辞{
//给这个DataGridViewComboBoxCell加上下拉事件
(e.Control as ComboBox).SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);
}
红角鸮
}
/// 组合框事件处理
/// </summary>
/// <param name="nder"></param>
/// <param name="e"></param>
public void ComboBox_SelectedIndexChanged(object nder, EventArgs e)
{
ComboBox combox = nder as ComboBox;
//这⾥⽐较重要
combox.Leave += new EventHandler(combox_Leave);
try
{
string model_nums = sc_tabledata.number_of_holes.Trim();
if (model_nums.Equals("单孔"))
{
if (Convert.ToString(combox.SelectedItem) != sc_tabledata_1.Span_1)
{
sc_tabledata_1.Span_1 = Convert.ToString(combox.SelectedItem);
韭菜鸡蛋干}明日恩典
}
el if (model_nums.Equals("双孔"))
{
if (dataGridView_attribute.CurrentCell == L0_Cell && dataGridView_attribute.CurrentCell.RowIndex != -1)
{
L0_Cell.Value = combox.SelectedItem;
if (Convert.ToString(L0_Cell.Value) != sc_tabledata_1.Span_1)雀屏之选
{
sc_tabledata_1.Span_1 = Convert.ToString(L0_Cell.Value);
}
清明节哪一天}
}
if (dataGridView_attribute.CurrentCell == L0_Cell_1 && dataGridView_attribute.CurrentCell.RowIndex != -1) {
L0_Cell_1.Value = combox.SelectedItem;
if (Convert.ToString(L0_Cell_1.Value) != sc_tabledata_1.Span_2)
{
sc_tabledata_1.Span_2 = Convert.ToString(L0_Cell_1.Value);
}
}
}
el if (model_nums.Equals("三孔"))
{
if (dataGridView_attribute.CurrentCell == L0_Cell && dataGridView_attribute.CurrentCell.RowIndex != -1) {
L0_Cell.Value = combox.SelectedItem;
if (Convert.ToString(L0_Cell.Value) != sc_tabledata_1.Span_1)
{
sc_tabledata_1.Span_1 = Convert.ToString(L0_Cell.Value);
}
}
if (dataGridView_attribute.CurrentCell == L0_Cell_1 && dataGridView_attribute.CurrentCell.RowIndex != -1) {
L0_Cell_1.Value = combox.SelectedItem;
if (Convert.ToString(L0_Cell_1.Value) != sc_tabledata_1.Span_2)
{
失去希望sc_tabledata_1.Span_2 = Convert.ToString(L0_Cell_1.Value);
}
}
if (dataGridView_attribute.CurrentCell == L0_Cell_2 && dataGridView_attribute.CurrentCell.RowIndex != -1) {
L0_Cell_2.Value = combox.SelectedItem;
if (Convert.ToString(L0_Cell_2.Value) != sc_tabledata_1.Span_3)
{
sc_tabledata_1.Span_3 = Convert.ToString(L0_Cell_2.Value);
}
}
}
//删全部⾏
while (dataGridView_attribute.Rows.Count > 0)
{
dataGridView_attribute.Rows.RemoveAt(0);
}
Sc_addRows();//重新建表⾏
Sc_refreshTableCell();//刷新表格单元
Sc_fill_dataview_data();//填数据
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//(这⼀步⽐较重要,如果不加,会导致lectedchanged事件⼀直触发)
/// <summary>
/// 离开combox时,把事件删除
/// </summary>
/// <param name="nder"></param>
/// <param name="e"></param>
public void combox_Leave(object nder, EventArgs e)
{
ComboBox combox = nder as ComboBox;
//做完处理,须撤销动态事件
combox.SelectedIndexChanged -= new EventHandler(ComboBox_SelectedIndexChanged);
}