DataGridViewRowCollection.GetRowCount(DataGridViewElementStates) Method

Definition

Returns the number of DataGridViewRow objects in the collection that meet the specified criteria.

C#
public int GetRowCount(System.Windows.Forms.DataGridViewElementStates includeFilter);

Parameters

includeFilter
DataGridViewElementStates

A bitwise combination of DataGridViewElementStates values.

Returns

The number of DataGridViewRow objects in the DataGridViewRowCollection that have the attributes specified by includeFilter.

Exceptions

includeFilter is not a valid bitwise combination of DataGridViewElementStates values.

Examples

The following code example illustrates the use of this method to get the number of selected rows.

C#
private void selectedRowsButton_Click(object sender, System.EventArgs e)
{
    Int32 selectedRowCount =
        dataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected);
    if (selectedRowCount > 0)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();

        for (int i = 0; i < selectedRowCount; i++)
        {
            sb.Append("Row: ");
            sb.Append(dataGridView1.SelectedRows[i].Index.ToString());
            sb.Append(Environment.NewLine);
        }

        sb.Append("Total: " + selectedRowCount.ToString());
        MessageBox.Show(sb.ToString(), "Selected Rows");
    }
}

Applies to

Product Versions
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

See also