简体   繁体   English

DataGridView RowCount 与 Rows.Count

[英]DataGridView RowCount vs Rows.Count

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 uxChargeBackDataGridView is empty then both are equal to 1;如果uxChargeBackDataGridView为空,则两者都等于 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?因此,如果其中任何一个等于 1,我可以假设用户没有输入任何数据,这在逻辑上是否成立?

My WinForms application has a button named RUN - could I use the above test to decide if this button is enabled or not ie only enable the button when the number of rows is > 1 ?我的 WinForms 应用程序有一个名为RUN的按钮 - 我可以使用上面的测试来决定是否启用此按钮,即仅number of rows is > 1时启用该按钮?

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

Rows.Count returns the number of rows Rows.Count 返回行数

Both statements are the same.两种说法都是一样的。 However, one thing to remember is that an "empty" datagridview has 1 record only if the AllowUsersToAddRow property is set to true.但是,要记住的一件事是“空”数据网格视图只有在AllowUsersToAddRow属性设置为 true 时才有 1 条记录。 Otherwise, the row count will be 0.否则,行数将为 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.编辑:我想补充一点,代替 MMK 的回答,如果您不手动或以编程方式更改 RowCount,它们将返回相同的值。 See http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rowcount.aspx .请参阅http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.rowcount.aspx

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, RowCount returns Rows.Count因此, RowCount返回Rows.Count

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.如果 AllowUserToAddRows 为 true,则不能将 RowCount 设置为 0。在这种情况下,请调用 DataGridViewRowCollection.Clear 方法删除除新记录行之外的所有行。 Calling Clear has the same result as setting RowCount to 1 in this case, but is much faster.在这种情况下,调用 Clear 与将 RowCount 设置为 1 的结果相同,但要快得多。

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

If it is true then a default empty row is considered.如果为真,则考虑默认空行。 It Implements IBindingList interface .它实现了 IBindingList 接口 Ref: DataGridView - what does AllowUserToAddRows do?参考: 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.AllowNew property are set to true.如果 DataGridView 绑定到数据,如果此属性和数据源的IBindingList.AllowNew属性都设置为 true,则允许用户添加行。

If AllowUserToAddRows is true at least one row will always be present in the grid for the new record.如果 AllowUserToAddRows 为真,则至少一行将始终出现在新记录的网格中。 To exclude this row check for property IsNewRow on the row object.要排除此行,请检查行对象上的属性 IsNewRow。

IsNewRow probably can solve mystery. IsNewRow 可能可以解开谜团。 It can give the meaning of 1. If IsNewRow is true created but there is nothing in it and not registered.它可以给出 1 的含义。如果 IsNewRow 为 true,则已创建但其中没有任何内容且未注册。 If IsNewRow false you have got 1 row filled full or partially.如果 IsNewRow 为 false,则您已将 1 行填满或部分填满。 I hope this is right for you.我希望这适合你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM