DataGridComboBoxColumn not displaying initial values.

Dec 1, 2008 at 5:43 PM
Edited Dec 1, 2008 at 5:54 PM
I'm in desperate need of some help. I am trying to use the new WPF data grid to display some records from a database table. Firstly I added the DataSet to the DataContext of the Grid and set the ItemsSource path. Set Auto create columns to true and result we have a populated DataGrid, so far so good. So lets try and expand this a little further I've added a DataGridComboBoxColumn. The ItemsSource of the combobox column is set to a lookup table from the same dataset as the DataGrid. I've set the DisplayMemberPath, SelectedValuePath and SelectedValueBinding properties. When the Grid is now displayed the combobox fields are initialy empty. If I select a field to edit the ComboBox is populated correctly and I can select a value. Then the selected value is dispayed in the combobox field. Also I have added the same field as Tex column and I can see that the value selected from the combo box is accepted. How do I get the combobox to display the initial values from the data table.
I'm obviously missing something quite straight forward and will feel suitably sheepish when the answer is pointed out to me.

Code snippets follow

This is the XAML for the DataGrid


dg:DataGrid Name="dgIndex" ItemsSource="{Binding Path=Index}" > 
<dg:DataGridTextColumn Header="Name" Binding="{Binding Path=Name}" /> 
<dg:DataGridComboBoxColumn x:Name="indexProductCol" DisplayMemberPath="Product" Header="Product" SelectedValueBinding="{Binding Path=Product}" SelectedValuePath="Product" /> 




This is C# code that initialises the DataSet and assigns the DataContext and ItemsSource

priceStagingDS = new PriceStaging(); 

indexTableAdapter =
new PACT_Config.PriceStagingTableAdapters.IndexTableAdapter(); 

productTableAdapter =
new PACT_Config.PriceStagingTableAdapters.ProductTableAdapter(); 

this.indexProductCol.ItemsSource = priceStagingDS.Product.Rows; 
dgIndex.DataContext = priceStagingDS;

Any help greatly appreciated.




Dec 2, 2008 at 10:24 AM
OK after doing some more digging I noticed a couple of solutions using the IValueConverter interface and Combo boxes. I created a converter and plugged it in to see what was being delivered t and from the cells. It seems it was a problem with the way strings weere being dealt with. The field in question is a nvar(50). The value stored in the table is trimmed so for example power is stored as "Power" the values in the look up table did not appear to be trimmed e.g "Power     ". I write a value converter to trim the field and every thing is now working, see below. After some further digging I found out that the lookup table had original been coded as char(10) and then the field was altered after the values were entered. The data in the table was fixed and the logic worked without the data converter. So in summary the combo box column works exactly as I would expect it too. Remeber to check the data you are using before you drive yourself round the bend. At least I learnt how to use a IValueConvertor. And I was right I said I'd feel sheepish when the answer was found.


ProductConverter : IValueConverter

PriceStaging.ProductDataTable products;

ProductConverter(PriceStaging.ProductDataTable products)
this.products = products;

IValueConverter Members

object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    object result = null;
    foreach (System.Data.DataRow row in products.Rows)
        PriceStaging.ProductRow prow = row as PriceStaging.ProductRow;
        if (prow != null)
            if (prow["Product"].ToString().Trim() == value.ToString())
                result = prow[




public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    return value.ToString().Trim();