使用 DataGridViewComboboxColumn 进行输入时出现问题
我有一个用于数据输入的 datagridview。我之前已经对所有文本列执行过此操作,效果很好。但现在我希望其中一列成为数据绑定组合框,以便用户可以选择选项。当我这样做时,生成的 gridview 的数据源最终会出现空行(但数量正确)。我缺少什么?
下面是代码:
DataGridViewComboBoxColumn cboCategory = new DataGridViewComboBoxColumn();
{
cboCategory.HeaderText = "Category";
cboCategory.DataSource = downtimeCategories;
cboCategory.DisplayMember = "Name";
cboCategory.ValueMember = "CategoryID";
cboCategory.DataPropertyName = "CategoryID";
gridDowntime.Columns.Add(cboCategory);
}
然后是获取 gridview 数据源的代码:
DataTable dt = (gridDowntime.DataSource as DataTable);
每次我得到一个具有正确行数的表,但所有行都是空的(尽管它们是长行,但数据集可视化工具必须滚动以显示整个单元格)。我怎样才能找到错误并改正呢?
编辑:我应该在这里提供一些具体的附加信息吗?
I have a datagridview which I'm using for data entry. I've done this before with all text columns, and it worked great. But now I want one of the columns to be a databound combobox so the user can select options. When I do this, the resulting gridview's datasource ends up with empty rows (but the right quantity). What am I missing?
Here is code:
DataGridViewComboBoxColumn cboCategory = new DataGridViewComboBoxColumn();
{
cboCategory.HeaderText = "Category";
cboCategory.DataSource = downtimeCategories;
cboCategory.DisplayMember = "Name";
cboCategory.ValueMember = "CategoryID";
cboCategory.DataPropertyName = "CategoryID";
gridDowntime.Columns.Add(cboCategory);
}
Then code to grab gridview's datasource:
DataTable dt = (gridDowntime.DataSource as DataTable);
Everytime I get a table with the correct number of rows, but all the rows are empty (although they are long rows, the dataset visualizer has to scroll to show the entire cell). How can I find the error and correct?
EDIT: Is there some specific additional information I should provide here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是我刚刚编写的一个简单的示例项目。
我认为您错误的关键是
DataGridViewComboboxColumn
的DataPropertyName
属性需要引用DataGridView
的数据源,而不是柱子。那里有一些绒毛——直接从互联网上抓取了数据表代码。但这里的关键部分是组合框列的属性设置。
我的数据源是 customertype 对象的列表:
因此我需要将列上的 DisplayMember 设置为“Name”,将 ValueMember 设置为“Id”,因为这引用了列数据源。 但是 DataPropertyName 的值设置为“CustomerType”,这是我们绑定到 DataGridView 的 DataTable 中的列的名称。
因此,经过聊天中的一点讨论后,发现上述情况是正确的,但您还需要确保 id 列的类型匹配。
用于向组合框列提供数据的数据表是从 sql 查询生成的,并且具有 in 类型的 id 列。您的支持数据表的 id 列如下所示:
这默认为字符串类型的列。尝试类似的方法:
Here is a simple example project that I just cooked up.
The key thing I think you are getting wrong is that the
DataPropertyName
property of theDataGridViewComboboxColumn
needs to refer to the datasource of theDataGridView
, not the column.Bit of fluff in there - grabbed that datatable code right off the interwebs. But the key part here is the setting of properties for the combobox column.
My datasource is a list of customertype objects:
So I need to set DisplayMember on the column to "Name" and ValueMember to "Id" since this references the columns datasource. However for the DataPropertyName was set the value to "CustomerType" which is the name of a column in the DataTable that we have bound to the DataGridView.
So after a wee bit of discussion in chat it turns out that the above is true but that you also need to ensure that the types of your id columns match.
The datatable used to provide data to the combobox column was generated from a sql query and had the id column of type in. Your backing datatable had the id column made like so:
This defaults to a column of type string. Try instead something like: