Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# DataGridView Automatically Adding One _Extra_ Row

[Microsoft Visual Studio 2008, Windows 7 Professional 64]

I have a C# class that extends DataGridView:

public class DataGridViewTest : DataGridView

This class programatically sets the number of columns and rows.

I have a Form application that creates an instance of DataGridViewTest and adds it to a GroupBox.

DataGridViewTest defines static members for the number of columns and the number of rows:

private static int NUM_COLUMNS = 2;
private static int NUM_ROWS = 2;

Here is the code that sets everything up:

public DataGridViewTest()
{
    for (int i = 0; i < NUM_COLUMNS; i++)
    {
        DataGridViewTextBoxColumn column = new DataGridViewTextBoxColumn();
        column.Name = "Column " + i.ToString();
        this.Columns.Add(column);
    }

    for (int i = 0; i < NUM_ROWS; i++)
    {
        DataGridViewRow row = new DataGridViewRow();
        Rows.Add(row);
    }
}

As you can see _NUM_ROWS_ is set to 2.

When the application runs, however, the DataGridViewTest shows a data grid with 3 rows, not 2. (Similarly, setting NUM_ROWS to 0 creates a data grid with 1 row.)

Why is this extra row being added?

Here is a screenshot:

DataGridView Extra Row

Thanks!

Jan

like image 712
Jan Tacci Avatar asked Dec 26 '22 19:12

Jan Tacci


1 Answers

The 3rd row is the for adding new rows. Set the property as follow:

DataGridView.AllowUserToAddRows = false

like image 124
Conrad Lotz Avatar answered Dec 31 '22 13:12

Conrad Lotz