简体   繁体   中英

Make particular column as combo box of DataGridView

I just want to make Data Grid. Where first and second column of data grid is for storing single value and third column as combo box.

The code that i tried is

    Dim productGrid As New DataGridView        
    ProductGrid.Columns(0).Name = "CB"
    ProductGrid.Columns(1).Name = "ProductGroup"
    ProductGrid.Columns(2).Name = "Product"
    Dim i As Integer
    With ProductGrid
        If .Rows.Count = 0 Then Exit Sub
        i = 1
        Dim objValueItems As New DataGridViewComboBoxCell
        objValueItems.Items.Add("Server")
        objValueItems.Items.Add("Standalone")
        objValueItems.Items.Add("Demo")
        objValueItems.Items.Add("Anywhere")
        ProductGrid.Item(2, i).Value = objValueItems
    End With

I am getting the error on "ProductGrid.Item(2, i).Value = objValueItems" this line. Error is " Index was out of range.

Bear in mind that all the cells of a ComboBox DGV Column have the same contents, thus you are not assigning a list of items to a given cell, but to all of them (to a column). You have to include this when adding the given column. Sample code:

Dim productGrid As New DataGridView
With productGrid
    .Columns.Add("CB", "CB") 'Text-type column
    .Columns.Add("ProductGroup", "ProductGroup")  'Text-type column
    Dim objValueItems As New DataGridViewComboBoxColumn
    With objValueItems
        .Name = "Product"
        .Items.Add("Server")
        .Items.Add("Standalone")
        .Items.Add("Demo")
        .Items.Add("Anywhere")
    End With
    .Columns.Add(objValueItems) 'ComboBox-type column
End With

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM