2012-03-21 130 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 2012-03-21 17:30:44

+0

是的,我想設置任何選定的索引 – 2012-03-21 17:41:01

+0

我的意思是在你想要設置什麼事件或行動? – PraveenVenu 2012-03-21 17:44:35

回答

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

請看我上次編輯 – 2012-03-21 17:58:59

+0

上面的代碼是不正確的 - 投票答案down 1 – MC9000 2015-07-11 20:05:47