This documentation is archived and is not being maintained.

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 int RowCount { get; set; }

Property Value

Type: System.Int32
The number of rows to display in the DataGridView.


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


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


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.


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.

private void UpdateLabelText()
    int WithdrawalTotal = 0;
    int DepositTotal = 0;
    int SelectedCellTotal = 0;
    int counter;

    // Iterate through all the rows and sum up the appropriate columns. 
    for (counter = 0; counter < (DataGridView1.Rows.Count);
        if (DataGridView1.Rows[counter].Cells["Withdrawals"].Value
            != null)
            if (DataGridView1.Rows[counter].
                Cells["Withdrawals"].Value.ToString().Length != 0)
                WithdrawalTotal += int.Parse(DataGridView1.Rows[counter].

        if (DataGridView1.Rows[counter].Cells["Deposits"].Value != null)
            if (DataGridView1.Rows[counter]
                .Cells["Deposits"].Value.ToString().Length != 0)
                DepositTotal += int.Parse(DataGridView1.Rows[counter]

    // Iterate through the SelectedCells collection and sum up the values. 
    for (counter = 0;
        counter < (DataGridView1.SelectedCells.Count); counter++)
        if (DataGridView1.SelectedCells[counter].FormattedValueType ==
            string value = null;

            // If the cell contains a value that has not been commited, 
            // use the modified value. 
            if (DataGridView1.IsCurrentCellDirty == true)

                value = DataGridView1.SelectedCells[counter]
                value = DataGridView1.SelectedCells[counter]
            if (value != null)
                // Ignore cells in the Description column. 
                if (DataGridView1.SelectedCells[counter].ColumnIndex !=
                    if (value.Length != 0)
                        SelectedCellTotal += int.Parse(value);

    // Set the labels to reflect the current state of the DataGridView.
    Label1.Text = "Withdrawals Total: " + WithdrawalTotal.ToString();
    Label2.Text = "Deposits Total: " + DepositTotal.ToString();
    Label3.Text = "Selected Cells Total: " + SelectedCellTotal.ToString();
    Label4.Text = "Total entries: " + DataGridView1.RowCount.ToString();

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0