How to programatically populate a DataGridViewComboBoxColumn in VB.net?

旧街凉风 提交于 2019-12-08 04:42:40

问题


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


回答1:


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



回答2:


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)



回答3:


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.



来源:https://stackoverflow.com/questions/26986557/how-to-programatically-populate-a-datagridviewcomboboxcolumn-in-vb-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!