I have a WPF DataGrid where a column, actually a DataGridComboBoxColumn, is bound to a List, the binding works just fine. The problem is when I choose an element from the ComboBox’s list it fills the field, but it becomes blank once I leave it, here is my code :
XAML :
<DataGrid Name="dgPrdCmd" AutoGenerateColumns="False" CanUserAddRows="True"CanUserDeleteRows="True">
<DataGrid.ItemsSource>
<collections:ArrayList>
<local:TestData></local:TestData>
</collections:ArrayList>
</DataGrid.ItemsSource>
<DataGrid.Columns>
<DataGridComboBoxColumn Header="Product Name" x:Name="cb_NomPrdCmd" IsReadOnly="False"/>
<DataGridTextColumn Header="Quantity"/>
<DataGridTextColumn Header="Unit Price"/>
</DataGrid.Columns>
</DataGrid>
and here’s my C# :
using (_entieties)
{
IQueryable<string> pName = from Product in _entieties.Products
select Product.prdName;
foreach (var name in pName)
{
prd.Add(id); //prd is just a list of strings
}
}
//here is the most important part
cb_NomPrdCmd.ItemsSource = prd;
thanx in advance
You are not binding the
SelectedValueorSelectedItemof the ComboBox to anything, so no item will get selected.Set either the
SelectedItemBindingorSelectedValueBindingon yourDataGridComboBoxColumnto maintain the selected item