DataGridViewRowCollection.GetRowCount(DataGridViewElementStates) メソッド

定義

コレクション内の指定した基準を満たす DataGridViewRow オブジェクトの数を返します。Returns the number of DataGridViewRow objects in the collection that meet the specified criteria.

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 値のビットごとの組み合わせ。A bitwise combination of DataGridViewElementStates values.

戻り値

DataGridViewRow 内の includeFilter で指定された属性を持つ DataGridViewRowCollection オブジェクトの数。The number of DataGridViewRow objects in the DataGridViewRowCollection that have the attributes specified by includeFilter.

例外

includeFilter は、DataGridViewElementStates 値のビットごとの正しい組み合わせではありません。includeFilter is not a valid bitwise combination of DataGridViewElementStates values.

次のコード例は、このメソッドを使用して、選択された行の数を取得する方法を示しています。The following code example illustrates the use of this method to get the number of selected rows.

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

適用対象

こちらもご覧ください