0

我有一个DataGridView, 里面有几个ComboBoxColumns。有没有办法创建一个事件,这样每次ComboBoxColumn输入一个单元格并选择一个项目时,事件就会触发?到目前为止,我能弄清楚的是:

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

这没有做任何事情。

4

2 回答 2

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
              }
        }
    }
于 2012-12-07T04:21:20.720 回答
0

您可能正在寻找该EditingControlShowing事件。

有关类似问题,请参见此处: Datagridview 上 ComboBoxColumn 中的“SelectedIndexChanged”事件

于 2012-12-07T00:42:35.557 回答