获取DataGridViewComboBoxCell VB.NET的SelectedItem [英] Get the SelectedItem of DataGridViewComboBoxCell VB.NET

查看:119
本文介绍了获取DataGridViewComboBoxCell VB.NET的SelectedItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家下午好,
我现在遇到的问题是我无法获取选择为组合框的值,我试图将文本和值设置为每个单元格组合框中的每个项目在数据网格中。
我的代码:

Very good afternoon to all, The problem I have now is that I can not get the value selected as a combobox, I'm trying to set the text and value to each item in the combobox of each cell in the datagrid. My Code:

CLASS MyListItem:

CLASS MyListItem:

Public Class MyListItem
    Private mText As String
    Private mValue As String

    Public Sub New(ByVal pText As String, ByVal pValue As String)
        mText = pText
        mValue = pValue
    End Sub

    Public ReadOnly Property Text() As String
        Get
            Return mText
        End Get
    End Property

    Public ReadOnly Property Value() As String
        Get
            Return mValue
        End Get
    End Property

    Public Overrides Function ToString() As String
        Return mText
    End Function
End Class

表格加载:

DataGridView1.Rows.Add()
Dim dgvcbc As DataGridViewComboBoxCell = DirectCast(DataGridView1.Rows(0).Cells(0), DataGridViewComboBoxCell)
dgvcbc.Items.Add(New MyListItem("Text to be displayed", "value of the item"))

尝试显示所选值:

Dim oItem As MyListItem = CType(**dgvcbc.SelectedItem**, MyListItem)
MessageBox.Show("The Value of the Item selected is: " & oItem.Value)

错误:'SelectedItem'不是以下成员'System.Windows.Forms.DataGridViewComboBoxCell'

ERROR: 'SelectedItem' is not a member of 'System.Windows.Forms.DataGridViewComboBoxCell'

如果有人知道如何使用组合框将值和文本设置为每个单元格的每个项目,非常感谢

If anyone has any idea how to set the values ​​and text to each item of each cell with combobox, I'd be very grateful thanks

推荐答案

您需要根据以下情况使用 Value 属性到 MSDN文档

You need to use the Value property according to the MSDN documentation:


与ComboBox控件不同,DataGridViewComboBoxCell没有
具有SelectedIndex和SelectedValue属性。而是从下拉列表中选择
值来设置单元格Value属性。

Unlike the ComboBox control, the DataGridViewComboBoxCell does not have SelectedIndex and SelectedValue properties. Instead, selecting a value from a drop-down list sets the cell Value property.

要加载DataGridViewComboBoxCell,您需要设置数据源

To load the DataGridViewComboBoxCell you need to set the DataSource.

根据数据源中数据的类型,您可能还需要设置DisplayMember以选择要显示在控件的显示部分中的属性或列名称,并ValueMember,以选择用于在选择项目时设置控件的Value属性的属性或列名。

Depending on the type of data in the datasource, you may also need to set the DisplayMember to choose the property or column name to show in the display portion of the control and the ValueMember to choose the property or column name that is used to set the Value property of the control when an item is selected.

以下是MSDN的一些其他指导数据源:

Here is some additional guidance from MSDN on the datasource:


通常,将通过DataGridViewComboBoxColumn.DataSource属性为单元格
的整个列设置此属性。

Typically this property will be set for an entire column of cells through the DataGridViewComboBoxColumn.DataSource property.

如果可能,请将DataSource设置为仅包含可能的
选择的源,例如选择列。然后,无需设置DisplayMember
属性。但是,如果源中的
更为复杂,请将DisplayMember设置为从中检索可能的选择的属性或列
的名称。

If possible, set DataSource to a source containing only the possible selections, like a column of selections. Then the DisplayMember property doesn't need to be set. But if the source is more complicated, set DisplayMember to the name of the property or column from which to retrieve possible selections.

如果使用DataSource设置为字符串数组,则不需要设置ValueMember和
DisplayMember,因为数组
中的每个字符串都将用于值和显示。

If DataSource is set to a string array, then ValueMember and DisplayMember do not need to be set because each string in the array will be used for both value and display.

因此,在您的情况下,您将需要执行以下操作:

So in your case, you will need to do something similar to the following:

Dim cListItems As New System.Collections.Generic.List(Of MyListItem)

cListItems.Add(New MyListItem("Text to be displayed", "value of the item"))

Dim dgvcbc As DataGridViewComboBoxCell = DirectCast(DataGridView1.Rows(0).Cells(0), DataGridViewComboBoxCell)
dgvcbc.DataSource = cListItems
dgvcbc.DisplayMember = "Text"
dgvcbc.ValueMember = "Value"

最后,如果所有单元格的值都相同,则你可能会想在创建列时将数据源添加到该列。上面的所有代码都将保持不变,只是您将 dgvcbc 引用替换为包含 datagridviewcomboboxcolumn 的变量。

Finally, if the values are the same for all cells, then you will probably want to assign the datasource to the column when you create it. All of the above code will remain the same, except you will replace the dgvcbc reference with the variable that holds the datagridviewcomboboxcolumn.

这篇关于获取DataGridViewComboBoxCell VB.NET的SelectedItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆