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

Time:01-16

I am trying to initialize a DataGridView to be 12 columns by 15 rows (with labels) and them all be combo boxes. Later on in the code I plan to populate each drop down with items from a single array called names(). But I cannot even get past the load code.

Also, any chance I can get an example or some hooks on how I would later add the names() array to each combo box? I want to press a button that runs a REGEX to extract values from a text file for the array.

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

CodePudding user response:

You should really do some reading about how to use the DataGridView control.

Here's a solution to get you started so you can work on the data load:

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
  •  Tags:  
  • Related