【问题标题】:Add value in datagrid cell after value of combobox is selected c#选择组合框的值后在datagrid单元格中添加值c#
【发布时间】:2013-08-12 11:49:12
【问题描述】:

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

【问题讨论】:

  • 如果您想在选择更改时立即采取行动,请捕获相应组合框的SelectionChanged 事件。或者您想在用户选择一个值并按下回车键句柄 CellValueChanged 事件时做出反应
  • 我想在组合框中的选择发生变化时立即采取行动,据说还有一个疑问,我如何理解组合框的值在哪一行发生了变化?

标签: c# winforms


【解决方案1】:

你可以试试这样的:

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

【讨论】:

  • 我从代码中了解到它的作用是,当用户选择表格中的组合框时,代码将被填充。我尝试了这段代码,我在函数private void dataGridView1_EditingControlShowing 中应用了一个断点,但执行从未从函数传递,你能告诉我可能出了什么问题吗??
  • @vin 你注册了事件处理程序dataGridView1_EditingControlShowing吗?如果你不知道怎么做,你必须先添加这一行(在你的表单构造函数中)dataGridView1.EditingControlShowing += dataGridView1_EditingControlShowing;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-04
  • 1970-01-01
  • 2011-05-27
  • 2012-10-02
相关资源
最近更新 更多