简体   繁体   中英

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额外行

Thanks!

Jan

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

DataGridView.AllowUserToAddRows = false

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM