Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Data Binding / August 2007

Tip: Looking for answers? Try searching our database.

DataGridView RowValidating IndexOutOfRangeException

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
imran.a - 02 Aug 2007 14:46 GMT
Hi all,
I am having a problem with inserting rows in to my datagridview control. The
datagridview is bound to a bindingsource with a filter set. If i try and add
a row to the datagridview while the filter is set i get an
IndexOutOfRangeException specifying that the index of the row i have just
added does not exist.

The exception occurs when i try and access the datagridview.currentrow
property in my rowvalidating event handler, or alternatively
datagridview.rows(e.rowindex).

       void ValidateRow(object sender, DataGridViewCellCancelEventArgs e)
       {
           DataGridView tmpDGV = ((DataGridView)sender);
           try
           {
               if (tmpDGV.IsCurrentRowDirty && tmpDGV.CurrentRow != null)
               {
                   if (Properties.Settings.Default.validation &&
RowHasValiadtionErrors(tmpDGV.CurrentRow))
                   {
                       MessageBox.Show("invalid format.", "Segment Format
Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                       e.Cancel = true;
                   }
                   else
                   {
                       PersistCellErrors(tmpDGV.CurrentRow);
                   }
               }
               tmpDGV.InvalidateRow(e.RowIndex);
           }
           catch (IndexOutOfRangeException ex)
           {
               //filtered row addition will throw this
               tmpDGV.InvalidateRow(e.RowIndex);
           }

If i debug this code and step through it i can see that when i have finished
editing the new record and hit enter the row validating event handler fires.
This works fine at first. The row validating event handler then fires a
second time and this is when the exception is thrown. I can catch the
exception and it would seem that the row is successfully added to the
datagridview however I'd like to know what it is that is causing this
behavior.

Any help would be greatly appreciated as I am stumped on this one!

Thanks.

Imran
imran.a - 02 Aug 2007 14:54 GMT
I forgot to mention one thing.

I have also managed to narrow down the conditions that cause the error and
it seems to surface if i have a filter set for a particular column value. For
example if I have a filter set to name = bob and attempt to add a row but
specify name = bill i will get the IndexOutOfRangeException.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.