I have one form open and then I go to the table and update one record. Then I return back and refresh the form, but that record is not updated.
I have a DataGrid in the form, which is bound to an ObservableCollection. On refresh, I add records from the database to this ObservableCollection using:
filteredProductList.Clear();
foreach(Formulation frm in dbContext.Formulations)
{
filterViewedList.Add(frm);
}
The PageLoad event is below:
private void loadData()
{
try
{
filterViewedList= new ObservableCollection<Formulation>(dbContext.Formulations);
dgRecords1.ItemsSource = filterViewedList;
}
The XAML is below:
<DataGrid Height="387" x:Name="dgRecords1" Margin="0,0,64,0"
IsSynchronizedWithCurrentItem="True" Style="{DynamicResource StyleDatagrid}"
ClipboardCopyMode="None" ColumnHeaderStyle="{DynamicResource DataGridColumnHeaderStyle1}"
SelectionChanged="dgRecords1_SelectionChanged" Grid.Column="1" Grid.Row="3">
<DataGrid.Resources>
<SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="LightBlue"/>
</DataGrid.Resources>
<DataGrid.Columns>
<DataGridTextColumn Width="110" Header="Code" Binding="{Binding FrmltnNo, Mode=TwoWay}" IsReadOnly="True" Foreground="Black"/>
<DataGridTextColumn Width="415" Header="Description" Binding="{Binding FrmltnName, Mode=TwoWay}" IsReadOnly="True" Foreground="Black"/>
<DataGridTextColumn Width="*" Header="Status" Binding="{Binding Status, Mode=TwoWay}" IsReadOnly="True" >
<DataGridTextColumn.CellStyle>
<Style TargetType="{x:Type DataGridCell}">
<Setter Property="Foreground" Value="{Binding Converter={StaticResource FGColorKey}}"/>
</Style>
</DataGridTextColumn.CellStyle>
</DataGridTextColumn>
</DataGrid.Columns>
</DataGrid>
Show the code you call “refresh”. dgRecords1.ItemsSource = filterViewedList is just going to be a static copy. I suspect the UI is not aware of the update. Expose filteredProductList as a public ObservableCollection FilteredProductList and bind to FilteredProductList and then the UI will be aware of the update via the magic of ObservableCollection. And it would be cleaner to update only the one rather than repopulate the whole list.