vb.net 如何使用此代码创建 DataGridView 列

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15507390/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:52:00  来源:igfitidea点击:

How to use this code to create DataGridView columns

vb.netdatagridview

提问by user2103442

I found this code on the web several months ago. It is great for creating datagridview columns. I have never used it to create a comboboxcolumn. Now I need to do that and can't figure out how to assign values to the column. And, I can't find where I first found this code. I can find snippets of it now but nothing complete.

几个月前我在网上找到了这段代码。它非常适合创建 datagridview 列。我从未用它来创建组合框列。现在我需要这样做,但无法弄清楚如何为列分配值。而且,我找不到我第一次找到这段代码的地方。我现在可以找到它的片段,但没有完整的。

It's easy to create a textboxcolumn and create dgv rows with values in them. But, I don't know how to create a comboboxcolumn. By the way, in this case, the column can use the same combobox values for each row.

创建 textboxcolumn 并创建带有值的 dgv 行很容易。但是,我不知道如何创建组合框列。顺便说一下,在这种情况下,列可以对每一行使用相同的组合框值。

Here's the code I have:

这是我的代码:

I create a datatable to hold the data to be bound to the dgv:

我创建了一个数据表来保存要绑定到 dgv 的数据:

Dim dt As New DataTable("Grid")
Dim dr As DataRow
dtSetupColumns(dt)

Public Sub dtSetupColumns(ByRef dt As DataTable)
        dt.Columns.Add("name_l", GetType(String))
        dt.Columns.Add("name_f", GetType(String))
        dt.Columns.Add("state", GetType(String))
End Sub

Assign values to the datatable:

为数据表赋值:

dr = dt.NewRow()
dr("name_l") = ' from query table
dt.Rows.Add(dr)

Create the DataGridView Columns:

创建 DataGridView 列:

dgvCreateColumns(dgv)

Public Sub dgvCreateColumns(ByRef inDataGridView As DataGridView)
  With inDataGridView.Columns
    .Add(dgvCreateColumn(ColumnStyle.TextBoxColumn, String.Empty, "Last Name", "name_l", 80, DataGridViewContentAlignment.MiddleLeft))
    .Add(dgvCreateColumn(ColumnStyle.TextBoxColumn, String.Empty, "First Name", "name_f", 80, DataGridViewContentAlignment.MiddleLeft))
    .Add(dgvCreateColumn(ColumnStyle.ComboBoxColumn, String.Empty, "State", "state", 80, DataGridViewContentAlignment.MiddleLeft))
  End With
End Sub


Public Shared Function dgvCreateColumn(ByVal ColumnType As ColumnStyle, ByVal format As String, ByVal headerText As String, ByVal dataPropertyName As String, ByVal width As Integer, ByVal alignment As DataGridViewContentAlignment, Optional ByVal bMakeVisible As Boolean = True, Optional ByVal inReadOnly As Boolean = False) As DataGridViewColumn
        Dim dgvC As DataGridViewColumn = Nothing

        Select Case ColumnType
            Case ColumnStyle.ButtonColumn
                dgvC = New DataGridViewButtonColumn
            Case ColumnStyle.CheckBoxColumn
                dgvC = New DataGridViewCheckBoxColumn
            Case ColumnStyle.ComboBoxColumn
                dgvC = New DataGridViewComboBoxColumn
            Case ColumnStyle.ImageColumn
                dgvC = New DataGridViewImageColumn
            Case ColumnStyle.LinkColumn
                dgvC = New DataGridViewLinkColumn
            Case ColumnStyle.TextBoxColumn
                dgvC = New DataGridViewTextBoxColumn
        End Select

        With dgvC
            .DataPropertyName = dataPropertyName
            .DefaultCellStyle.Alignment = alignment
            .DefaultCellStyle.Format = format
            If ColumnType = ColumnStyle.ButtonColumn Then
                '.colum()
            End If
            .HeaderText = headerText
            .Name = headerText
            .ReadOnly = inReadOnly
            If width = 0 Then
                .Visible = False
            Else
                .Width = width
                If bMakeVisible = False Then
                    .Visible = False
                Else
                    .Visible = True

                End If
            End If
        End With

        Return dgvC
    End Function

采纳答案by Ruben_PH

You have to go thru each Cell in each Row

你必须通过每一行中的每个单元格

For i as Integer = 0 to dgv.Rows.Count -1 
   Dim gridComboBox As New DataGridViewComboBoxCell
   gridComboBox.DataSource = dt
   gridComboBox.ValueMember = dt.Columns(0).ColumnName
   dgv.Item(2, i) = gridComboBox 'Change the Cell in Column:2 Row:i to ComboBox
Next