Glory Raj
Glory Raj

Reputation: 17701

how to get the selected item id in list box using c# winforms

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...

Upvotes: 0

Views: 5981

Answers (4)

Ozkan Konca
Ozkan Konca

Reputation: 115

Create a property to use as ListBoxItem

class Group
    {
        private string _name;
        private string _id;

        public Group(string name, string id)
        {
            _name = name;
            _id = id;
        }

        public override string ToString()
        { return _name; }

        public string id
        {
            get { return _id; }
        }

        public string name
        {
            get { return _name; }
        }

    }

Add Item to Listbox

lbAvailableGroups.Items.Add(new Group(ds.Tables[0].Rows[i][1].ToString(), ds.Tables[0].Rows[i][0].ToString()));
}

Get the Item Id and Name

            Group p = (Group)lbUserGroups.SelectedItem;
            int GroupId = Convert.ToInt32(p.id);
            string GroupName = p.name

Upvotes: 0

Mitja Bonca
Mitja Bonca

Reputation: 4546

Maybe this will help:

  DataTable table = new DataTable();
    public Form1()
    {
        InitializeComponent();

        //you fill the table from database, I will show you my example (becuase I dont have dataBase)!
        table.Columns.Add("CategoryID", typeof(int));
        table.Columns.Add("CategoryName", typeof(string));
        table.Rows.Add(1, "name 1"); 
        table.Rows.Add(2, "name 2");
        table.Rows.Add(3, "name 3");

        listBox1.DataSource = new BindingSource(table, null);
        listBox1.DisplayMember = "CategoryName";
        listBox1.ValueMember = "CategoryID";
    }

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (listBox1.SelectedIndex > -1)
        {
            DataRowView data = listBox1.SelectedItem as DataRowView;
            int id = int.Parse(data["CategoryID"].ToString());
            string name = data["CategoryName"].ToString();
        }
    }

Upvotes: 0

David Anderson
David Anderson

Reputation: 13680

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
    };
}

Upvotes: 2

Jeroen
Jeroen

Reputation: 4023

Do something like this. when you get the selected listitem it will be of type Cat which holds the id and name of the category.

 public class Cat
    {
          public int Id { get;set;}
          public string Name { get;set;}

         public override string ToString()
         {
             return this.Name;
          }

    }

    private void getcategorynames()
         { 
            var categorytypes = (from categories in age.categories
                           select categories.category_Name).ToList();

listcategories.SelectedIndexChanged += new EventHandler(listcategories_SelectedIndexChanged);

            foreach (var c in categorytypes.select(p=> new Cat { Id = p.category_Id, Name = p.category_Name}))
            {

                listcategories.Items.Add(c);


            }

void listcategories_SelectedIndexChanged(object sender, EventArgs e)
        {
            Cat selected = (Cat)(sender as ListBox).SelectedItem;
        }

Upvotes: 0

Related Questions