2

我一直在为此挠头。

所以我在设计模式中添加了datagridview我的表单。

datagridview 有 2 列,一列是文本框列,另一列是组合框列。

我已经想出了如何以编程方式填充文本框的单元格,但是我不知道要使用哪个属性来填充组合框列。

我只是希望有dropdown3 个选项。任何想法都会很棒。

PS:我两天前刚拿起VB.net,所以如果问题很原始,我深表歉意:)

4

3 回答 3

5

如果你DataSource的组合框中有一个,你可以这样做

Dim dgvcc As New DataGridViewComboBoxCell
With dgvcc
   .DataSource = answerStr
   .ValueMember = "CampaignAnswerId"
   .DisplayMember = "Answer"
End With

DataGridViewName.Item(columnIndex, rowIndex) = dgvcc

或者你可以这样做

Dim dgvcc As New DataGridViewComboBoxCell
dgvcc.Items.Add("test1")
dgvcc.Items.Add("test2")
dgvcc.Items.Add("test3")

DataGridViewName.Item(columnIndex, rowIndex) = dgvcc

请注意,当您在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
于 2014-11-18T08:55:18.767 回答
0

试试这个:

'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)
于 2016-03-24T21:10:47.870 回答
0

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.

于 2019-08-14T18:23:11.633 回答