Updating or Adding Only The Relevant Record of WPF DataGrid

Faraz Qureshi 116 Reputation points
2020-11-08T19:54:51.063+00:00

Hi All,

While going through a sample project in learning WPF Core as I attempted to work with setting up a DataGrid with Updating Buttons' Column on the Window, I found that with code pieces like the following, if I carried out any change, upon clicking button on the SAME row, the said datagrid returned back to the original data again, i.e. not updating the relevant record in the backend SQL database.

However, at the same, if I carried out the said change but clicked the said button on some OTHER row the data change carried on other row is found to be updated.

Besides that, while applying the feature of CanUserAddRows as "True" since I want the said button to be allowing to add the data in respect of new record, if any inserted, I find the same to be not saved/created upon clicking the said button, and the Visual Studio to be complaining as:

SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_Product_Movt". The conflict occurred in database "CRUDDB", table "dbo.ProductItems", column 'ProdId'. The statement has been terminated.

Relevant portions of Code are as follows:

<DataGrid Grid.Row="1" Grid.ColumnSpan="2" x:Name="MovtData" AutoGenerateColumns="False" CanUserAddRows="True" IsReadOnly="False" ColumnWidth="auto" Margin="2" IsSynchronizedWithCurrentItem="True" FontSize="12">

<DataGrid.Columns>
    <DataGridTextColumn Header="Id" Binding="{Binding TranxId}"/>
    <DataGridTextColumn Header="As On" Binding="{Binding TranxDt}"/>
    <DataGridTextColumn Header="Transaction" Binding="{Binding SalePurchase}"/>
    <DataGridTextColumn Header="Qty" Binding="{Binding Quantity}"/>
    <DataGridTextColumn Header="Rate" Binding="{Binding TranxRate}"/>
    <DataGridTextColumn Header="Product" Binding="{Binding WhichProd}"/>

    <DataGridTemplateColumn Header="Update">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <Button Content="Update" Click="btnMSave_Click" Foreground="Green"/>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>

    <DataGridTemplateColumn Header="Delete">
        <DataGridTemplateColumn.CellTemplate>
            <DataTemplate>
                <Button Content="Delete" Click="btnMDel_Click" Foreground="Red"/>
            </DataTemplate>
        </DataGridTemplateColumn.CellTemplate>
    </DataGridTemplateColumn>
</DataGrid.Columns>

and the relevant piece of code at back-end in MainWindow.xaml.cs being:

private ProductContext PDC;
ProductMovt M2BU = new ProductMovt();

private void btnMSave_Click(object sender, RoutedEventArgs e)
{
    M2BU = ((FrameworkElement)sender).DataContext as ProductMovt;
    PDC.Update(M2BU);
    PDC.SaveChanges();
    MovtData.ItemsSource = PDC.ProductMovts.Where(f => f.ProductItem == ProdData.DataContext).ToList();
}

Please see if any of you experts can guide.

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,758 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.