Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGridView RowCount vs Rows.Count

If I have a 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; does it therefore logically stand that if either of these is equal to 1 I can assume the user has not input any data?

My WinForms application has a button named 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 ?

like image 411
whytheq Avatar asked Jul 23 '12 12:07

whytheq


People also ask

How to Count the total number of rows in a DataGridView using c#?

Use Property of RowCount to get the number of rows in a Data Grid View.

What is counted by row count?

From a software perspective, row count is correct. As a system variable (see transact sql @@rowcount or set rowcount), the phrase "row count" is commonly understood to mean the count of the rows in the result set. This is convention.

How can I count rows in Datagridview in VB net?

Text = DataGridView1. RowCount it display the number of row populated with some data.


6 Answers

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

Rows.Count returns the number of rows

like image 135
MMK Avatar answered Sep 19 '22 01:09

MMK


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. Otherwise, the row count will be 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.

like image 21
Holger Brandt Avatar answered Sep 20 '22 01:09

Holger Brandt


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

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

So, RowCount returns Rows.Count

like image 38
Computer User Avatar answered Sep 21 '22 01:09

Computer User


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

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

Check the note below on the DataGridView.RowCount Property

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.

If RowCount is 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.

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

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.

like image 31
Niranjan Singh Avatar answered Sep 20 '22 01:09

Niranjan Singh


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.

like image 43
I-A-N Avatar answered Sep 21 '22 01:09

I-A-N


IsNewRow probably can solve mystery. It can give the meaning of 1. If IsNewRow is true created but there is nothing in it and not registered. If IsNewRow false you have got 1 row filled full or partially. I hope this is right for you.

like image 31
nonexist Avatar answered Sep 19 '22 01:09

nonexist