简体   繁体   中英

how to fetch wpf combobox selected value (key)

I want to find selected value of wpf combobox here is my code:

DataTable dt = new DataTable();
dt.Columns.Add("id", typeof(string));
dt.Columns.Add("name", typeof(string));
dt.Rows.Add("ks34dsd45g6g74g", "Amisha");
dt.Rows.Add("2ks34dsd45g6g74g", "Rhitik");
dt.Rows.Add("ks34dsd45g6g74g3", "Aishwarya");
dt.Rows.Add("4ks34ds545g6g74g", "Sonam");
dt.Rows.Add("ks55dsd45g6g74g5", "Aamana");

ddl_BusinessIDNames.DataContext = dt;
ddl_BusinessIDNames.DisplayMemberPath = dt.Columns[1].ToString();
ddl_BusinessIDNames.SelectedValuePath = dt.Columns[0].ToString();

that means at run time combo box show the readable text to user ie Amisha,Rhitik,Aishwarya etc but my question is if user select "sonam" how to fetch the key of "sonam" ie "4ks34ds545g6g74g" because Convert.ToString(ddl_BusinessIDNames.SelectedValue) this code fetch the display text of combo box but what if i want to fetch the key of value "sonam"

Please suggest...

private void ComboBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
{
    label1.Content = dt.Rows[Combobox1.SelectedIndex][0];
}

Combobox1.SelectedIndex - represents selected row index of your datatable

0 - column index in datatable with id, U want to get.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM