Can I set dataGridView rows based on the value in numericUpDown

18 Views Asked by At

Using WinForm C# I'm trying to set a the number of rows in the dataGridView to match the numericUpDown default value of 4 and then have the number of rows increase/decrease in line with the numericUpDown value.

I'm quite new to this and so far I've tried the following:

    private void numericUpDown1_ValueChanged(object sender, EventArgs e)
    {
        while (dataGridView1.Rows.Count < numericUpDown1.Value) ;
        dataGridView1.Rows.Add();

        while (dataGridView1.Rows.Count > numericUpDown1.Value) ;
        dataGridView1.Rows.Remove();
    }

It seems to work in terms of adding a new row when the numericUpDown value is increased but then errors when I try to decrease.

0

There are 0 best solutions below