DataGridView.CurrentRow.IsNewRow property is really, really confusing RRS feed

  • Question

  • I'm trying to figure out the best way to handle users adding new rows of data to a DataGridView. The moment a user enters data into an empty row on the DataGridView a new empty row is added to the DataGridView below where the user started typing. So, the "UserAddedRow" event handler occurs. Wonderful. The only problem is that the index of the new row is always 1 larger than the actual new row that the user is still editing. Okay, so I figure this out and use the DataGridView.CurrentRow property instead to populate the row with some default values. That's the actual row the user has just added. But does it's "IsNewRow" property equal true? Hehehe. Nope!

    So, I find myself scratching my head and asking myself these questions. What's the best events to use to know when to check and see if the event is in regards to a newly added row?

    There are so many events firing constantly on a DataGridView control that it's absolutely unthinkable that the developers offered no way to check the "IsNewRow" property in a meaningful way.

    I'm desperately looking for some good advice on how to use this control because it's very frustrating at the moment. Thank You for reading!

    EDIT: I could use a global static variable to save the newly added row index so that I can handle all the events related to the row but, wow, is this really the best way?

    Wednesday, November 6, 2019 12:08 AM

All replies

  • Hi,

    In advance, sorry if I misunderstand your situation.

    If you want Rows.Count to be the same as real/existing rows, do this:
    DataGridVeiw.AllowUserToAddRows = False 
    This make the last new row invisible and Rows.Count is the same number of real rows.

    But by this, users cannot add a new row. So, you need to think about a feature for adding a row.
    I always add a button in Form, and this enables users to add a new row.


    Ashidacchi --

    If you want to do this in Designer view, do like this:
    • Edited by Ashidacchi Wednesday, November 6, 2019 12:58 AM
    Wednesday, November 6, 2019 12:42 AM
  • Thank You for your input.

    I know an easy work around is to simply add a new row of data using anything but the DataGridView UI control and simply add a new row to the DataGridView once all the data is collected.

    My main question was trying to understand the logic behind the native functionality of the DataGridView itself. I questioned whether the control itself has inherent design flaws and if I have discovered one of them. In other words, they gave us a control that supposedly allows us to add new rows using the control itself. So, I wanted to do it their way using the control itself for the most part. But if it's 10x easier to add rows not using the UI of the DataGridView control itself, well, that doesn't represent the control in a good light.

    Edit: The only event handler for a DataGridView that makes any real sense is the 'UserDeletedRow' event handler. With regards to the actions of adding a new row or updating an existing row using the event handlers does not provide any good context for what's actually happening.

    So, at this point, I give up. It is not possible to use only the DataGridView control UI itself to add or update records without losing your marbles. It's just too much silly work. 

    If I am wrong still, somebody please step up and let me know. Thank You!

    Wednesday, November 6, 2019 1:24 AM
  • Hi,

    When a user enters data into an empty row, a new empty row will be added to the DataGridView below.

    The property DataGridViewRow.IsNewRow is used to check if the row is the last row in the DataGridView.

    For more information about DataGridViewRow.IsNewRow Property, you can refer to this document.

    [DataGridViewRow.IsNewRow Property].

    To achieve your requirement, there is a workround you can refer to.

    private void Button1_Click(object sender, EventArgs e)
                if (dataGridView1.Rows[dataGridView1.CurrentRow.Index + 1].IsNewRow==true)
                    //Edit CurrentRow

    Best Regards
    Daniel Zhang

    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact

    Wednesday, November 6, 2019 3:09 AM
  • Thank You for your reply. It seems the .IsNewRow property was never even intended to represent a new row of data as entered by an end-user. But instead represents an empty row that is never really usable.

    Using the word workaround sounds like your confirming my suspicions of 'design flaws' or shortcomings.

    It's not a big deal. I'm not saying it isn't a good control. I'm just trying to understand all the quirks and oddities and to me this is one of the bigger ones.

    Wednesday, November 6, 2019 3:41 AM
  • A DataGridView control works best when working from the DataSource to add, edit, delete and filter data. 

    Typically a developer will either use a DataTable as the DataSource of a DataGridView or a BindingSource component with its DataSource set to a DataTable then the BindingSource become the DataSource for the DataGridView which in this case one never needs to access rows or cells, instead access the BindingSource.

    For example to add a new record

    (DataTable) _someBindingSource.DataSource

    Provides access to the data in the DataGridView. Get the current row

    if ( _bindingSource.Current != null)
        var currentRow = ((DataRowView)_bindingSource.Current).Row;

    Add a new record


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    Wednesday, November 6, 2019 8:49 PM