checked item

  • Thread starter Thread starter Hrvoje Voda
  • Start date Start date
H

Hrvoje Voda

I fill a checked listBox with data from dataset.

I would like, when a user checked one item , to save it in dataset.

The problem is that I don't know how to get its ID number, because I have
only a name ?

Hrcko
 
Hrvoje Voda said:
I fill a checked listBox with data from dataset.
I would like, when a user checked one item , to save it in dataset.
The problem is that I don't know how to get its ID number, because I have
only a name ?

When you add items to the listbox, such as

lb.Items.Add(Name)


Instead of adding a string, create a small class that holds a name and an ID
and add the object instead:

class LbItem {
string _Name;
int _ID;

public LbItem(string Name, int ID) {
_Name = Name;
_ID = ID;
}

public override string ToString() {
return _Name;
}
}

To add the item to the listbox:
lb.Items.Add(new LbItem(Name, ID));


To get the checked items back out:

foreach (LbItem Item in lb.CheckedItems) {
int ID = Item.ID;
// etc.
}

This may have a few typos here since typing in by hand, not pasting...

-- Alan
 
Back
Top