views:

421

answers:

3

Hi!

I'm writing a Database Editor / Bill of Materials Maker (2 separate .exe) for work, and I have this crazy issue. Here's how the flow works in the apps: Open Database, Search Database, Check Items Needed, Send to BOM Maker, Save as .xls.

So far, I can send checked Items to the BOM Maker, but only if I open the search window, check the items, without actually searching the List. Currently in the Search Form of the Database Editor, i have this loop:

for (int i = 0; i < rowCount; i++)
{
 if (ResultBox1.Items[i].Checked == true)
 {
  //Code that creates .txt file to be loaded by the BOM Maker...
 }
}

The loop works flawlessly, but only if i avoid using the search function. The Search Function does clear the ListView, and populate it with results, but why would that matter?

The error i get is:

InvalidArgument=Value of '22' is not valid for 'index'. Parameter name: index

'22' being the line I checked relative to the Array I use to populate the ListView from the start.

Unless i need to look into my Search Method, is there another way to perform this action? I'm useless at "foreach" loops, could anyone give me an opinion?

Thank you!

+1  A: 

It looks like the major problem is that you're getting your index range from your database results, but your ListView doesn't accurately reflect the database results you're using for your index range.

You've forgotten to update something somewhere when you do your search.

Probably the easiest way to handle the issue is to remove the dependency on the database results and depend only on the ListView Items list. For example:

var qry = from item in ResultBox1.Items where item.Checked select item;
foreach(var item in qry)
{
  // handle checked items individually. 
}
Randolpho
+1  A: 

Does this work?

foreach (ListViewItem item in ResultBox1.Items)
{
 if (item.Checked)
 {
  // Do somethign with it
 }
}
Neil Barnwell
I'm trying to use your code, but for the if statement, the ".Checked" option isn't there for the "item". Any idea how i can get it to come up?
Alex McCulloch
AHA!! i figured it out, instead of "var" i used "ListViewItems" instead, and it works like a charm now. THANKS!
Alex McCulloch
Ahh, sorry - it probably assumed `item` was of type Object. My bad - I'll update my answer.
Neil Barnwell
A: 
Partha Choudhury
Is SelectedItems the same as CheckedItems?
Roger Lipscombe
I believe it's not, I'd like to check a box on for item(s) to send it/them to the BOM Maker. Selecting the Row not as functional, IMO.
Alex McCulloch