DataGridComboBox not retaining values when I click on another cell

Oct 2, 2008 at 8:38 PM
Edited Oct 2, 2008 at 8:39 PM
I have a DataGrid displaying different columns amongst which one of them is a combobox grid control. The combo box shows all the data but as soon as I switch to another cell the field doesn't retain the selected data. How should i get around this issue, I am using the following code:

<Grid.Resources>
            <ObjectDataProvider x:Key="PerilData" ObjectType="{x:Type local:Peril}" MethodName="GetPeril"/>
            <local:PerilConverter x:Key="PerilConverter"/>
        </Grid.Resources>

<dg:DataGridComboBoxColumn DataFieldBinding="{Binding CODE,
                                                            Converter={StaticResource PerilConverter},
                                                            ConverterParameter=DESCRIPT}"                                           
                                           ItemsSource="{Binding Source={StaticResource PerilData}}"
                                           Header ="Sub Perils"
                                           DataFieldTarget="Text">
                    <dg:DataGridComboBoxColumn.EditingElementStyle>
                        <Style TargetType="ComboBox">
                            <Setter Property="SelectedValuePath" Value="CODE" />
                            <Setter Property="DisplayMemberPath" Value="DESCRIPT" />
                        </Style>
                    </dg:DataGridComboBoxColumn.EditingElementStyle>
</dg:DataGridComboBoxColumn>
Oct 3, 2008 at 6:08 PM
Any reply would be much appreciable!
Coordinator
Oct 6, 2008 at 2:58 PM
It's hard to tell from just this code snippet.  It is very similar to the combobox samples on this post, http://blogs.msdn.com/vinsibal/archive/2008/08/28/wpf-datagrid-working-with-datagridcomboboxcolumns-part-2.aspx, however the updated to the selected data works in those samples.  It could possibly be in your converter or the way your data source (PerilData) is setup.  Try to diff it with the sample solution on that post.  If you are still having problems please post the code for your converter and PerilData.
Oct 6, 2008 at 6:33 PM
Edited Oct 6, 2008 at 6:34 PM
Here's the code for my converter:

namespace WPF_Data_Grid_Combo_Box
{
    public class PerilConverter : IValueConverter
    {
        #region IValueConverter Members

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string perilID = (string)value;
            DataTable dataTable = DataReader.GetPeril().Tables["Peril"];

            foreach (DataRow row in dataTable.Rows)
            {
                if (row["CODE"].ToString() == perilID)
                {
                    return row[parameter.ToString()];
                }
            }

            return null;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string displayId = (string)value;

            DataTable dataTable = DataReader.GetPeril().Tables["Peril"];
            foreach (DataRow row in dataTable.Rows)
            {
                if (row[parameter.ToString()].ToString() == displayId)
                {
                    return row["CODE"];
                }
            }
            return null;
        }

        #endregion
    }
}


I still can't get around the problem... I also noticed that the sample code is performing in the same way whereby when we select a value from the combo box and move the cursor to another cell and click on it we lose our previous combo box selection.
Coordinator
Oct 7, 2008 at 3:34 AM
So you are seeing the same problem in the samples?  I'm not able to repro your issue then.  Are these the correct steps that you are doing:

1. open a cell of the ComboBoxColumn for edit (F2)
2. open the the dropdown and select a new value in the drop down list
3. press 'Tab' or 'Enter'

Expect: cell in ComboBoxColumn updates to new value selected in drop down list
Actual: cell in ComboBoxColumn stays as the same value as before

Is that correct b/c I'm not able to see that issue with the samples.

Oct 7, 2008 at 4:54 PM
In the sample, if I click on another cell after selecting a value from the combo box in the preceding cell, I lose my selection and the preceding cell defaults to NULL. That has been the exact behavior of my program as well.