Click here to Skip to main content
15,889,266 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I have used the following code to set the display member and value member in Combobox. I tried to get the value member by accessing the Combobox.Text property. But it is only returning the Display member value. Is there any way to get the display member value?

What I have tried:

I have tried by using the following code,

Combobox.Items

But it is returning the value as DataRow.
Posted
Updated 2-Jun-16 0:30am
Comments
Zafar Sultan 2-Jun-16 6:17am    
Can you please post the relevant code? Combobox.Items does not give enough information about your problem.

1 solution

Looks like you are using a DataTable as the DataSource.
If so, you can do something like the following example.

Initialize the combo box:
C#
DataTable dtProviders = DbProviderFactories.GetFactoryClasses();
comboDataProviders.DataSource = dtProviders;
comboDataProviders.DisplayMember = "Name";
comboDataProviders.ValueMember = "InvariantName";


To access the selected item, you can do like this:
C#
private void comboDataProviders_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        DataRowView drv = (comboDataProviders.SelectedItem as DataRowView);
        if (drv != null)
        {
            DataRow dr = drv.Row;
            string dataProvider = dr["InvariantName"].ToString();
            switch (dataProvider)
            {
                case "MySql.Data.MySqlClient": Do somethinsg break;
                default: throw new Exception(string.Format("The Data Provider '{0}' is not supported.", dataProvider));
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}


IF you want a better answer, you need to post more information about your specific case.
 
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