C# 选择combobox的值后在datagrid单元格中添加值c

C# 选择combobox的值后在datagrid单元格中添加值c,c#,winforms,C#,Winforms,我正在尝试创建一个输出简单文本文件的应用程序。这是我在c语言中的第一个项目。我已经创建了一个数据网格表。每行基本上有5个单元格,在用户输入后动态添加行。用户只能更改5个单元格中的2个单元格中的值。 我的问题就在这里,, 在第三个单元格中,用户必须从单元格3的组合框中选择一个值,选择组合框中的值后,将填充单元格4和5中的值。然而,我无法做到这一点。 附件是供参考的图像。您可以尝试以下方法: //EditingControlShowing event handler for your dataGri

我正在尝试创建一个输出简单文本文件的应用程序。这是我在c语言中的第一个项目。我已经创建了一个数据网格表。每行基本上有5个单元格,在用户输入后动态添加行。用户只能更改5个单元格中的2个单元格中的值。 我的问题就在这里,, 在第三个单元格中,用户必须从单元格3的组合框中选择一个值,选择组合框中的值后,将填充单元格4和5中的值。然而,我无法做到这一点。
附件是供参考的图像。

您可以尝试以下方法:

//EditingControlShowing event handler for your dataGridView1
private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e){
   ComboBox combo = e.Control as ComboBox;
   if(combo != null) combo.SelectedIndexChanged += GridComboSelectedIndexChanged;
}
private void GridComboSelectedIndexChanged(object sender, EventArgs e) {
   ComboBox combo = sender as ComboBox;
   //Your populating code here
   //you can access the selected index via combo.SelectedIndex
   //you can access the current row by dataGridView1.CurrentCell.OwningRow
   //then you can access to the cell 4 and cell 5 by dataGridView.CurrentCell.OwningRow.Cells[4] and dataGridView.CurrentCell.OwningRow.Cells[5]
}

如果要在选择更改时立即执行操作,请捕获相应组合框的SelectionChanged事件。或者,当用户选择一个值并按enter键handle CellValueChanged Event时,您想做出反应。当组合框中的选择发生变化时,我想立即采取行动,因为我还有一个疑问,我如何理解组合框的值在哪一行发生了变化?正如我从代码中理解的那样,当用户选择表中的组合框时,将填充代码。我尝试使用此代码,我在函数private void dataGridView1_EditingControlShowing中应用了一个断点,但该函数从未执行过,您能告诉我可能出了什么问题吗???@vin您注册了事件处理程序dataGridView1_EditingControlShowing了吗?如果您不知道如何操作,则必须首先在窗体构造函数dataGridView1.EditingControlShowing+=dataGridView1\U EditingControlShowing中添加此行;