WPF DataGrid - DataGridComboBoxColumn Loading

Jun 18, 2009 at 2:41 AM

Hi

I am trying to setup a DataGrid with a DataGridComboBoxColumn.

For the ItemsSource of the DataGrid I have a method that calls a stored procedure and returns me an ObservableCollection of the same and I am able to display the data.

I need to have one of the columns be editable using a combobox and I have a lookup stored procedure to provide the values as a List<string>. Both sps lookup from the same table.

The problem is that I am getting an empty column. Below is the xaml for this. Please can someone help out?

 

<Window
   x:Class="WPFObservableCollection.GroupingWindow"
   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   xmlns:dg="http://schemas.microsoft.com/wpf/2008/toolkit"
   xmlns:local="clr-namespace:WPFObservableCollection"
   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
   Title="ComboTest">
    <Window.Resources>
        <ObjectDataProvider x:Key="Source1" ObjectType="{x:Type local:PeopleDataProvider}" MethodName="GetPeople"/>
        <ObjectDataProvider x:Key="Source2" ObjectType="{x:Type local:PeopleDataProvider}" MethodName="GetCities" />
    </Window.Resources>
    <Grid>
        <dg:DataGrid
         Name="PeopleGrid"
         AutoGenerateColumns="False"
         ItemsSource="{Binding Source={StaticResource Source1}}">
            <dg:DataGrid.Columns>
                <dg:DataGridTextColumn Binding="{Binding Path=City, Mode=TwoWay}" Header="City"/>
                <dg:DataGridComboBoxColumn SelectedValueBinding="{Binding City}"                                            
                                           SelectedValuePath="City"
                                           DisplayMemberPath="City"
                                           Header="City"
                                           ItemsSource="{Binding Source={StaticResource Source2}}">
                    <dg:DataGridComboBoxColumn.EditingElementStyle>
                        <Style TargetType="ComboBox">
                            <Setter Property="IsEditable" Value="True" />
                        </Style>
                    </dg:DataGridComboBoxColumn.EditingElementStyle>
                </dg:DataGridComboBoxColumn>
                <dg:DataGridTextColumn Binding="{Binding Path=FirstName, Mode=TwoWay}" Header="First Name"/>
                <dg:DataGridTextColumn Binding="{Binding Path=LastName, Mode=TwoWay}" Header="Last Name"/>
                <dg:DataGridTextColumn Binding="{Binding Path=id}" x:Name="id" IsReadOnly="True" Visibility="Hidden"/>
            </dg:DataGrid.Columns>
        </dg:DataGrid>
    </Grid>
</Window>

 

 

Jun 19, 2009 at 1:40 PM

Does the return type of GetCities contain attribute/field publicly exposed 'City' in it?

As the SelectedValueBinding is set to City you need to have it I guess. So then you will get the combobox filled values.

And if the collection of values contain the city value of the record in datagrid you will get the value as selected.

- B

Jun 19, 2009 at 4:06 PM

I think you also have to  set the binding on the "EditingElementStyle",  include an "ElementStyle" and set the "IsSynchronized"property to false.

                 <dg:DataGridComboBoxColumn SelectedValueBinding="{Binding City}"                                           
                                           SelectedValuePath="City"
                                           DisplayMemberPath="City"
                                           Header="City"
                                           ItemsSource="{Binding Source={StaticResource Source2}}">
                      <dg:DataGridComboBoxColumn.ElementStyle>
                        <Style TargetType="ComboBox">
                            <Setter Property="IsEditable" Value="True" />
                            <Setter Property="ItemsSource"
                                        Value="{Binding Source={StaticResource Source2}}">
                            <Setter Property="IsSynchronizedWithCurrentItem" Value="False" />
                        </Style>
                   </dg:DataGridComboBoxColumn.ElementStyle>
                    <dg:DataGridComboBoxColumn.EditingElementStyle>
                        <Style TargetType="ComboBox">
                            <Setter Property="IsEditable" Value="True" />
                      <Setter Property="ItemsSource"
                                        Value="{Binding Source={StaticResource Source2}}">
                                <Setter Property="IsSynchronizedWithCurrentItem" Value="False" />
                        </Style>
                   </dg:DataGridComboBoxColumn.EditingElementStyle>
                </dg:DataGridComboBoxColumn>

Hope this works

 

Jun 19, 2009 at 7:54 PM

Thanks bnovicewpf

My collection did not have a column named as City and when I fixed that, I was able to make it work.