• 0

Remove items from a dataGridView issue.


Question

Hi everyone, a friend of mine is trying to remove items from a dataGridView that have a calculated value of less than a given number OR which are blank. The numbers are calculated based on the difference in days in a column and todays date and placed in an unbound column. He can't seem go figure out what the issue is, so a few days ago he asked me to look at it for him. Maybe it's me, but I just don't see anything wrong with the code... I thought maybe another set of eyes would maybe see something that we're missing... He's working in C# 2.0. If you need anything else, please let me know. Here is the applicable code:

// remove the rows that aren't "missing"

for (int i = RDGV1.Rows.Count - 1; i >= 0; i--)

{

if (RDGV1.Rows.Cells[10].Value != null ||

RDGV1.Rows.Cells[10].Value.ToString() != "" ||

double.Parse(RDGV1.Rows.Cells[10].Value.ToString()) >= 30)

{

RDGV1.Rows.RemoveAt(i);

}

}

If I change the code to remove only values that are not null or "", it works. If it tries to remove values that are not null, "", or less than 30 though, it has trouble. I do not get or receive any sort of error. But values under 30 are included. Breakpoints have not showed anything that showed me there was a problem... Code execution does however appear to end after this if statement, so... Does anyone happen to see where the issue may be? Any help would be greatly appreciated. I had hoped I would be able to help him... I'll give credit of course to whoever helps, I may even be able to then get him to sign up for Neowin... LOL

Thanks Again,

4 answers to this question

Recommended Posts

  • 0

I think you need to first determine if the value is present and check for >=30. In the code you offered the null checks will always pass and short-circuit the if statement, preventing it from evaluating the >= 30 check at the end.

Does this do what you're looking for?:

for (int i = RDGV1.Rows.Count - 1; i >= 0; i--)
{
   string val = RDGV1.Rows[i].Cells[10].Value != null ? RDGV1.Rows[i].Cells[10].Value.ToString() : "";
   double dbl;
   bool doRemove = false;
   if (double.TryParse(val, out dbl))
   {
	  doRemove = (dbl >= 30);
   }
   else
   {
	  doRemove = !string.IsNullOrEmpty(val);
   }

   if(doRemove)
   {
	  RDGV1.Rows.RemoveAt(i);
   }
}

  • 0

The other issue you may run into, is that you are removing items from the gridview while you are interating over its index in a forward direction. The problem with this is, as you move through and remove items, the number of items in the gridview decreases, so I'm surprised it hasn't thrown an Index out of bounds exception...

This probably won't occur if you iterate over the items in reverse order, as the indices of items that occur earlier in the list won't change as you remove them.

  • 0

Thank you very much Azymn, that did the trick. I just could not figure out what was wrong. I REALLY appreciate your help.

Thanks for your post also Antaris. I will look into your suggestion as well. I've never iterated through a dataGridView backwards, so I'm not sure how that would be done offhand, but I will definitely be looking into that. It does make a lot of sense.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.