Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I want to be able to add a value to a combobox so that i can later store the character eg. M to my table in sql. i do not want the full text to be stored in my table example Male

any help plssss!!!
Posted

Here is sample code. please try this.


C#
class MyClass
        {
            public string Data { get; set; }
            public string Value { get; set; }
        }

        private void BindData()
        {
            ComboBox yourComboBox = new ComboBox();

            List<MyClass> obj = new List<MyClass>();
            for (int i = 0; i < 5; i++)
            {
                obj.Add(new MyClass() { Data = "Data" + i, Value = (i * 5).ToString() });
            }

            yourComboBox.DisplayMember = "Data";
            yourComboBox.ValueMember = "Value";
            yourComboBox.DataSource = obj;
        }


it is working fine i have tested...
if any confusion let me know.
 
Share this answer
 
Comments
Aiman Ali 23-Aug-14 3:22am    
Muhammed thanks alot. I managed a way to solve this issue at last.. I will test your code and if also works i will accept this solution.
MuhammadUSman1 23-Aug-14 4:08am    
Ok and You Welcome :)
 
Share this answer
 
Comments
Aiman Ali 21-Aug-14 10:01am    
i am able to add items to the combobox for example Male, Famale but i only want to store M for male and F for female in my table.

how do i do that???
MuhammadUSman1 22-Aug-14 3:11am    
Use substring and save first char into table.

http://msdn.microsoft.com/en-us/library/aka44szs%28v=vs.110%29.aspx
Aiman Ali 22-Aug-14 4:33am    
how come combobox does not contain a data value that i can add to specify that "M" is the data value that is going to be stored and Male is just plain text to show the user.. hope u getting me..
MuhammadUSman1 23-Aug-14 0:46am    
Please check my solution.
MuhammadUSman1 22-Aug-14 3:13am    
Which database you are using?
and please provide piece code...

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900