vb.netdatagridviewdatagridviewcomboboxcell

How to programatically populate a DataGridViewComboBoxColumn in VB.net?


I have been scratching my head for a while over this.

So I have added in design mode a datagridview to my form.

The datagridview has 2 columns, one column is textbox column, the other column is combobox column.

I have figured out how to programmatically populate the cells of a textbox, however I can't figure out which property to use to populate the combobox column.

I am simply looking to have a dropdown with 3 options. Any ideas would be great.

P.S: I just picked up VB.net 2 days ago so I apologize if the question is primitive :)


Solution

  • If you have a DataSource in your combobox, you can do this

    Dim dgvcc As New DataGridViewComboBoxCell
    With dgvcc
       .DataSource = answerStr
       .ValueMember = "CampaignAnswerId"
       .DisplayMember = "Answer"
    End With
    
    DataGridViewName.Item(columnIndex, rowIndex) = dgvcc
    

    or you can just do this

    Dim dgvcc As New DataGridViewComboBoxCell
    dgvcc.Items.Add("test1")
    dgvcc.Items.Add("test2")
    dgvcc.Items.Add("test3")
    
    DataGridViewName.Item(columnIndex, rowIndex) = dgvcc
    

    Take note that you have to do this while you loop inside the DataGridView.

    For rowIndex as integer = 0 To DataGridViewName.Rows.Count - 1
        Dim dgvcc As New DataGridViewComboBoxCell
        dgvcc.Items.Add("test1")
        dgvcc.Items.Add("test2")
        dgvcc.Items.Add("test3")
    
        DataGridViewName.Item(yourtextboxcolumnIndex, rowIndex) = dgvcc
    Next