Click here to Skip to main content
Click here to Skip to main content

ItemData in .NET

By , 10 Dec 2007
Rate this:
Please Sign up or sign in to vote.

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:

#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)

About the Author

S.Raja
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 ... PinmemberPeter Hayward7-Feb-12 18:48 
GeneralThank you! I searched a long time to find this solution! Pinmemberdavedoerr7-Apr-11 9:32 
Generalshow of ball Pinmembertallesrr@yahoo.com9-Sep-08 3:14 
GeneralWrite Article After Reading it Yourself PinmemberRi Qen-Sin11-Dec-07 1:23 
GeneralArticle Formatting PinmemberJeffrey Walton11-Dec-07 0:18 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 11 Dec 2007
Article Copyright 2007 by S.Raja
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid