Click here to Skip to main content
15,895,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi!

I am checking values between two datagridviews and if the value does not exist in one of them I need to insert to value. My code works fine but when I get to the value that does not exist I get an Index out of range error.

I know what the error means but I do not know how to handle it. I need to check if the index is out of range before executing my code.

Can someone please tell me how to check if the Index is out of range?
Posted
Comments
Arun Jacob 2-Aug-10 1:44am    
Can you post the snippet where you get error?

So, first, when people see you responding to a perfectly valid answer with stuff like, "useless, absolutely useless..." it definitely doesn't make anyone want to help you.

Secondly, yes, buy a book. Start with the basics of coding, because it's clear you haven't done that. From the question, I can only assume that you've done something like:

C#
int i = 0;

while (true)
{
  if (dataGridView1.Rows(i)...)
  {

  }
  i++;
}


which would not be the correct way to do it at all.

Pretty much anything that controls a list will have a Count property. This tells you...wait for it...the number of items in that list. So, how about:

C#
int i = 0;

while(i < dataGridView1.Rows.Count)
{
  //do something

  i++;
}


of course, that is the solution that you start with in a basic coding class. There are many, many other ways to do it and if you don't know them, then buy a book or find a class.
 
Share this answer
 
Comments
Yusuf 2-Aug-10 17:47pm    
I think he is using the same index in both datagridviews. Since he mentioned if it is not found, what it means is one gridview may have more items than the second. Just pure guess.
I suggest buying a basic programming book and reading it, and learning basic coding BEFORE ASP.NET. But, I see that's not likely to happen.

Check the number of objects in a collection to make sure the index you're about to request, exists. There's a Count property, typically.
 
Share this answer
 
Comments
Illegal Operation 1-Aug-10 23:32pm    
Useless. Absolutely useless...
Christian Graus 2-Aug-10 2:00am    
It's only useless if you are stupid. There is a Count property, and you check it before you access the variable. If the count is greater than the number you want to check, then the object does not exist, and you'll get the exception.
koool.kabeer 2-Aug-10 3:33am    
please try to avoid harsh words .......
we are all here as community......
be polite......
and please dont use "Useless" or "stupid" words......
but this answer is right.........
koool.kabeer 2-Aug-10 3:34am    
i would like to vote but my link to vote is not working.....
William Winner 2-Aug-10 13:32pm    
Reason for my vote of 5
"useless...absolutely useless..."

not you, but the person asking the question.

And what's with koool.kabeer?
To check if an index is out of range, you could use an if-statement to check the current index you are trying to access against the "Count" property of the collection you are enumerating. An array uses the "Length" property rather than "Count". You could also catch the exception thrown when you attempt to access an index that is out of range.

However, why you should probably be asking yourself why you are attempting to access an index that is out of range. If you are using a for-loop, you should be using 0 as the lower bound and Count - 1 as the upper bound (in the case of an array, that would be Length - 1). If you are using a while-loop, your condition for exiting should include the possibility that you may not find what you are looking for and should exit the loop before you exceed the possible range of indexes.

This is a basic concept and the reason CG suggested you get a book. His answer was fine, but it seems you didn't understand it. Perhaps if you post some code we can help you more specifically, but until then you'll just get non-specific answers. FYI, here is an example of how to cause an IndexOutOfRangeException.
C#
String[] strings = new String[1];
strings[1] = "something"; // IndexOutOfRangeException.

The fix should be obvious (i.e., don't access index 1, as index 0 is the only index you can access on the example array).
 
Share this answer
 
Your question is not complete. Are you expecting both gridview to have the items sorted in some fashion, so, each index should have the same value? Or are you checking for the whole second gridview for a given value?

If both have the same items (except the missing ones) in the same order then check the values for each given index and if they don't match then you need to insert it.

If not, you would need to search for the value in the whole gridview, for that you need to use the Count property to loop through the content of the gridview.
 
Share this answer
 
Try this -

MIDL
void myMethod()
{
    try
    {
    }
    catch (IndexOutOfRangeException Ex)
    {
    }
    catch (Exception Ex)
    {
    }
}
 
Share this answer
 
Comments
Christian Graus 2-Aug-10 2:01am    
That doesn't check if the index is out of range, it just throws an exception, which is expensive, and a waste of time.
koool.kabeer 2-Aug-10 3:32am    
what you are answering .......
that does not check ....
that catches the exception....
Toli Cuturicu 2-Aug-10 3:44am    
Reason for my vote of 1
does not answer the question

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