将数据加载到DataGridViewComboBoxColumn VB.NET [英] Load data into DataGridViewComboBoxColumn VB.NET
问题描述
我整天都在撞墙,试图找出答案。
Hi I have been banging my head against the wall all day trying to figure this out.
我有一个显示SQL查询结果的DataGridView
I have a DataGridView that is displaying the results of an SQL Query
查询返回3个字段:GROUPNUM ,GROUPNAME,COACHING
The query returns 3 fields: GROUPNUM, GROUPNAME, COACHING
组字段中仅包含字符串,并且显示效果很好,但是COACHING字段是单个字符字段,将具有Y或N在里面。对于该列,我想有一个以Y或N为项目的组合框。这就是我到目前为止所拥有的。
The group fields just have strings in them and they are displaying fine, but the COACHING field is a single character field that will either have a Y or an N in it. For that column I want to have a combobox with Y or N as the items. Here is what I have so far.
dtGroups是一个装有SQL数据适配器的数据表。
dtGroups is a data table that was filled with an SQL data adapter.
dvGroups = dtGroups.DefaultView
'Set up datagrid view
With dgvToPopulate
.Columns.Clear()
.AutoGenerateColumns = False
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells
.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells
.DataSource = dtGroups
With .Columns
Dim groupNumColumn, groupNameColumn As New DataGridViewTextBoxColumn
With groupNumColumn
.DataPropertyName = "GROUPNUM"
.HeaderText = "Group Number"
.ReadOnly = True
End With
With groupNameColumn
.DataPropertyName = "GROUPNAME"
.HeaderText = "Group Name"
.ReadOnly = True
End With
Dim coachingColumn As New DataGridViewComboBoxColumn
With coachingColumn
.HeaderText = "RN Coaching"
.Items.AddRange({"Y", "N"})
.DataPropertyName = "COACHING"
.DisplayMember = .DataPropertyName
.ValueMember = .DisplayMember
End With
.AddRange({groupNumColumn, groupNameColumn, coachingColumn})
End With
End With
网格按我想要的方式设置,并显示t除所有comboBox以外的所有数据均未选中。我如何根据该字段中存储的内容获取comboBox的Y或N。
The grid is set up the way I want and it is displaying the all the data except all the comboBoxes have nothing selected. How do I get the comboBox to have a Y or N in them based on what was stored in that field.
任何帮助,我们将不胜感激。
Any help with this would be appreciated.
推荐答案
DisplayMember
和 ValueMember
链接到 DataSource
属性。只要您更改这些属性之一,就将重置数据连接。换句话说:items-collection已清除。
Both DisplayMember
and ValueMember
are linked to the DataSource
property. Whenever you change one of these properties, the data connection is reset. In other words: the items-collection is cleared.
以您的示例为例。 String
类没有名为 COACHING
的成员。它具有 Length
和 Chars
等属性。因此,您无法创建绑定。此外,items-collection接受所有类型的对象。
Take your example for instance. The String
class do not have a member named COACHING
. It has properties like Length
and Chars
etc. So you cannot create a binding. Also, the items-collection accepts all kinds of objects.
您需要创建并绑定自定义数据源。以下是使用 DataTable
的示例:
You need to create and bind a custom data source. Here's an example using a DataTable
:
Dim ynTable As New DataTable()
With ynTable
.Columns.Add("Value", GetType(String))
.Rows.Add("Y")
.Rows.Add("N")
.AcceptChanges()
End With
With coachingColumn
.HeaderText = "RN Coaching"
.DataSource = ynTable
.DataPropertyName = "COACHING"
.DisplayMember = "Value"
.ValueMember = "Value"
End With
这篇关于将数据加载到DataGridViewComboBoxColumn VB.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!