I have list box(lstcategories) .. filled with items coming from database by using the below code ...
private void getcategorynames()
{
var categorytypes = (from categories in age.categories
select categories.category_Name).ToList();
foreach (string item in categorytypes)
{
listcategories.Items.Add(item);
}
my problem is if i click on the item in list box i need to do something.. like that
if i click on the category name(list box item) i need to pass the selected category name to the database
can any one pls help on this...
ListBox.Items is a collection of objects, so you can store the category object itself instead of the string representation of it.
age.Categories.ToList().ForEach((c) => listcategories.Items.Add(c));
Then in ListBox.SelectedIndexChanged
Category category = (Category)listcategories.SelectedItem;
// Do something with category.Id
If you want to do it all inline
private void getcategorynames() {
age.Categories.ToList().ForEach((c) => listcategories.Items.Add(c));
listcategories.SelectedIndexChanged += (sender, e) => {
Category category = (Category)listcategories.SelectedItem;
// Do something with category.Id
};
}