C# DataGridView Automatically Adding One _Extra_ Row
Asked Answered
S

1

5

[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

Stagger answered 17/8, 2012 at 9:17 Comment(0)
M
13

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

DataGridView.AllowUserToAddRows = false

Mirador answered 17/8, 2012 at 9:22 Comment(1)
Ahh!! That's simple enough... Thanks!Stagger

© 2022 - 2024 — McMap. All rights reserved.