Click here to Skip to main content
15,565,403 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am relatively new to coding. I am facing a problem that when I clear all rows of an unbound datagridview with AllowUserstoAddRows true, I get ArgumentOutOfRangeException for e.rowindex on clicking the datagridview. Any suggestion on how to clear the datagridview after save?


I do not want to disable AllowUserstoAddRows

What I have tried:

I tried to search solution in different threads here but could not come across except for some suggestions to disable AllowUserstoAddRows
Updated 3-Oct-21 0:48am
Richard MacCutchan 3-Oct-21 4:20am    
You need to show the code where the error occurs. Please use the Improve question link above, and add complete details of what is not working.
RickZeeland 3-Oct-21 5:21am    
If you start with a new datagridview you will see that the problem is not in AllowUserstoAddRows, but probably in one of your routines. You can use a Try..Catch to catch errors, and you should check before getting values from the datagridview if the values are valid.

1 solution

Start by looking at the CellClick event handler with the debugger and see exactly what it is doing. In all probability, it doesn't check to see if the DGV contains any data but just continues as if it did - and throws an exception when the row and / or cell indexing fails.

To fix that, you'd have to check the sizes - but since we have no idea what your code is doing, we can't help you fix it!

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900