How to programatically populate a DataGridViewComboBoxColumn in VB.net?

22.5k views Asked by At

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 :)

3

There are 3 answers

0
Codemunkeee On BEST ANSWER

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
0
Ainsworth On

I do this in the DataGridView_RowsAdded event

My code looks like this:

    Dim qualifierCell As DataGridViewComboBoxCell =
        DirectCast(gridAddTable.Rows(e.RowIndex).Cells(gtQualifier.Index), DataGridViewComboBoxCell)
       'gridAddTable.Rows(e.RowIndex).Cells(gtQualifier.Index)
    For t As Int32 = 0 To tableMnemonics.Count - 1
        qualifierCell.Items.Add(tableMnemonics(t))
    Next

Notes:

You may have noticed my use of gtQualifier.Index. I prefer using the actual name of the cell but you could also use Cell("gtQualifier") or just the index: Cell(0) in my case.

You can skip the DirectCast call (using the commented code directly after it), but then Visual Studio will warn you about an implicit cast (no big deal) In C# you need to explicitly cast because it does not do implicit casting. You would write:

 (DataGridViewComboBoxCell)gridAddTable.Rows(e.RowIndex).Cells(gtQualifier.Index)

If you code this in the DataGridView_RowsAdded event, you will need to make sure that tableMnemonics is populated prior to the call to InitializeComponents because InitializeComponents adds the first new row. The InitializeComponents call is located in the New() subroutine in your .Designer file. (in C# New is in the FormName.cs file)

Alternatively, you could put this code in the UserAddedRow event. In this case you would need to populate the first added row in your form's Load event.

0
Rei Salazar On

Try this:

'Declare ComboBoxColumn
Dim cbColumn As New DataGridViewComboBoxColumn
cbColumn.Name = "Column ComboBox"

'Add Values 
For value As Integer = 0 To 5
    cbColumn.Items.Add("Value = " & value.ToString)
Next

'Add ComboBox 
DataGridView1.Columns.Add(cbColumn)