DataGridView::RowCount Property

 

Gets or sets the number of rows displayed in the DataGridView.

Namespace:   System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)

public:
[BrowsableAttribute(false)]
property int RowCount {
	int get();
	void set(int value);
}

Property Value

Type: System::Int32

The number of rows to display in the DataGridView.

Exception Condition
ArgumentException

The specified value when setting this property is less than 0.

-or-

The specified value is less than 1 and AllowUserToAddRows is set to true.

InvalidOperationException

When setting this property, the DataSource property is set.

If RowCount is set to a value less than the current value, rows will be removed from the end of the Rows collection. If RowCount is set to a value greater than the current value, rows will be added to the end of the Rows collection. The additional rows are based on the row specified in the RowTemplate property.

If you set the RowCount property to 0, all rows will be removed from the DataGridView. This is equivalent to calling the DataGridViewRowCollection::Clear method.

System_CAPS_noteNote

If AllowUserToAddRows is true, you cannot set RowCount to 0. In this case, call the DataGridViewRowCollection::Clear method to remove all rows except the row for new records. Calling Clear has the same result as setting RowCount to 1 in this case, but is much faster.

The RowCount property can be used with the ColumnCount property to create a simple DataGridView for displaying and editing text. If you set the RowCount property to a value greater than 0 for a DataGridView control without columns, a DataGridViewTextBoxColumn is added automatically.

The following code example demonstrates the use of the RowCount property. In this example, this property is used to track the number of entries in a DataGridView. This example is part of a larger example available in the SelectionChanged event.

No code example is currently available or this language may not be supported.

.NET Framework
Available since 2.0
Return to top
Show: