This project has moved. For the latest updates, please go here.

How to update a cell in a DataGrid once another cell is modified on the same row?

May 23, 2013 at 11:10 AM
Hello!

I have developed a program to display and edit data in a DataGrid, based on the Book Library sample. My setting are:

Data Mapping: Entity Framework

Database: SQL Server 2010

Binding main table: tblAddress; fields: Address_ID (auto-incremented), FK_Contact_ID, Address_1, Address_2, ...

Binding foreign (linked) table: tblContact; field: Contact_ID (auto-incremented), ContactName

Navigation Property: AddressToContact (tblAddress.FK_Contact_ID ==> tblContact.Contact_ID)

DataGrid Columns: Address_ID, FK_Contact_ID, ContactName, Address_1, Address_2, ...


XAML:
<DataGrid x:Name="xDataGrid" DataContext="{Binding}" ItemsSource="{Binding AddressCollection}" SelectedItem="{Binding SelectedAddress}" SelectionChanged="DataGridSelectionChanged">
<DataGrid.Columns>
            <DataGridTextColumn Header="Address_ID" IsReadOnly="True"
                    Binding="{Binding Address_ID,  Mode=OneWay}" />

            <DataGridTextColumn Header="FK_Contact_ID" 
                    Binding="{Binding FK_Contact_ID, Mode=TwoWay, 
                    UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True, NotifyOnValidationError=True}" />
<DataGridTextColumn Header="ContactName" 
                    Binding="{Binding AddressToContact.ContactName, Mode=TwoWay, 
                    UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True, ValidatesOnExceptions=True, NotifyOnValidationError=True}" />
</DataGrid.Columns>
</DataGrid>
When I run the application all cells are populated from the database correctly. However, when I change a cell value in the FK_Contact_ID column, the corresponding ContactName cell should get updated automatically; but it does not until I click on it.

Please advise how I can fix this issue. In fact, the Mode attribute of the ContactName column should be set to OneWay.

If you need more info, please let me know.

Regards


Sabba
May 26, 2013 at 1:22 AM
Edited May 26, 2013 at 1:23 AM
Fixed! Overlooked to create a corresponding partial class tblAddress to invoke a PropertyChanged event.