2012-03-17 7 views
1

非常に良い午後、 私は今、私はコンボボックスとして選択された値を得ることができないということです、私は各項目にテキストと値を設定しようとしていますデータグリッド内の各セルのコンボボックス。 マイコード:DataGridViewComboBoxCellのSelectedItemを取得する

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 

フォームのLoad:

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) 

ERROR: 'のSelectedItemは' ではありません'System.Windows.Forms.DataGridViewCom'のメンバーboBoxCell」

誰もがコンボボックスで、各セルの各項目に値やテキストを設定する方法任意のアイデアを持っている場合、私はあなたがに応じValueプロパティを使用する必要が非常に感謝感謝

答えて

1

だろうMSDN documentationは:

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をロードするには、DataSourceを設定する必要があります。

データソース内のデータのタイプによっては、DisplayMemberを設定して、コントロールの表示部分に表示するプロパティまたは列名を選択し、ValueMemberを使用してプロパティまたは列名を選択する必要がありますアイテムが選択されたときにコントロールのValueプロパティを設定するために使用されます。ここで

は、データソース上のMSDNからいくつかの追加のガイダンスです:

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

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.

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.

だからあなたの場合には、次のような何かをする必要があります:

最後に
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" 

、値があればすべてのセルで同じである場合は、作成時に列にデータソースを割り当てたいと思うかもしれません。あなたはdatagridviewcomboboxcolumnを保持する変数とdgvcbc参照に置き換えられますを除いて、上記のコードのすべてが、同じままになります。

+0

はい、しかし、あなたは私に私が割り当てるとアリコンボボックスの各項目に値を取得する方法の例を与えることができます?おかげ –

+0

私は、追加情報や問題を解決するための提案された方法で答えを更新しました。 –

+0

だけでなく、私はそれが正しく値を割り当て参照が、私はおかげで再び及びお手数?comoboboxを変更したい項目の値を示すことができるように私は、質問を閉じる前に別の質問を持っていますが、 –

関連する問題