DataGridView.RowCount 属性

定义

获取或设置 DataGridView 中显示的行数。Gets or sets the number of rows displayed in the DataGridView.

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

属性值

要在 DataGridView 中显示的行数。The number of rows to display in the DataGridView.

属性

异常

当设置此属性时,指定的值小于 0。The specified value when setting this property is less than 0.

-or- 指定的值小于 1,并且 AllowUserToAddRows 设置为 trueThe specified value is less than 1 and AllowUserToAddRows is set to true.

设置此属性的同时设置了 DataSource 属性。When setting this property, the DataSource property is set.

示例

下面的代码示例演示如何使用 RowCount 属性。The following code example demonstrates the use of the RowCount property. 在此示例中,此属性用于跟踪 @no__t 0 中的条目数。In this example, this property is used to track the number of entries in a DataGridView. 此示例是 SelectionChanged 事件中提供的更大示例的一部分。This example is part of a larger example available in the SelectionChanged event.

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 设置为小于当前值的值,则将从 @no__t 集合的末尾删除行。If RowCount is set to a value less than the current value, rows will be removed from the end of the Rows collection. 如果 RowCount 设置为大于当前值的值,则将行添加到 @no__t 集合的末尾。If RowCount is set to a value greater than the current value, rows will be added to the end of the Rows collection. 其他行将基于 RowTemplate 属性中指定的行。The additional rows are based on the row specified in the RowTemplate property.

如果将 @no__t 0 属性设置为0,则将从 @no__t 中删除所有行。If you set the RowCount property to 0, all rows will be removed from the DataGridView. 这等效于调用 @no__t 0 方法。This is equivalent to calling the DataGridViewRowCollection.Clear method.

备注

如果 AllowUserToAddRows @no__t 为-1,则不能将 @no__t 2 设置为0。If AllowUserToAddRows is true, you cannot set RowCount to 0. 在这种情况下,请调用 DataGridViewRowCollection.Clear 方法,删除新记录行以外的所有行。In this case, call the DataGridViewRowCollection.Clear method to remove all rows except the row for new records. 在这种情况下,调用 Clear 与设置 @no__t 的结果相同,但速度要快得多。Calling Clear has the same result as setting RowCount to 1 in this case, but is much faster.

@No__t-0 属性可以与 @no__t 属性一起使用,以创建简单的 DataGridView 用于显示和编辑文本。The RowCount property can be used with the ColumnCount property to create a simple DataGridView for displaying and editing text. 如果将 RowCount 属性设置为一个大于0的值(对于不包含列的 @no__t 1 控件),则会自动添加 @no__t。If you set the RowCount property to a value greater than 0 for a DataGridView control without columns, a DataGridViewTextBoxColumn is added automatically.

适用于

另请参阅