Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

VB.NET WinForm: Dynamically add DataGridView rows Using ComboBox selection for number of rows

$
0
0

Hi,

I have a Windows Form that has an unbound DataGridView control.  I dynamically add two DataGridViewComboBox columns on form load.

There is a combobox that users can select from numbers 1 to 15.  Depending on the selection, I need to add the corresponding number of rows to the DataGridView.

Problem:  The number of rows does not reset on ComboBox selected index changed event.  How do I solve this?

Below are my code snippets:

Private Sub AddDataGridViewColumns()
        'Add combobox columns'
        Dim CboxCol1 As New DataGridViewComboBoxColumn() With {
            .Name = "CboxCol1",
            .HeaderText = "Plant",
            .MaxDropDownItems = 15,
            .Width = 300
        }

        Dim CboxCol2 As New DataGridViewComboBoxColumn() With {
            .Name = "CboxCol2",
            .HeaderText = "Management Plan",
            .MaxDropDownItems = 15,
            .Width = 500
        }

        DgvMgt.Columns.Add(CboxCol1)
        DgvMgt.Columns.Add(CboxCol2)
End Sub

Private Sub AddDataGridViewRows(rownum As Integer)
        For i As Integer = 1 To rownum
            DgvMgt.Rows.Add(i)
        Next
End Sub

Private Sub CboxPlanNum_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CboxPlanNum.SelectedIndexChanged
        Dim rownum As Integer = CInt(CboxPlanNum.SelectedItem)
        AddDataGridViewRows(rownum)
End Sub

Appreciate any response.


Marilyn Gambone


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>