Click here to Skip to main content
15,886,110 members
Articles / Desktop Programming / Win32
Article

ItemData in .NET

Rate me:
Please Sign up or sign in to vote.
2.56/5 (9 votes)
10 Dec 2007CPOL 53.3K   203   16   5
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)


Written By
Software Developer (Senior)
India India
Around 6 Years of software engineering experience in Microsoft Technologies such as C#, C++,VB and Vbscript

Comments and Discussions

 
QuestionSo simple ... Pin
Peter Hayward7-Feb-12 18:48
Peter Hayward7-Feb-12 18:48 
GeneralThank you! I searched a long time to find this solution! Pin
davedoerr7-Apr-11 9:32
davedoerr7-Apr-11 9:32 
Generalshow of ball Pin
tallesrr@yahoo.com9-Sep-08 3:14
tallesrr@yahoo.com9-Sep-08 3:14 
GeneralWrite Article After Reading it Yourself Pin
Ri Qen-Sin11-Dec-07 1:23
Ri Qen-Sin11-Dec-07 1:23 
GeneralArticle Formatting Pin
Jeffrey Walton11-Dec-07 0:18
Jeffrey Walton11-Dec-07 0:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.