Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / Win32

ItemData in .NET

2.56/5 (9 votes)
10 Dec 2007CPOL 1   203  
Combo box and List Box Item Data in .NET

ItemData in .NET , In visual studio 6.0 you have ItemData to bind additional value to combo box and list box ,in .Net we don't have that then how to do that ?

Visual studio 6.0 : Returns/sets a specific number for each item in a ComboBox or ListBox control.

Using the code

Are you looking for Item Data property in .NET to store additional information (values) with the value in Combo Box or List Box? This property is not available in .NET then how to do that? Here .Net has it magic, every thing is treated as object in .NET Combo box or List Box add method takes the parameter as object so we can create our own custom class or structure and bind to the control which can hold any number of items.

Still not clear then let’s see the code:

C#
#region Itemdata

    public struct ItemData
    {
        public int EmpID;
        public string EmpName;
        public double Salary;

        public ItemData(int _EmpID, string _EmpName, double _Salary)
        {
            EmpID = _EmpID;
            EmpName = _EmpName;
            Salary = _Salary;
        }

        public override string ToString()
        {
            return this.EmpName;
        }
    }
    #endregion


//Load the values into Combo Box and List Box with the help of 
//Custom structure

private void frmItemData_Load(object sender, EventArgs e)
        {
            listBoxEmp.Items.Clear();
            listBoxEmp.Items.Add(new ItemData(10, "Raja", 20000));
            listBoxEmp.Items.Add(new ItemData(20, "Sekar", 40000));
            listBoxEmp.Items.Add(new ItemData(30, "kumar", 60000));
            comboEmp.Items.Clear();
            comboEmp.Items.Add(new ItemData(10,"Raja",20000));
            comboEmp.Items.Add(new ItemData(20, "Sekar", 40000));
            comboEmp.Items.Add(new ItemData(30, "kumar", 60000));
        }

//Now the Value is loaded into List box and Combo box How do you access it //while selecting the item see the below code

//When select the values in Combo box Messagebox will show the Itemdata
private void comboEmp_SelectedIndexChanged(object sender, EventArgs e)
        {
            MessageBox.Show("Emp Name: " + ((ItemData)comboEmp.SelectedItem).EmpName + "  Emp ID: " + ((ItemData)comboEmp.SelectedItem).EmpID.ToString() + "  Salary:" + ((ItemData)comboEmp.SelectedItem).Salary.ToString());
        }

//When select the values in ListBox Messagebox will show the Itemdata


        private void listBoxEmp_SelectedIndexChanged(object sender, EventArgs e)
        {

            MessageBox.Show("Emp Name: " + ((ItemData)listBoxEmp.SelectedItem).EmpName + "  Emp ID: " + ((ItemData)listBoxEmp.SelectedItem).EmpID.ToString() + "  Salary:" + ((ItemData)listBoxEmp.SelectedItem).Salary.ToString());
        }

This is an initial article about itemdata in .Net I will keep updating this thread.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)