Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am selecting 'id' from database using combo-box..now how can i retrieve corresponding 'name' of id in text-box in C# win form application after clicking on 'id'from combo box
Posted
Comments
Member 10984793 21-Oct-14 5:21am    
please help...
Mathew Soji 21-Oct-14 5:32am    
Not clear with what you mean by retrieve corresponding 'name' of id in text-box . Please be more clear.
Rajeev Jayaram 21-Oct-14 6:04am    
Use the SelectedValue and SelectedIndex property. See my answer below.

In the Event "ComboBox1_SelectedValueChanged", Use the SelectedValue and SelectedIndex property,

C#
if(ComboBox1.SelectedIndex == id)
{
    textBox1.Text = ComboBox1.SelectedValue.ToString();
}
 
Share this answer
 
v2
You can try the following approach assuming you have loaded the data from the SQL query into a DataTable.
(If not see Populating a DataSet from a DataAdapter[^])

C#
// Dummy table created as an example
DataTable dt = new DataTable();
dt.Columns.Add("Id", typeof(int));
dt.Columns.Add("Name", typeof(string));

dt.Rows.Add(1, "Donald Duck");
dt.Rows.Add(2, "Donald Trump");
dt.Rows.Add(3, "Duffy Duck");

comboBox1.DataSource = dt;
comboBox1.ValueMember = "Id";
comboBox1.DisplayMember = "Name";


This way the Name will be shown in the combo box, and the Id is accessible as well.

For example:
C#
void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    DataRowView drv = (comboBox1.SelectedItem as DataRowView);
    if (drv != null)
    {
        DataRow dr = drv.Row;
        int id = (int)dr["Id"];
        string name = dr["Name"].ToString();
        // If you really want to use a text box, then set 
        textBox1.Text = dr["Name"].ToString();    }
}
 
Share this answer
 
v2

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