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则会自动添加 。

适用于

另请参阅