DataGridViewRowCollection.GetRowCount(DataGridViewElementStates) 方法

定义

返回集合中符合指定的条件的 DataGridViewRow 对象的数目。

public:
 int GetRowCount(System::Windows::Forms::DataGridViewElementStates includeFilter);
public int GetRowCount (System.Windows.Forms.DataGridViewElementStates includeFilter);
member this.GetRowCount : System.Windows.Forms.DataGridViewElementStates -> int
Public Function GetRowCount (includeFilter As DataGridViewElementStates) As Integer

参数

includeFilter
DataGridViewElementStates

DataGridViewElementStates 值的按位组合。

返回

DataGridViewRowCollection 中具有 includeFilter 指定的属性的 DataGridViewRow 对象的数目。

例外

includeFilter 不是 DataGridViewElementStates 值的有效按位组合。

示例

下面的代码示例演示了如何使用此方法获取所选行数。

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");
    }
}
Private Sub selectedRowsButton_Click( _
    ByVal sender As Object, ByVal e As System.EventArgs) _
    Handles selectedRowsButton.Click

    Dim selectedRowCount As Integer = _
        dataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected)

    If selectedRowCount > 0 Then

        Dim sb As New System.Text.StringBuilder()

        Dim i As Integer
        For i = 0 To selectedRowCount - 1

            sb.Append("Row: ")
            sb.Append(dataGridView1.SelectedRows(i).Index.ToString())
            sb.Append(Environment.NewLine)

        Next i

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

    End If

End Sub

适用于

另请参阅