vb.net 无法设置值成员/显示成员来绑定组合框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22816159/
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 17:17:19  来源:igfitidea点击:

Unable to set valuemember/display member to bind combobox

vb.netdatagridviewcombobox

提问by Kat

I'm having trouble adding dynamically a combo box column to my datagridview (before you ask, yes it must be dynamic and not done in editor).

我在将组合框列动态添加到我的 datagridview 时遇到了麻烦(在你问之前,它必须是动态的,而不是在编辑器中完成)。

The main feature is that the combobox cell is different for each row, so it must be done using combo box cell. checkedRows is a datatable.

主要特点是每行的组合框单元格都不同,所以必须使用组合框单元格来完成。CheckRows 是一个数据表。

Name of the datagridview is editCameraTable. It already has a few columns at this point:

数据网格视图的名称是 editCameraTable。此时它已经有几列:

'create new column
            Dim resComboColumn As New DataGridViewComboBoxColumn _
                With {.HeaderText = "Resolution", .ReadOnly = False, .DisplayIndex = 7, .Name = "Resolution", _
                      .DisplayMember = "Name", .ValueMember = "ID", .DataPropertyName = "ID"}

           'add combo box column 

            EditCameras.editCameraTable.Columns.Insert(17, resComboColumn)
            addResCmbBox(checkedRows, resComboColumn)

Pretty straight forward. You'll notice I have the value member, dataproperty name, etc. Here's the addResCmbBox definition:

很直接。您会注意到我有值成员、数据属性名称等。这是 addResCmbBox 定义:

Public Function addResCmbBox(ByRef DT As DataTable, column As DataGridViewComboBoxColumn)

    Dim resolutions As String()
    'for each camera 
    For i As Integer = 0 To DT.Rows.Count - 1

        Dim camera As camera = convertDTtoCam(DT, i)

        'get the resarray 
        Select Case DT.Rows(i).Item("Maker").ToString.ToLower

            Case "acti"
                resolutions = ACTi.GetResArray(camera)
            Case Else
                resolutions = ACTi.GetResArray(camera)
        End Select

        'add items to combobox list 
        Dim comboCell As New DataGridViewComboBoxCell

        comboCell.DataSource = resolutions

        For j As Integer = 0 To resolutions.Length - 1

            'set to current resolution value
            If resolutions(j).TrimStart("N") = camera.res Then
                comboCell.Value = resolutions(j)

            End If

        Next
        comboCell.DisplayMember = "Name"
        comboCell.ValueMember = "ID"
        EditCameras.editCameraTable("Resolution", i) = comboCell


    Next
    Return Nothing
End Function

camera is a structure. I have no problems until I get to the displayMember and value member problem, i.e. the last line starting with "editcameras.editcameratable...".

相机是一种结构。在我遇到 displayMember 和 value 成员问题之前,我没有任何问题,即以“editcameras.editcameratable...”开头的最后一行。

When doing so, the exception of "The Field Name does not exist" pops up. If I don't assign the displayMember and valueMember, I have no problems. But, I can't get the value selected in the comboBox (it comes back as Null). At runtime, the combobox column has the valuemember and display name as "ID" and "Name".

这样做时,会弹出“字段名称不存在”的异常。如果我不分配 displayMember 和 valueMember,我就没有问题。但是,我无法获得在组合框中选择的值(它返回为 Null)。在运行时,组合框列的值成员和显示名称为“ID”和“名称”。

How can I bind this comboboxcell to the row so that I can later get it's selected value?

如何将此组合框单元格绑定到该行,以便稍后可以获取它的选定值?

UPDATE:

更新:

I did as was commented, and created a struct/class that was meant to be the resolution property:

我按照评论做了,并创建了一个结构/类,它是分辨率属性:

 Public Class ResolutionStruct
 Property Name As String
 Property ID As String
 End Class

And within the loop I create a list of this class, and assign the values to it:

在循环中,我创建了一个此类的列表,并将值分配给它:

      Dim resolutionList As New List(Of ACTi.ResolutionStruct)

        For j As Integer = 0 To resolutions.Length - 1

            Dim resClass As New ACTi.ResolutionStruct
            resClass.Name = resolutions(j)
            resClass.ID = resolutions(j)
            resolutionList.Add(resClass)


        Next

        'set combocell values
        comboCell.DisplayMember = "Name"
        comboCell.ValueMember = "ID"
        comboCell.DataSource = resolutionList

        EditCameras.editCameraTable("Resolution", i) = comboCell

However, the comboboxCell doesn't show any value when it drops down. So, now I've bound the values but it shows nothing. Is there anything else I should be doing so that I get both the holy combo of seeing the values I'm picking AND having them be bound to the data grid view? :D

但是,comboboxCell 在下降时不显示任何值。所以,现在我已经绑定了这些值,但它什么也没显示。还有什么我应该做的,以便我获得查看我选择的值并将它们绑定到数据网格视图的神圣组合?:D

UPDATE 2So, mea culpa. I was adding the combobox cell to the wrong column!

更新 2所以,我的罪过。我正在将组合框单元格添加到错误的列中!

So now, it is showing the values. I click a value, and try to grab the selected value as as string:

所以现在,它正在显示值。我单击一个值,并尝试将所选值作为字符串获取:

  Dim cmbbox2 As DataGridViewComboBoxCell = editCameraTable("Resolution", i)
                 resolution(i) = cmbbox2.Selected.ToString()

But it still says it's a null value! Mid build I checked the combobox props. IN fact "selected" is a boolean as false. It has no value, says it has no items as well. Any ideas on why it says it is null?

但它仍然说它是一个空值!中期构建我检查了组合框道具。事实上,“selected”是一个布尔值,为false。它没有价值,说它也没有物品。关于为什么它说它为空的任何想法?

UPDATE3:

更新3

I recently resorted a different column in the table, and the values from the combo box are cleared! I guess it's really never being attached/bound in the first place.

我最近在表中重新使用了一个不同的列,组合框中的值被清除了!我想它真的从来没有被附加/绑定过。

UPDATE4:

更新4

Fixed it!!

修复!!

Apparently this line: editCameraTable.Sort(editCameraTable.Columns("ID"), System.ComponentModel.ListSortDirection.Ascending)

显然这一行: editCameraTable.Sort(editCameraTable.Columns("ID"), System.ComponentModel.ListSortDirection.Ascending)

Caused the table to freak out! I can now get the value (woohoo!)

把桌子吓坏了!我现在可以获得价值(呜呼!)

回答by WozzeC

Right, I'll try to explain this shortly:

是的,我会尽快解释一下:

DisplayMember and ValueMember are supposed to be set using properties. For example you create a class containing Name and ID

DisplayMember 和 ValueMember 应该使用属性设置。例如,您创建一个包含 Name 和 ID 的类

Public Class Test
  Property Name as String
  Property ID as String
End Class

Create a few of these objects and put them in a list. Set the list as the datasource to the combobox. Now you can access the DisplayMember and ValueMember as you have written it in your code. Value would be the ID and SelectedItem would be the entire class.

创建一些这样的对象并将它们放在一个列表中。将列表设置为组合框的数据源。现在您可以访问您在代码中编写的 DisplayMember 和 ValueMember。值将是 ID,SelectedItem 将是整个类。

What you are doing now is that you are adding a list of strings to the combobox. A String does not contain the Property Name nor ID, so naturally you can't fetch them. See it like this:

您现在正在做的是将字符串列表添加到组合框。字符串不包含属性名称和 ID,因此您自然无法获取它们。像这样看:

To be able to use Value and/or DisplayMember you need to be able to fetch the Property by yourself. In this case:

为了能够使用 Value 和/或 DisplayMember,您需要能够自己获取属性。在这种情况下:

resolutions(j).Name 
or 
resolutions(j).ID

This does not work.

这不起作用。

But for example you would be able to do this:

但例如,您可以这样做:

resolutions(j).Length

So You would be able to do this, which would display the Length in the combobox:

所以你可以这样做,这将在组合框中显示长度:

Combobox.DisplayMember = "Length"

To currently get the value you would have to do:

要当前获得您必须执行的值:

Combobox.SelectedItem.ToString()

But since you have it in a combobox column My guess is that this won't cut it since you can't fetch the value from the DataGridView.

但是由于您在组合框列中拥有它,我的猜测是这不会削减它,因为您无法从 DataGridView 中获取值。

EDIT:You are still doing this right?

编辑:你仍然这样做吗?

<DataGridView>.Item("Resolution", i) = comboCell

Otherwise you will have empty comboboxes.

否则,您将有空的组合框。

EDIT2:No need to fetch value from Combobox, get it from Grid cell instead:

EDIT2:不需要从 Combobox 中获取值,而是从 Grid 单元格中获取它:

<DataGridView>.Item("Resolution", i).Value

When creating the columns don't forget to set a defaultvalue to the combobox, otherwise it might be Nothing:

创建列时不要忘记为组合框设置默认值,否则它可能是 Nothing:

comboCell.DisplayMember = "Name"
comboCell.ValueMember = "ID"
comboCell.Value = resolutions(0)