user863551
user863551

Reputation:

Pulling multiple value members from list box

I have a list box which the user can select multiple lines from, this list box basically contains a list of pets the user has, each pet has a reference, the pets name is displayed and each of the items value member is the reference of that pet. If the user has selected multiple pets within the list box, I would like to pull each of the value members so I can process them, so far this is the code I have:

        string lvCat;
        foreach (Object selectedCat in lstCustCats.SelectedItems)
        {
            lvCat = selectedCat as String;
            DataRow Cats = dSSystem.Bookings_Cats.NewRow();
            Cats["BookRef"] = lblBookingRefR.Text;
            Cats["CatRef"] = lvCat;
            dSSystem.Bookings_Cats.Rows.Add(Cats);
            this.bookings_CatsTableAdapter.Update(this.dSSystem.Bookings_Cats);
        }

This code fetches the first item within the list box for each selected item, but does not iterate through the list box, any idea on how I can accomplish this?

Thanks

Upvotes: 1

Views: 459

Answers (1)

No Idea For Name
No Idea For Name

Reputation: 11577

you are trying to do lvCat = selectedCat as String; which is wrong, because the items in lstCustCats.SelectedItems aren't strings. this should work

lvCat = selectedCat.ToString();

i'm flaging it as duplicated but it's damn close to this. after you'll run that code, if you got in the lvCat a class name, then that's the class selectedCat is type of. in that case you can cast selectedCat to that type and continue from there

Upvotes: 1

Related Questions