如何为要更改的ComboBox列单元格添加事件

时间:2012-12-07 00:08:11

标签: c# winforms datagridview event-handling datagridviewcomboboxcell

我有一个DataGridView,里面有几个ComboBoxColumns。有没有办法创建一个事件,以便每次输入ComboBoxColumn单元格并选择一个项目时,事件都会触发? 到目前为止,我所能想到的只有:

private void dataGridView1_CellEnter(object sender, DataGridViewCellEventArgs e)
{
    if (e.ColumnIndex == 0)
    {
        MessageBox.Show("Amanda");
    }
}

哪个没做任何事情。

2 个答案:

答案 0 :(得分:0)

您可能正在寻找EditingControlShowing事件。

在此处查看类似问题: "SelectedIndexChanged" event in ComboBoxColumn on Datagridview

答案 1 :(得分:0)

尝试这个。

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
              }
        }
    }