Surya
Surya

Reputation: 127

Set Combobox Item from C# using Value in WPF

I have combobox in my XAML window,I have one datatable which contains two columns ID,Name. I bind that datable in to combobox items using below code.

cBox.SelectedValuePath="ID";
cBox.DisplayMemberPath="Name";
cBox.ItemsSource = dtable.DefaultView;

What I want is, I want to select particular item from c# using ID (not Name), then it will select 1st element of the combobox like below

cBox.SelectedIndex = 0/1/2/3/...;

If I use below code, it will select any item in the combobox but I want to select combobox item using ID

 cBox.Text = dtable.Rows[1][1].ToString();

Upvotes: 0

Views: 851

Answers (1)

Deepak Sharma
Deepak Sharma

Reputation: 4170

you have to use the selectedValue property..

try the below

cBox.SelectedValue = dtable.Rows[1][0].ToString();

you can download the working sample from here..

https://drive.google.com/uc?export=download&id=0Bxxluya0NKB2dGZOMjc0SlRJVWc

Upvotes: 1

Related Questions