将数据加载到 DataGridViewComboBoxColumn VB.NET

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25452853/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 18:05:43  来源:igfitidea点击:

Load data into DataGridViewComboBoxColumn VB.NET

vb.netdatagridviewdatagridviewcombobox

提问by slister

Hi I have been banging my head against the wall all day trying to figure this out.

嗨,我整天都在用头撞墙,试图弄清楚这一点。

I have a DataGridView that is displaying the results of an SQL Query

我有一个显示 SQL 查询结果的 DataGridView

The query returns 3 fields: GROUPNUM, GROUPNAME, COACHING

查询返回 3 个字段:GROUPNUM、GROUPNAME、COACHING

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.

组字段中只有字符串并且显示正常,但 COACHING 字段是一个单字符字段,其中将包含 Y 或 N。对于该列,我想要一个带有 Y 或 N 作为项目的组合框。这是我到目前为止所拥有的。

dtGroups is a data table that was filled with an SQL data adapter.

dtGroups 是一个填充了 SQL 数据适配器的数据表。

            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

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.

网格按照我想要的方式设置,它显示所有数据,除了所有组合框都没有选择任何内容。如何根据该字段中存储的内容让组合框在其中包含 Y 或 N。

Any help with this would be appreciated.

对此的任何帮助将不胜感激。

回答by Bj?rn-Roger Kringsj?

Both DisplayMemberand ValueMemberare linked to the DataSourceproperty. Whenever you change one of these properties, the data connection is reset. In other words: the items-collection is cleared.

双方DisplayMemberValueMember链接到DataSource属性。无论何时更改这些属性之一,都会重置数据连接。换句话说:items-collection 被清除了。

Take your example for instance. The Stringclass do not have a member named COACHING. It has properties like Lengthand Charsetc. So you cannot create a binding. Also, the items-collection accepts all kinds of objects.

以你的例子为例。该String班没有一个名为成员COACHING。它有样特性LengthChars等,所以您不能创建一个具有约束力。此外,items-collection 接受各种对象。

You need to create and bind a custom data source. Here's an example using a DataTable:

您需要创建和绑定自定义数据源。这是使用 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