tglk
tglk

Reputation: 89

WPF DataGrid binding

I came across an issue when a IList is bound to a datagrid.

The class structure is,

public class Customer
{
    public virtual int CustomerID { get; set; }
    public virtual string Name { get; set; }
    public virtual string AddressLine1 { get; set; }
    public virtual string AddressLine2 { get; set; }
    public virtual string AddressLine3 { get; set; }
    public virtual bool IsActive { get; set; }
    public virtual SalesLine SalesLine { get; set; }
    public virtual int Precedence { get; set; }
}

public class SalesLine
{
    public virtual int SalesLineID { get; set; }
    public virtual string Name { get; set; }
    public virtual SalesPerson SalesPerson { get; set; }
    public virtual IList<Customer> Customers { get; set; }
}

I create a new IList of Customer class.

IList<Customer> customerList = new IList<Customer>();

Then the "customerList" is populated. Since customer has a SalesLine the "salesLines" IList is populated with all sales lines in the database.

Now with following XAML I am going to bind the customerList to the datagrid,

<DataGrid AutoGenerateColumns="False" Grid.Column="1" Grid.Row="1" HorizontalAlignment="Stretch"  
                  Name="grdCustomers" VerticalAlignment="Stretch" Grid.ColumnSpan="2" 
                  ItemsSource="{Binding CustomerList}" CanUserAddRows="True">
            <DataGrid.Columns>
                <DataGridTextColumn Header="Name" Binding="{Binding Name}" Width="250" />
                <DataGridTextColumn Header="Address Street" Binding="{Binding AddressLine1}" Width="150" />
                <DataGridTextColumn Header="Address Town" Binding="{Binding AddressLine2}" Width="150" />
                <DataGridTextColumn Header="Address Area" Binding="{Binding AddressLine3}" Width="150" />
                <DataGridCheckBoxColumn Header="Is Active" Binding="{Binding IsActive}" Width="75" />
                <DataGridComboBoxColumn Header="Sales Line"  Width="150"
                        SelectedItemBinding="{Binding  SalesLine}"
                        SelectedValuePath="SalesLineID"
                        DisplayMemberPath="Name">
                    <DataGridComboBoxColumn.ElementStyle>
                        <Style TargetType="ComboBox">
                            <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.SalesLineList}"/>
                        </Style>
                    </DataGridComboBoxColumn.ElementStyle>
                    <DataGridComboBoxColumn.EditingElementStyle>
                        <Style TargetType="ComboBox">
                            <Setter Property="ItemsSource" Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.SalesLineList}"/>
                        </Style>
                    </DataGridComboBoxColumn.EditingElementStyle>
                </DataGridComboBoxColumn>
            </DataGrid.Columns>
        </DataGrid>

Now it properly display all the columns except the sales line column.

Even though I have bound the SalesLine to the column, it does not display the current rows sales line name in the grid. However, when I click on the dropdown box, the dropdown is populated.

Please help me displaying the sales line name on the sales line column.

Upvotes: 0

Views: 511

Answers (1)

Rachel
Rachel

Reputation: 132548

This is probably because the Customer.SalesLine property does not point to an object in SalesLineList

You need to overwrite the ToString() method in the SalesLine class to make two objects be the same if their IDs match. By default, it will only consider them the same if they reference the exact same object in memoroy.

Upvotes: 1

Related Questions