Results 1 to 4 of 4

Thread: [RESOLVED] [2005] DataGridView CellValidated Event Issues

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    Resolved [RESOLVED] [2005] DataGridView CellValidated Event Issues

    I have a DataGridView bound to a DataSet on my form. When the user clicks on the Cancel button, I call DataSet.Clear and reset the various parts of the form to get it ready for new data input.

    The problem is that if the user is in the DGV when Cancel is clicked and DataSet.Clear is called, then the CellValidated event fires and gives an IndexOutOfRangeException.

    The following exception occurred in the DataGridView:
    System.IndexOutOfRangeException: Index 0 does not have a value.
    at System.Windows.Forms.CurrencyManager.get_Item(Int32 index)
    at System.Windows.Forms.DataGridView.DataGridViewDataConnection.GetError(Int32 rowindex)


    This is firing on the first line of the following block of code:

    Code:
            If Not String.IsNullOrEmpty(dgv.Rows(e.RowIndex).ErrorText.Trim) Then
                dgv.Rows(e.RowIndex).ErrorText = String.Empty
            End If
    I had this same thing happen in a previous app and I just created an IsResetting form-level boolean. On click of Cancel, I would set it to True and then any routines that gave problems, I would have them watch the IsResetting and if True, don't process the routine.

    I would rather not go the route of the IsResetting boolean. I am hoping that there is some way of looking at the control or the DataSet and knowing what state everything is in.


    EDIT:
    I have determined that I can access the data in the cells of the DGV. The error happens when I look at the ErrorText property for the DGV Row object.
    Last edited by Aspnot; Sep 24th, 2007 at 08:50 AM.
    My.Settings.Signature = String.Empty

  2. #2
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,290

    Re: [2005] DataGridView CellValidated Event Issues

    You can check the row count on your datatable or DGV, and if it's > 0 then you run the code.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    Re: [2005] DataGridView CellValidated Event Issues

    I actually did. RowCount = 2. I can look at all sorts of information about the DGV in break mode, but I cannot look at the ErrorText property.
    My.Settings.Signature = String.Empty

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    May 2003
    Posts
    758

    Re: [2005] DataGridView CellValidated Event Issues

    An update in case anyone else runs into this.

    I replaced:
    Code:
            If Not String.IsNullOrEmpty(dgv.Rows(e.RowIndex).ErrorText.Trim) Then
                dgv.Rows(e.RowIndex).ErrorText = String.Empty
            End If
    with
    Code:
            dgv.Rows(e.RowIndex).ErrorText = String.Empty
    and everything works.

    I tested various scenarios, and just not looking at ErrorText prior to clearing it was enough to stop the IndexOutOfRangeException.

    Weird, but now it works. I cannot imagine how many hours I spent nailing this one down.
    My.Settings.Signature = String.Empty

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