DataGridView.RowCount 屬性

定義

取得或設定 DataGridView 中顯示的資料列數目。

public:
 property int RowCount { int get(); void set(int value); };
[System.ComponentModel.Browsable(false)]
public int RowCount { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.RowCount : int with get, set
Public Property RowCount As Integer

屬性值

要顯示在 DataGridView 中的資料列數目。

屬性

例外狀況

設定這個屬性時所指定的值小於 0。

-或-

指定的值小於 1 且 AllowUserToAddRows 設為 true

在設定這個屬性時,已設定了 DataSource 屬性。

範例

下列程式碼範例示範 如何使用 RowCount 屬性。 在此範例中,這個屬性是用來追蹤 中的 DataGridView 專案數目。 此範例是 事件中較大範例的 SelectionChanged 一部分。

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);
        counter++)
    {
        if (DataGridView1.Rows[counter].Cells["Withdrawals"].Value
            != null)
        {
            if (DataGridView1.Rows[counter].
                Cells["Withdrawals"].Value.ToString().Length != 0)
            {
                WithdrawalTotal += int.Parse(DataGridView1.Rows[counter].
                    Cells["Withdrawals"].Value.ToString());
            }
        }

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

    // Iterate through the SelectedCells collection and sum up the values.
    for (counter = 0;
        counter < (DataGridView1.SelectedCells.Count); counter++)
    {
        if (DataGridView1.SelectedCells[counter].FormattedValueType ==
            Type.GetType("System.String"))
        {
            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]
                    .EditedFormattedValue.ToString();
            }
            else
            {
                value = DataGridView1.SelectedCells[counter]
                    .FormattedValue.ToString();
            }
            if (value != null)
            {
                // Ignore cells in the Description column.
                if (DataGridView1.SelectedCells[counter].ColumnIndex !=
                    DataGridView1.Columns["Description"].Index)
                {
                    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();
}
Private Sub UpdateLabelText()
    Dim WithdrawalTotal As Integer = 0
    Dim DepositTotal As Integer = 0
    Dim SelectedCellTotal As Integer = 0
    Dim counter As Integer

    ' Iterate through all the rows and sum up the appropriate columns.
    For counter = 0 To (DataGridView1.Rows.Count - 1)
        If Not DataGridView1.Rows(counter) _
            .Cells("Withdrawals").Value Is Nothing Then

            If Not DataGridView1.Rows(counter) _
                .Cells("Withdrawals").Value.ToString().Length = 0 Then

                WithdrawalTotal += _
                    Integer.Parse(DataGridView1.Rows(counter) _
                    .Cells("Withdrawals").Value.ToString())
            End If
        End If

        If Not DataGridView1.Rows(counter) _
            .Cells("Deposits").Value Is Nothing Then

            If Not DataGridView1.Rows(counter) _
                .Cells("Deposits").Value.ToString().Length = 0 Then

                DepositTotal += _
                    Integer.Parse(DataGridView1.Rows(counter) _
                    .Cells("Deposits").Value.ToString())
            End If
        End If
    Next

    ' Iterate through the SelectedCells collection and sum up the values.
    For counter = 0 To (DataGridView1.SelectedCells.Count - 1)
        If DataGridView1.SelectedCells(counter).FormattedValueType Is _
        Type.GetType("System.String") Then

            Dim value As String = Nothing

            ' If the cell contains a value that has not been commited,
            ' use the modified value.
            If (DataGridView1.IsCurrentCellDirty = True) Then

                value = DataGridView1.SelectedCells(counter) _
                    .EditedFormattedValue.ToString()
            Else

                value = DataGridView1.SelectedCells(counter) _
                    .FormattedValue.ToString()
            End If

            If value IsNot Nothing Then

                ' Ignore cells in the Description column.
                If Not DataGridView1.SelectedCells(counter).ColumnIndex = _
                    DataGridView1.Columns("Description").Index Then

                    If Not value.Length = 0 Then
                        SelectedCellTotal += Integer.Parse(value)
                    End If
                End If
            End If
        End If

    Next

    ' 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()
End Sub

備註

如果 RowCount 設定為小於目前值的值,則會從集合結尾 Rows 移除資料列。 如果 RowCount 設定為大於目前值的值,則會將資料列新增至集合結尾 Rows 。 其他資料列是以 屬性中指定的 RowTemplate 資料列為基礎。

如果您將 RowCount 屬性設定為 0,所有資料列都會從 DataGridView 中移除。 這相當於呼叫 DataGridViewRowCollection.Clear 方法。

注意

如果 為 AllowUserToAddRowstrue ,則無法設定 RowCount 為 0。 在此情況下,請呼叫 DataGridViewRowCollection.Clear 方法,以移除新記錄的資料列以外的所有資料列。 在此案例中,呼叫 Clear 的結果與設定 RowCount 為 1 相同,但速度會更快。

屬性 RowCount 可以與 屬性搭配 ColumnCount 使用,以建立簡單的 DataGridView 來顯示和編輯文字。 如果您針對沒有資料行的 DataGridView 控制項將 RowCount 屬性設定為大於 0 的值, DataGridViewTextBoxColumn 則會自動加入 。

適用於

另請參閱