在 C# 的 DataGridView 中,可以将某一列的单元格设置为 ComboBox。当用户选择 ComboBox 中的某一项时,可以触发 SelectedIndexChanged 事件来处理这个事件。
具体的实现步骤如下:
DataGridViewComboBoxColumn column = new DataGridViewComboBoxColumn();
column.DataPropertyName = "ColumnName";
column.HeaderText = "Column Header Text";
column.Name = "Column Name";
column.DataSource = yourDataSource;
column.DisplayMember = "DisplayMember";
column.ValueMember = "ValueMember";
dataGridView1.Columns.Add(column);
处理 ComboBox 的 SelectedIndexChanged 事件:
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
DataGridViewComboBoxEditingControl comboBox = e.Control as DataGridViewComboBoxEditingControl;
if (comboBox != null)
comboBox.SelectedIndexChanged -= new EventHandler(ComboBox_SelectedIndexChanged);
comboBox.SelectedIndexChanged += new EventHandler(ComboBox_SelectedIndexChanged);
private void ComboBox_SelectedIndexChanged(object sender, EventArgs e)
DataGridViewComboBoxEditingControl comboBox = sender as DataGridViewComboBoxEditingControl;
if (comboBox != null)
int selectedIndex = comboBox.SelectedIndex;
// 处理选中项改变的逻辑
以上代码中,我们在 DataGridView 中处理了 EditingControlShowing 事件,获取了 ComboBox 编辑控件,并为它添加了 SelectedIndexChanged 事件的处理函数 ComboBox_SelectedIndexChanged。在 ComboBox_SelectedIndexChanged 中,我们可以获取到 ComboBox 当前的选中项,并在这里处理选中项改变的逻辑。
希望这些代码能对你有所帮助。