Click here to Skip to main content
15,793,452 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
hello
i want to display id in combox and their value (means its name) in textbox
for that purpose i did bellow code..

C#
string Query = "select id, name from LateFee";
           cmd = new SqlCommand(Query, con);

               //con.Open();

               dr = cmd.ExecuteReader();
               DataTable dt = new DataTable();
               dt.Columns.Add("id", typeof(string));
               //dt.Columns.Add("name", typeof(string));
               dt.Load(dr);
               comboBox1.ValueMember = "id";
               comboBox1.DisplayMember = "id";

               comboBox1.DataSource = dt;
               binding = new Binding("Text", comboBox1, "Text");
               textBox1.DataBindings.Add(binding);


but what is happing that in combox when i select id,in text box id is only appear but i want in textbox name should appear.
what should i do???
Posted
Updated 7-Apr-13 3:13am
v2

1 solution

You need to change a few things,

1)
//dt.Columns.Add("name", typeof(string));
<< uncomment that out.
2)
comboBox1.ValueMember = "id";
should be
comboBox1.ValueMember = "name";


3)I think you need to change the binding from:
new Binding("Text", comboBox1, "Text");

to:
new Binding("Text", comboBox1, "SelectedValue");
 
Share this answer
 
v2
Comments
Arifa S 7-Apr-13 8:27am    
yes thanks now it is working properly ...

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