Results 1 to 5 of 5

Thread: How To Determine If New Row is Selected in DataGridView

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Location
    Woodbridge, VA
    Posts
    9

    How To Determine If New Row is Selected in DataGridView

    This should be simple, but I can't seem to figure it out. How do I determine if the user has moved to the new record row of a DataGridView in VB 2010? I'm trying to do something like this:

    If NewRecordRow entered then
    Do something
    Else
    Do something else
    EndIf

    I assume that code such as the above could be put in the DataGridView_SelectionChanged event and it would execute whenever the user navigated to a dirrerent row.

    Am I close?

    Thanks.

    Dave

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How To Determine If New Row is Selected in DataGridView

    vb Code:
    1. If myDataGridView.CurrentRow.IsNewRow Then
    The problem with using the SelectIonChanged event is that it is raised whenever a cell is selected or unselected, i.e. highlighted, which can occur without the CurrentRow changing. CurrentCellChanged also gets raised when changing cells within the same row. Either of these events may be OK, as long as you don't mind the code being executed on some occasions where the user may not have changed the CurrentRow.

    If your grid is bound, you should do so via a BindingSource, in which case you can handle the CurrentChanged event of the BindingSource.

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Location
    Woodbridge, VA
    Posts
    9

    Re: How To Determine If New Row is Selected in DataGridView

    1. Thanks for the tip about the BindingSource CurrentChanged event. I did not realize that the DataGridView SelectionChanged event fired when you changed cells also.

    2. As you suggested, I tried:

    If myDataGridView.CurrentRow.IsNewRow Then

    but I immediately get this error:

    System.NullReferenceException was unhandled by user code

    Can you shed any light on why?

    Thanks.
    Dave

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: How To Determine If New Row is Selected in DataGridView

    If you get a NullReferenceException then that means that something that precedes a dot is Nothing. On that line, that can only be 'myDataGridView' or 'myDataGridView.CurrentRow'.

    If it's the former then that would only happen if it was in an event handler that was executed during the initialisation of the form and before the DataGridView is created. That could be the case and if so, you must write some conditional code to ensure that you don't try to use the control before it exists.

    If it's the latter then that would indicate that there is no row current in the grid. Again, if that's the case, you need to write some conditional code, this time to check for Nothing first.

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2010
    Location
    Woodbridge, VA
    Posts
    9

    Re: How To Determine If New Row is Selected in DataGridView

    Thanks for the quick reply.

    I did some quick coding and it appears the problem is/was that the .CurrentRow was causing the problem. I included this line before the main body of the code

    If TblPrescriptionsDetailsDataGridView.CurrentRow IsNot Nothing Then

    and I no longer get the exception. But, my main code seems to be functioning slightly differently now. I need to relook at the logic.

    Thanks again. I may be back with additional questions, if you don't mind.

    VB 2010 is powerful, but tricky.

    Dave

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width