DataGridView.RowCount 属性

定义

获取或设置 DataGridView 中显示的行数。

C#
[System.ComponentModel.Browsable(false)]
public int RowCount { get; set; }

属性值

要在 DataGridView 中显示的行数。

属性

例外

当设置此属性时,指定的值小于 0。

- 或 -

指定的值小于 1,并且 AllowUserToAddRows 设置为 true

设置此属性的同时设置了 DataSource 属性。

示例

下面的代码示例演示如何使用 RowCount 属性。 在此示例中,此属性用于跟踪 中的 DataGridView条目数。 此示例是 事件中提供的更大示例的 SelectionChanged 一部分。

C#
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)
            {

                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();
}

注解

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

适用于

产品 版本
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅