C # DataGridView automatically adds one _Extra_ Row

[Microsoft Visual Studio 2008, Windows 7 Professional 64]

I have a C # class that extends a DataGridView:

public class DataGridViewTest : DataGridView 

This class programmatically sets the number of columns and rows.

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

The 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 installs everything:

 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.

However, when the application starts, 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 line added?

Here is a screenshot:

DataGridView Extra Row

Thanks!

Jan

+4
source share
1 answer

The third line is adding new lines. Set the property as follows:

DataGridView.AllowUserToAddRows = false

+9
source

All Articles