【发布时间】:2020-11-15 18:41:10
【问题描述】:
我正在尝试用特定单元格中的组合框填充数据网格视图。
// Create data table based on source data
DataTable dt_SourceMapping = new DataTable("SourceMapping"); // This is the main datatable to bind to the data grid view
// Set Columns (clone)
dt_SourceMapping = dt.Clone(); // I already have 'dt' defined elsewhere as a datatable and it is already loaded with data.
// I use this method to get the same columns for dt_SourceMapping
// Clear rows
dt_SourceMapping.Clear(); // I clear the existing rows because I want to add the below rows
dt_SourceMapping.Rows.Add(); // Ignore/Map
dt_SourceMapping.Rows.Add(); // Target col
dt_SourceMapping.Rows.Add(); // Source data type
dt_SourceMapping.Rows.Add(); // Target data type
dt_SourceMapping.Rows.Add(); // Data formatting
// Ignore/Map
// Here I am creating a datatable to store the values for Ignore/Map (ie, 2 options)
DataTable dt_Map_IgnoreMap = new DataTable("MapCBOptions");
dt_Map_IgnoreMap.Columns.Add("IgnoreMap");
dt_Map_IgnoreMap.Rows.Add("Ignore");
dt_Map_IgnoreMap.Rows.Add("Map");
// Here I am defining a new DGVComboBoxCell and binding it to the datatable
DataGridViewComboBoxCell dgvcb_IgnoreMap = new DataGridViewComboBoxCell();
dgvcb_IgnoreMap.DataSource = dt_Map_IgnoreMap;
dgvcb_IgnoreMap.DisplayMember = "IgnoreMap";
dgvcb_IgnoreMap.ValueMember = "IgnoreMap";
// Set datagridview mapping source
dataGridView1.DataSource = dt_SourceMapping;
// Assign the combox to Column 1, row 0 (just for testing)
dataGridView1[1,0] = dgvcb_IgnoreMap;
但数据网格视图中的结果显示为空白(组合框中没有列出任何项目):
dt_Map_IgnoreMap 确实注册了行。
我确实认为这是问题所在,但我不明白它有什么问题。我从 DataSet 中类似地填充了其他组合框(不是 DataGridViews),它们工作正常。
我已经尝试在 SO 上查看这些内容(以及花了几个小时在互联网上搜索),但我的最后一招是来到这里,因为我被难住了!
DataGridViewComboBoxCell populated but will not display content
How to populate each DataGridViewComboBoxCell with different data?
DataGridViewComboBoxCell valueValue is invalid with binded DataGridViewComboBoxCell
更新:
我终于能够重新访问这个项目,并希望能从我离开的地方继续。我仍然对这件作品有疑问。
更新 2:
我认为这与绑定的数据源有关。
作为测试,我刚刚创建了 5 个空白行和 3 个列:
// ADD columns and rows
dataGridView1.ColumnCount = 3;
dataGridView1.RowCount = 5;
我使用相同的代码来分配 datagridview 组合框单元,它起作用了:
所以我想我现在的问题是,如何编辑使用绑定源的 DGV?
我试过这些:
// format data grid view
dataGridView1.AllowUserToAddRows = true;
dataGridView1.AllowUserToDeleteRows = true;
dataGridView1.AllowUserToResizeRows = true;
dataGridView1.ReadOnly = false;
dataGridView1.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
但它们仍然不起作用。
【问题讨论】:
-
我已经更新了帖子。我认为问题与使用绑定数据源有关。即,我似乎无法更新使用绑定数据源的 datagridview 单元格。希望有人可以帮助解决这个问题?
标签: c# .net winforms datagridview combobox