【问题标题】:Populate DataGridViewComboBoxCell from DataTable not working从 DataTable 填充 DataGridViewComboBoxCell 不起作用
【发布时间】: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 确实注册了行。

但这就是我看到的 dgvcb_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

更新:

我终于能够重新访问这个项目,并希望能从我离开的地方继续。我仍然对这件作品有疑问。

这是我希望它看起来如何的屏幕截图(在 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;

但它们仍然不起作用。

【问题讨论】:

  • 我已经更新了帖子。我认为问题与使用绑定数据源有关。即,我似乎无法更新使用绑定数据源的 datagridview 单元格。希望有人可以帮助解决这个问题?

标签: c# .net winforms datagridview combobox


【解决方案1】:

原来是因为数据网格视图被绑定到数据表上(我最初这样做是为了获取列标题)。

以防万一其他人在未来搜索这个可能需要一个可能的解决方案,这就是我所做的(仅显示一个组合框作为示例):

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

最终结果:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-21
    • 1970-01-01
    • 2012-12-09
    • 1970-01-01
    • 1970-01-01
    • 2022-01-22
    • 2018-01-19
    • 2010-10-22
    相关资源
    最近更新 更多