如何在 VB.net 中以编程方式填充 DataGridViewComboBoxColumn?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26986557/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to programatically populate a DataGridViewComboBoxColumn in VB.net?
提问by Jeanno
I have been scratching my head for a while over this.
我一直为此挠头。
So I have added in design mode a datagridviewto my form.
所以我在datagridview我的表单中添加了设计模式 a 。
The datagridview has 2 columns, one column is textbox column, the other column is combobox column.
datagridview 有 2 列,一列是文本框列,另一列是组合框列。
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 dropdownwith 3 options. Any ideas would be great.
我只是想拥有dropdown3 个选项。任何想法都会很棒。
P.S: I just picked up VB.net 2 days ago so I apologize if the question is primitive :)
PS:我 2 天前刚刚拿起 VB.net,所以如果问题很原始,我深表歉意:)
回答by Codemunkeee
If you have a DataSourcein your combobox, you can do this
如果你DataSource的组合框中有一个,你可以这样做
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.
请注意,当您在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
回答by Rei Salazar
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)
回答by Ainsworth
I do this in the DataGridView_RowsAddedevent
我在DataGridView_RowsAdded事件中执行此操作
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.
您可能已经注意到我使用了 gtQualifier.Index。我更喜欢使用单元格的实际名称,但在我的情况下,您也可以使用 Cell("gtQualifier") 或仅使用索引:Cell(0)。
You can skip the DirectCastcall (using the commented code directly after it), but then Visual Studiowill 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:
您可以跳过 DirectCast调用(在它之后直接使用注释代码),但是Visual Studio会警告您隐式转换(没什么大不了的)在 C# 中,您需要显式转换,因为它不执行隐式转换。你会写:
(DataGridViewComboBoxCell)gridAddTable.Rows(e.RowIndex).Cells(gtQualifier.Index)
If you code this in the DataGridView_RowsAddedevent, you will need to make sure that tableMnemonics is populated prior to the call to InitializeComponentsbecause InitializeComponents adds the first new row. The InitializeComponents call is located in the New()subroutine in your .Designerfile. (in C# Newis in the FormName.cs file)
如果在DataGridView_RowsAdded事件中对此进行编码,则需要确保在调用InitializeComponents之前填充 tableMnemonics,因为 InitializeComponents 添加了第一个新行。InitializeComponents 调用位于.Designer文件中的New()子例程中。(在 C# 中New位于 FormName.cs 文件中)
Alternatively, you could put this code in the UserAddedRowevent. In this case you would need to populate the first added row in your form's Loadevent.
或者,您可以将此代码放在UserAddedRow事件中。在这种情况下,您需要在表单的Load事件中填充第一个添加的行。

