John Edwards
John Edwards

Reputation: 1546

Datagrid ComboBox Binding

I have a table in my database that represents a list of areas. I would like to get all of these items in this list and bind them to a combobox dropdown in my datagrid. This currently works, and my grid column displays this dropdown. However, when I select an item in the cell and move to the next row, the cell becomes blank. Also, if the entry in the database has a saved value of "area1", and my dropdown list has "area1,area2,area3,etc", when the grid loads, it doesn't autoselect area1, the cell is simply blank.

In my model:

comboboxColumn1.ItemsSource = ctx.AREAS;//db context loading all areas into combobox

In my xaml:

 <DataGrid.Columns>

            <DataGridComboBoxColumn DisplayMemberPath="Name" Header="some header" x:Name="comboboxColumn1" SelectedValueBinding="{Binding Name}" />
....

How would I go about setting this selected value so that it is equal to whatever entry is in the db?

Upvotes: 1

Views: 579

Answers (1)

Bob.
Bob.

Reputation: 4002

You need to set the SelectedValuePath value to your variable property. The DisplayMemberPath is for what is shown, and SelectedValuePath is what is selected. SelectedItem is the actual item that is selected.

Upvotes: 1

Related Questions