Click here to Skip to main content
12,505,902 members (55,016 online)
Click here to Skip to main content
Add your own
alternative version

Stats

35.5K views
203 downloads
16 bookmarked
Posted

ItemData in .NET

, 10 Dec 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

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

Share

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

You may also be interested in...

Pro
Pro

Comments and Discussions

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

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