WPF- Changes not updated in datagrid column

Daniel D 0 Reputation points
2025-05-13T00:33:30.32+00:00

Hi All,

I have a WPF datagrid control in the UI. It has three columns ProductID, Rate and Amount. As user enters the rate in "Rate" column or select Product in "ProductID" column it has some calculations in datagrid "celleditending" event to update "Amount" column.

private void dtGrdProduct_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)

{

int productId = 0, currentRowIndex = 0;

decimal rate = 0, amount = 0;

try

{

    switch (Convert.ToString(e.Column.Header).ToUpper())

    {

        case "PRODUCT ID":

            productId = Convert.ToInt32(((ComboBox)e.EditingElement).SelectedValue);

            switch (productId)

            {

                case 1:

                case 2:

                    rate = 5.00;

                    amount = (rate * Convert.ToDecimal(this.txtblkSubTotal.Text)) / 100;

                    break;

                default:

                    rate = 1.00;

                    amount = (rate * Convert.ToDecimal(this.txtblkSubTotal.Text)) / 100;

                    break;

            }

            var dvPO = this.dtGrdProduct.ItemsSource as DataView;

            currentRowIndex = e.Row.GetIndex();

            dvPO[currentRowIndex]["Rate"] = rate;

            dvPO[currentRowIndex]["Amount"] = amount;

            break;

        case "@":

            var dv = this.dtGrdProduct.ItemsSource as DataView;

            currentRowIndex = e.Row.GetIndex();

            rate = Convert.ToDecimal(dv[currentRowIndex]["Rate"]);

            amount = (rate * Convert.ToDecimal(this.txtblkSubTotal.Text) / 100);

            dv[currentRowIndex]["Amount"] = amount;

            break;

    }

}

}

I have been using ADO.NET DataView as Itemssource of datagrid and updating the "Amount" in same dataview which is bound to itemssource of datagrid "DtGrdProduct" in UI. The issue is this update value in "Amount" column is not reflecting or updated in datagrid column "Amount" in UI. Please advise how can I fix this issue.

Regards,

Daniel

Developer technologies | XAML
{count} votes

1 answer

Sort by: Most helpful
  1. Daniel D 0 Reputation points
    2025-05-13T08:32:58.1033333+00:00

    Hi @Viorel ,

    I have established the bindings in XAML. Both "Rate" and "Amount" columns are DataGridTemplateColumn.

    <DataGrid Grid.Row="2" Name="dtGrdProduct" HorizontalAlignment="Right" AutoGenerateColumns="False" ItemsSource="{Binding Path= Product}" CanUserDeleteRows="False" Height="{Binding RelativeSource= {RelativeSource AncestorType= {x:Type UserControl}}, Path= ActualHeight, Converter={StaticResource HeightConverter}, ConverterParameter= '8'}" MinHeight="50" Margin="10,10,5,5" Width="500" HorizontalGridLinesBrush="LightGray" VerticalGridLinesBrush="LightGray" Loaded="DataGrid_Loaded" CellEditEnding="dtGrdProduct_CellEditEnding" PreviewKeyDown="DataGrid_PreviewKeyDown">

    <DataGrid.RowValidationRules>
    
        <local:DataRowValidation ValidationStep="UpdatedValue"></local:DataRowValidation>
    
    </DataGrid.RowValidationRules>
    
    <DataGrid.Columns>
    
        <DataGridComboBoxColumn x:Name="cmbProduct" Header="Product ID" DisplayMemberPath="Name" SelectedValuePath="ID" SelectedValueBinding="{Binding Path= ProductID, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"></DataGridComboBoxColumn>
    
        <DataGridTemplateColumn Header="@" HeaderStyle="{StaticResource HeaderContentRightAlign}" CellStyle="{StaticResource CellTextRightAlign}">
    
            <DataGridTemplateColumn.CellTemplate>
    
                <DataTemplate>
    
                    <TextBlock Text="{Binding Path= Rate, StringFormat={}{0:n2}}"></TextBlock>
    
                </DataTemplate>
    
            </DataGridTemplateColumn.CellTemplate>
    
            <DataGridTemplateColumn.CellEditingTemplate>
    
                <DataTemplate>
    
                    <Grid>
    
                        <Grid.RowDefinitions>
    
                            <RowDefinition Height="Auto"></RowDefinition>
    
                            <RowDefinition Height="Auto"></RowDefinition>
    
                        </Grid.RowDefinitions>
    
                        <TextBlock Text="{Binding Path= Rate, StringFormat={}{0:n2}}">
    
                            <TextBlock.Style>
    
                                <Style TargetType="{x:Type TextBlock}">
    
                                    <Style.Setters>
    
                                        <Setter Property="Visibility" Value="Collapsed"></Setter>
    
                                    </Style.Setters>
    
                                    <Style.Triggers>
    
                                        <DataTrigger Binding="{Binding Path= ProductID}" Value="4">
    
                                            <DataTrigger.Setters>
    
                                                <Setter Property="Visibility"  Value="Visible"></Setter>
    
                                            </DataTrigger.Setters>
    
                                        </DataTrigger>
    
                                    </Style.Triggers>
    
                                </Style>
    
                            </TextBlock.Style>
    
                        </TextBlock>
    
                        <TextBox Grid.Row="1" Text="{Binding Path= Rate, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged,  StringFormat={}{0:n2}}">
    
                            <TextBox.Style>
    
                                <Style TargetType="{x:Type TextBox}">
    
                                    <Style.Setters>
    
                                        <Setter Property="Visibility" Value="Visible"></Setter>
    
                                    </Style.Setters>
    
                                    <Style.Triggers>
    
                                        <DataTrigger Binding="{Binding Path= ProductID}" Value="4">
    
                                            <DataTrigger.Setters>
    
                                                <Setter Property="Visibility"  Value="Collapsed"></Setter>
    
                                            </DataTrigger.Setters>
    
                                        </DataTrigger>
    
                                    </Style.Triggers>
    
                                </Style>
    
                            </TextBox.Style>
    
                        </TextBox>
    
                    </Grid>
    
                     </DataTemplate>
    
            </DataGridTemplateColumn.CellEditingTemplate>
    
        </DataGridTemplateColumn>
    
        <DataGridTextColumn IsReadOnly="True" Binding="{Binding Path= ProductID, Converter={StaticResource productConverter} }"></DataGridTextColumn>
    
        <DataGridTemplateColumn Header="Amount" HeaderStyle="{StaticResource HeaderContentRightAlign}" CellStyle="{StaticResource CellTextRightAlign}">
    
            <DataGridTemplateColumn.CellTemplate>
    
                <DataTemplate>
    
                    <TextBlock Text="{Binding Path=Amount, StringFormat={}{0:n2}}">
    
                    </TextBlock>
    
                </DataTemplate>
    
            </DataGridTemplateColumn.CellTemplate>
    
            <DataGridTemplateColumn.CellEditingTemplate>
    
                <DataTemplate>
    
                    <Grid>
    
                        <Grid.RowDefinitions>
    
                            <RowDefinition Height="Auto"></RowDefinition>
    
                            <RowDefinition Height="Auto"></RowDefinition>
    
                        </Grid.RowDefinitions>
    
                        <TextBlock Text="{Binding Path=Amount, StringFormat={}{0:n2}}">
    
                            <TextBlock.Style>
    
                                <Style TargetType="{x:Type TextBlock}">
    
                                    <Style.Setters>
    
                                        <Setter Property="Visibility" Value="Visible"></Setter>
    
                                    </Style.Setters>
    
                                    <Style.Triggers>
    
                                        <DataTrigger Binding="{Binding Path= ProductID}" Value="4">
    
                                            <DataTrigger.Setters>
    
                                                <Setter Property="Visibility" Value="Collapsed"></Setter>
    
                                            </DataTrigger.Setters>
    
                                        </DataTrigger>
    
                                    </Style.Triggers>
    
                                </Style>
    
                            </TextBlock.Style>
    
                        </TextBlock>
    
                        <TextBox Grid.Row="1" Text="{Binding Path= Amount, Mode=TwoWay, StringFormat={}{0:n2}}">
    
                            <TextBox.Style>
    
                                <Style TargetType="{x:Type TextBox}">
    
                                    <Style.Setters>
    
                                        <Setter Property="Visibility" Value="Collapsed"></Setter>
    
                                    </Style.Setters>
    
                                    <Style.Triggers>
    
                                        <DataTrigger Binding="{Binding Path= ProductID}" Value="4">
    
                                            <DataTrigger.Setters>
    
                                                <Setter Property="Visibility" Value="Visible"></Setter>
    
                                            </DataTrigger.Setters>
    
                                            </DataTrigger>
    
                                    </Style.Triggers>
    
                                </Style>
    
                            </TextBox.Style>
    
                        </TextBox>
    
                    </Grid>
    
                </DataTemplate>
    
            </DataGridTemplateColumn.CellEditingTemplate>
    
        </DataGridTemplateColumn>
    
        <DataGridTextColumn Header="Comments">
    
            <DataGridTextColumn.Binding>
    
                <Binding Path="Comments">
    
                    <Binding.ValidationRules>
    
                        <local:DataRowValidation ValidationStep="UpdatedValue"></local:DataRowValidation>
    
                    </Binding.ValidationRules>
    
                </Binding>
    
            </DataGridTextColumn.Binding>
    
        </DataGridTextColumn>
    
    </DataGrid.Columns>
    ```</DataGrid>
    
    Regards,
    
    Daniel
    
    0 comments No comments

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.