来自list / dictionary / datatable的c#datagridview combobox列数据

编程入门 行业动态 更新时间:2024-10-16 22:23:42
本文介绍了来自list / dictionary / datatable的c#datagridview combobox列数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我确定这个简单,但我花了大约1/2天的谷歌搜索和尝试各种各样的东西。

我有一个datatable和一列是另一个数据库表的整数ID外键。

我有一个datagridview,我想使用一个组合框列来允许用户改变价值。但是,不用使用整数,使用这些名称是非常好的。

我尝试用公共成员int ID和string Name创建一个简单的结构体;一个字典,并查找枚举(但是编译时不知道值),但没有任何工作。

我可以使用结构值填充组合框,但是无法以编程方式设置所选项/索引;即,如果ID5在数据表中,则将组合框选中的项目设置为具有ID为5的结构体。

所以要清楚我是想要:

gridview datasource的fk ID的 1 2 3 外键表: ID名称 1名称1 2名称2 3名称3 / pre>

Datagridviewcombobox列应该加载三个项目;应显示为名称1,名称2,名称3。基于gridview数据源的FK ID,每个选择的项目都应该匹配。

帮助?

Andrew

解决方案

您可以设置 DataGridViewComboBoxColumn.DataSource 属性,然后使用 ValueMember 和 DisplayMember 属性,以确定在 ComboBox 中显示的内容。最简单的可能是将您的FK值加载到 DataTable 中,并将其用作数据源。例如:

//假设你的DataGridViewComboBox列是fkCol //,你的fk值在一个DataTable称为fkTable fkCol.DataSource = fkTable; fkCol.ValueMember =ID; fkCol.DisplayMember =Name;

我不知道如何绑定您的 DataGridView 到您的初始 DataTable ,但您可以将 DataGridViewComboBox 列与原始 DataTable 使用 DataPropertyName :

fkCol.DataPropertyName =ColumnName ;

I'm sure this simple but i've spent a about 1/2 a day googling and trying various things out.

I've got a datatable and one column is an integer ID foreign key to another database table.

I've got a datagridview and i'd like to use a combobox column to allow user to change value. But instead of using the integers, it would be great to use the names.

I've tried creating a simple struct with public members int ID and string Name; a dictionary and looked into enums (however values not known at compile time) but not got anything to work yet.

I was able to populate the combobox with struct values, but not able to programmatically set the selected item/index; ie, if ID "5" is in the datatable, set the combo box selected item to the struct that has an ID of 5.

So to be clear i'm wanting:

gridview datasource's fk ID's 1 2 3 Foreign Key table: ID Name 1 Name 1 2 Name 2 3 Name 3

Datagridviewcombobox column should be loaded with three items; should display as "Name 1, Name 2, Name 3". Based on the gridview datasource's FK id, the selected item for each should match.

Help ?

Andrew

解决方案

You can set the DataGridViewComboBoxColumn.DataSource property, and then use the ValueMember and DisplayMember properties to determine what is shown in the ComboBox. Easiest is probably to load your FK values into DataTable and use that as the data source. For your example:

// assuming your DataGridViewComboBox column is fkCol // and your fk values are in a DataTable called fkTable fkCol.DataSource = fkTable; fkCol.ValueMember = "ID"; fkCol.DisplayMember = "Name";

I am not sure how you are binding your DataGridView to your initial DataTable, but you can associate the DataGridViewComboBox column with a specific column in your original DataTable using DataPropertyName:

fkCol.DataPropertyName = "ColumnName";

更多推荐

来自list / dictionary / datatable的c#datagridview combobox列数据

本文发布于:2023-11-10 16:22:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1575877.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据   dictionary   list   datatable   combobox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!