2012-03-21 28 views
3

DataGridViewComboBoxCellのSelectedIndexを設定するにはどうすればよいですか?DataGridViewComboBoxCellのSelectedIndex? VB.NET

コードはアイテムをコンボボックスを埋めるが、私はそれらのいずれかを選択する必要が

マイコード:

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

       If ds.Tables("items_prices").Rows(0).Item("item_selldozen") > 0 Then 
        Dim item_selldozen As String = ds.Tables("items_prices").Rows(0).Item("item_selldozen") 
        cListItems.Add(New Combobox_values("Docena (" + item_selldozen + ")", item_selldozen)) 
       End If 


       Dim dgvcbc As DataGridViewComboBoxCell = DirectCast(CType(main.ActiveMdiChild, discount_new_discount).discountitems_new_discount.Rows(last_row).Cells(3), DataGridViewComboBoxCell) 

       dgvcbc.DataSource = cListItems 'Fill Remote Comboboxcell 
       dgvcbc.DisplayMember = "Text" 
       dgvcbc.ValueMember = "Value" 
+0

を? – PraveenVenu

+0

はい選択したインデックスを設定したい –

+0

設定したいイベントやアクション中に意味しましたか? – PraveenVenu

答えて

4

あなたのDataGridViewでComboBoxColumnを持って、あなたは何をお知りになりたい場合はコンボボックスの選択したインデックスがあり、あなたはこれを実行する必要があります。

  1. がEditingConハンドルDataGridViewのtrolShowingイベントです。このイベントハンドラで、現在の列が関心があるかどうかを確認します。その後、我々は一時的なコンボボックスオブジェクトを作成し、選択したインデックスを取得 :あなたが選択したインデックスを設定したいんとき
Private Sub dataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) 
    If dataGridView1.CurrentCell.ColumnIndex = 0 Then 
     ' Check box column 
     Dim comboBox As ComboBox = TryCast(e.Control, ComboBox) 
     comboBox.SelectedIndexChanged += New EventHandler(AddressOf comboBox_SelectedIndexChanged) 
    End If 
End Sub 


Private Sub comboBox_SelectedIndexChanged(sender As Object, e As EventArgs) 
    Dim selectedIndex As Integer = DirectCast(sender, ComboBox).SelectedIndex 
    MessageBox.Show("Selected Index = " & selectedIndex) 
End Sub 
+0

私の最後の編集を参照してください –

+0

上記のコードは間違っています - 投票ダウン1 – MC9000

関連する問題