C# DataGridView RowCount 与 Rows.Count

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11612791/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 18:31:18  来源:igfitidea点击:

DataGridView RowCount vs Rows.Count

c#winformsdatagridview

提问by whytheq

If I have a DataGridView uxChargeBackDataGridView.

如果我有一个 DataGridView uxChargeBackDataGridView

Are the following syntactically different but effectively the same?:

以下在句法上不同但实际上相同吗?:

int numRows = uxChargeBackDataGridView.Rows.Count;
int numRowCount = uxChargeBackDataGridView.RowCount;

If uxChargeBackDataGridViewis empty then both are equal to 1; does it therefore logically stand that if either of these is equal to 1 I can assume the user has not input any data?

如果uxChargeBackDataGridView为空,则两者都等于 1;因此,从逻辑上讲,如果其中任何一个等于 1,我是否可以假设用户没有输入任何数据?

The WinFroms has a button RUN- could I use the above test to decide if this button is enabled or not i.e only enable the button when the number of rows is >1?

WinFroms 有一个按钮RUN- 我可以使用上面的测试来决定是否启用此按钮,即仅在number of rows is >1?

采纳答案by Holger Brandt

Both statements are the same. However, one thing to remember is that an "empty" datagridview has 1 record only if the AllowUsersToAddRowproperty is set to true. Otherwise, the row count will be 0.

两种说法都是一样的。但是,要记住的一件事是,只有当该AllowUsersToAddRow属性设置为 true 时,“空”datagridview 才会有 1 条记录。否则,行数将为 0。

EDIT: I would like to add, in lieu of MMK's answer that if you do not change the RowCount, either manually or programatically, they will return the same value. See http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rowcount.aspx.

编辑:我想补充一点,代替 MMK 的回答,如果您不手动或以编程方式更改 RowCount,它们将返回相同的值。请参阅http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rowcount.aspx

回答by MMK

RowCount gets or sets the number of rows displayed in the DataGridView.

Rows.Count returns the number of rows

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

Rows.Count 返回行数

回答by Niranjan Singh

Following statement are return same result but RowCount limits the number of rows displayed in the DataGridView..

以下语句返回相同的结果,但 RowCount 限制了 DataGridView 中显示的行数。

int numRows = uxChargeBackDataGridView.Rows.Count;
int numRowCount = uxChargeBackDataGridView.RowCount;

Check the note below on the DataGridView.RowCount Property

检查下面关于DataGridView.RowCount 属性的注释

If AllowUserToAddRows is true, you cannot set RowCount to 0. In this case, call the DataGridViewRowCollection.Clear method to remove all rows except the row for new records. Calling Clear has the same result as setting RowCount to 1 in this case, but is much faster.

如果 AllowUserToAddRows 为 true,则不能将 RowCount 设置为 0。在这种情况下,调用 DataGridViewRowCollection.Clear 方法删除除新记录行以外的所有行。在这种情况下,调用 Clear 的结果与将 RowCount 设置为 1 的结果相同,但速度要快得多。

If RowCountis set to a value less than the current value, rows will be removed from the end of the Rows collection. If RowCount is set to a value greater than the current value, rows will be added to the end of the Rows collection. The additional rows are based on the row specified in the RowTemplate property.

如果RowCount设置为小于当前值的值,则行将从 Rows 集合的末尾删除。如果 RowCount 设置为大于当前值的值,则行将添加到 Rows 集合的末尾。附加行基于 RowTemplate 属性中指定的行。

If it is true then a default empty row is considered. It Implements IBindingList interface. Ref: DataGridView - what does AllowUserToAddRows do?

如果为真,则考虑默认的空行。它实现了IBindingList 接口。参考:DataGridView - AllowUserToAddRows 做什么?

If the DataGridView is bound to data, the user is allowed to add rows if both this property and the data source's IBindingList.AllowNewproperty are set to true.

如果 DataGridView 绑定到数据,并且此属性和数据源的IBindingList.AllowNew属性都设置为 true ,则允许用户添加行。

回答by I-A-N

If AllowUserToAddRows is true at least one row will always be present in the grid for the new record. To exclude this row check for property IsNewRow on the row object.

如果 AllowUserToAddRows 为 true,则新记录的网格中将始终存在至少一行。要排除此行,请检查行对象上的 IsNewRow 属性。

回答by Computer User

If you decompile the assembly System.Windows.Forms.DataGridView, you will see:

如果反编译程序集System.Windows.Forms.DataGridView,您将看到:

public int RowCount
        {
            get
            {
                return this.Rows.Count;
            } 

So, RowCountreturns Rows.Count

所以,RowCount返回Rows.Count