正在初始化 VB.NET DataGridView 以包含所有组合框;从数组填充的按钮

Initializing VB.NET DataGridView to have all Combo Boxes; Button to populate from Array

我正在尝试将 DataGridView 初始化为 12 列乘 15 行(带标签),并且它们都是组合框。稍后在代码中,我计划用名为 names() 的单个数组中的项目填充每个下拉列表。但是我什至无法通过加载代码。

此外,我是否有机会获得有关稍后如何将 names() 数组添加到每个组合框的示例或一些提示?我想按下一个运行 REGEX 的按钮来从数组的文本文件中提取值。

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        DataGridView1.RowHeadersWidth = 50
        Dim comboBoxCell As New DataGridViewComboBoxCell
        For row As Integer = 0 To 14            
            Dim i As Integer = DataGridView1.Rows.Add()
            DataGridView1.Rows(i).HeaderCell.Value = (i + 1).ToString()
            For col As Integer = 0 To 11
                DataGridView1(col, row) = comboBoxCell
            Next
        Next
    End Sub

Private Sub RefreshButton_Click(sender As Object, e As EventArgs) Handles RefreshButton.Click

    '***** REGEX code to extract name values from text file add to array called names() *****

    '***** some more code to enumerate DataGridView1 combo boxes with names() array *****

    End Sub

您真的应该阅读一些有关如何使用 DataGridView 控件的资料。

这是一个让您入门的解决方案,以便您可以处理数据加载:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    DataGridView1.RowHeadersWidth = 50

    'Initialise the grid rows and columns. Columns must be specified first.
    'No need to add individually for initial state.
    DataGridView1.ColumnCount = 12
    DataGridView1.RowCount = 15

    For row As Integer = 0 To 14
        DataGridView1.Rows(row).HeaderCell.Value = (row + 1).ToString()
        For col As Integer = 0 To 11
            'Each cell in the grid needs a new instance of combobox
            DataGridView1(col, row) = New DataGridViewComboBoxCell
        Next
    Next

End Sub

Private Sub RefreshButton_Click(sender As Object, e As EventArgs) Handles RefreshButton.Click

    '***** REGEX code to extract name values from text file add to array called names() *****

    Dim names As String() = {"select", "one", "two", "three", "four", "five"}


    '***** some more code to enumerate DataGridView1 combo boxes with names() array *****

    Dim combo As DataGridViewComboBoxCell
    For row = 0 To DataGridView1.Rows.Count - 1
        For col = 0 To DataGridView1.Columns.Count - 1
            combo = DataGridView1(col, row)
            combo.DataSource = names
        Next
    Next

End Sub