-1

How to calculate the two input columns values to Display a Total column in a same DataGrid in a runtime. Which event is best event to calculate the grid value. Please help me.

XAML:

<DataGrid.Columns>
   <DataGridTextColumn Header="StudentName" FontWeight="Bold" Width="100"
                       Binding="{Binding Student}" />
   <DataGridTextColumn Header="Subject1" FontWeight="Bold" Width="100" 
                       Binding="{Binding Subject1}" />
   <DataGridTextColumn Header="Subject2" FontWeight="Bold" Width="100" 
                       Binding="{Binding Subject2}"/>
   <DataGridTextColumn Header="Total" FontWeight="Bold" Width="100"
                       Binding="{Binding Total}"/>
</DataGrid.Columns>

Code:

private void dgSubject_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) { }
Sentry
  • 4,102
  • 2
  • 30
  • 38
  • 3
    Welcome to StackOverflow! We want to help you, but you have to tell us a bit more about your problem and show us what you have already tried. – Sentry Dec 21 '16 at 06:35
  • private void dgSubject_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e) { } – SadamHussain Dec 21 '16 at 08:57

1 Answers1

0

If you want to use events to calculate a sum, you might want to check the CellEditEnding event. However, the better way to go for in WPF is data binding, because it gives you better control on when your data should be updated and it provides a better separation of presentation and business code (which is what MVVM is about).

Data binding means that you create an object that contains your data and connect it with your DataGrid. The grid will display the information of the object and it will update it when the user changes the grid data. It also automatically performs the necessary conversions. When your object implements the INotifyPropertyChanged event, the DataGrid will also be updated when your object changes (regardless of who changed it: the user with an edit or the application in program code). In your case you would need something similar to this:

class ExampleObject : INotifyPropertyChanged {
    private int m_value1;
    private int m_value2;

    public int Value1 {
        get {
            return m_value1;
        }
        set {
            if (value != m_value1) {
                m_value1 = value;
                OnPropertyChanged(new PropertyChangedEventArgs(nameof(Value1));
                OnPropertyChanged(new PropertyChangedEventArgs(nameof(Sum));
            }
        }
    }

    public int Value2 {
        get {
            return m_value2;
        }
        set {
            if (value != m_value2) {
                m_value2 = value;
                OnPropertyChanged(new PropertyChangedEventArgs(nameof(Value2));
                OnPropertyChanged(new PropertyChangedEventArgs(nameof(Sum));
            }
        }
    }

    public int Sum {
        get {
            return m_value1 + m_value2;
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, e);
        }
    }
}

When you bind your data object to the grid, it will discover that it implements INotifyPropertyChanged and automatically update when you edit the values. Check this post on how to bind your object to the DataGrid.

Community
  • 1
  • 1
Sefe
  • 13,731
  • 5
  • 42
  • 55