14

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 ?

6 Answers 6

18

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

Rows.Count returns the number of rows

4
  • 1
    It is short, yet not to the point at all. Simply copying after manual is a wrong idea, and you can see here why. Simply check DisplayedRowCount method and you'll get it why. Also, as someone already wrote, those 2 properties are exactly the same, as first uses the second one to get the value. Commented Jul 26, 2018 at 8:23
  • I've read it. And your answer is just a copy of MSDN, where you can find some a bit misleading info. Both properties return the same (while your answer suggest something different) as one refers to another. If you'd actually want to get "number of rows displayed" you have to use DisplayedRowCount, but of course, that was not the question here. So in the end, I don't know why a misleading answer, one that actually is not answer at all (just a copy from MSDN, without any direct answer), received most positive points. Commented Jul 27, 2018 at 12:38
  • @SoulReaver you seems to be very angry mate, relax you don't like the answer vote it down find some good answer post it here.
    – MMK
    Commented Jul 27, 2018 at 14:16
  • I am not angry. It's just that my comments are plain text without emoticons, which I usually use in other typical forums. I've only meant to write, that this answer, while being very popular (what can make other people think it is actually better than the chosen answer) it is incorrect. It contains no direct answer and as I've already wrote, MS documentation is not free of bugs/mistakes. So if you do direct copy from there, you should actually check if what you copy is correct, before you (re-)publish it. In short RowCount does not (in most cases) return number of rows displayed in DGV. Commented Jul 30, 2018 at 5:53
12

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.

1
  • the whole point of this form is to accept a variable amount of input from the user (not something you could know from the OP) so I suspect AllowUsersToAddRow = false isn't really a possibility
    – whytheq
    Commented Jul 23, 2012 at 13:07
7

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

1

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.

1

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.

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.