从 DataTable 填充 DataGridViewComboBoxCell 不起作用
Populate DataGridViewComboBoxCell from DataTable not working
我正在尝试在 特定单元格.
中使用组合框填充数据网格视图
// 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 确实注册了行。
但这就是我对 dgvcb_IgnoreMap 的看法:
我确实认为这是问题所在,但我不明白它有什么问题。我已经从 DataSet 中类似地填充了其他组合框(不是 DataGridViews),它们工作正常。
我已经尝试在 SO 上查看这些内容(并花了几个小时在互联网上搜索)但我最后的选择是来这里,因为我被难住了!
DataGridViewComboBoxCell populated but will not display content
How to populate each DataGridViewComboBoxCell with different data?
更新:
我终于能够重新访问这个项目并希望从我离开的地方继续。我对这篇文章仍有疑问。
这是我希望它看起来像的屏幕截图(在 excel 中):
更新 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;
但它们仍然不起作用。
原来是因为数据网格视图被绑定到数据 table(我这样做是为了最初获得列 headers)。
以防将来搜索此内容的其他人想要一个可能的解决方案,这就是我所做的(仅以一个组合框为例):
// 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
// Add Columns to DGV and remove sortable
foreach (DataColumn column in dt_SourceMapping.Columns)
{
dataGridView1.Columns.Add(column.ColumnName, column.ColumnName);
dataGridView1.Columns[column.Ordinal].SortMode = DataGridViewColumnSortMode.NotSortable;
}
// Add dummy rows
dataGridView1.RowCount = 6;
// format data grid view
dataGridView1.RowHeadersWidth = 5;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AllowUserToDeleteRows = false;
dataGridView1.AllowUserToResizeRows = false;
dataGridView1.ReadOnly = false;
dataGridView1.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
// ADD combobox
//------------------------------------------------
// Row 0: IgnoreMap
// Ignore/Map
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");
//------------------------------------------------
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
// Row 0: IgnoreMap
DataGridViewComboBoxCell dgvcb_IgnoreMap = new DataGridViewComboBoxCell
{
DataSource = dt_Map_IgnoreMap,
DisplayMember = "IgnoreMap",
ValueMember = "IgnoreMap"
};
dataGridView1.Rows[0].Cells[i] = dgvcb_IgnoreMap;
}
最终结果:
我正在尝试在 特定单元格.
中使用组合框填充数据网格视图// 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 确实注册了行。
但这就是我对 dgvcb_IgnoreMap 的看法:
我确实认为这是问题所在,但我不明白它有什么问题。我已经从 DataSet 中类似地填充了其他组合框(不是 DataGridViews),它们工作正常。
我已经尝试在 SO 上查看这些内容(并花了几个小时在互联网上搜索)但我最后的选择是来这里,因为我被难住了!
DataGridViewComboBoxCell populated but will not display content
How to populate each DataGridViewComboBoxCell with different data?
更新:
我终于能够重新访问这个项目并希望从我离开的地方继续。我对这篇文章仍有疑问。
这是我希望它看起来像的屏幕截图(在 excel 中):
更新 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;
但它们仍然不起作用。
原来是因为数据网格视图被绑定到数据 table(我这样做是为了最初获得列 headers)。
以防将来搜索此内容的其他人想要一个可能的解决方案,这就是我所做的(仅以一个组合框为例):
// 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
// Add Columns to DGV and remove sortable
foreach (DataColumn column in dt_SourceMapping.Columns)
{
dataGridView1.Columns.Add(column.ColumnName, column.ColumnName);
dataGridView1.Columns[column.Ordinal].SortMode = DataGridViewColumnSortMode.NotSortable;
}
// Add dummy rows
dataGridView1.RowCount = 6;
// format data grid view
dataGridView1.RowHeadersWidth = 5;
dataGridView1.AllowUserToAddRows = false;
dataGridView1.AllowUserToDeleteRows = false;
dataGridView1.AllowUserToResizeRows = false;
dataGridView1.ReadOnly = false;
dataGridView1.EditMode = DataGridViewEditMode.EditOnKeystrokeOrF2;
// ADD combobox
//------------------------------------------------
// Row 0: IgnoreMap
// Ignore/Map
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");
//------------------------------------------------
for (int i = 0; i < dataGridView1.Columns.Count; i++)
{
// Row 0: IgnoreMap
DataGridViewComboBoxCell dgvcb_IgnoreMap = new DataGridViewComboBoxCell
{
DataSource = dt_Map_IgnoreMap,
DisplayMember = "IgnoreMap",
ValueMember = "IgnoreMap"
};
dataGridView1.Rows[0].Cells[i] = dgvcb_IgnoreMap;
}
最终结果: