0

I am using datagrid in WPF application. Following is my XAML code

<DataGrid x:Name="dgTest" HorizontalAlignment="Left" Height="296" Margin="184,115,0,0" VerticalAlignment="Top" Width="599" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Title" Width="60" Binding="{Binding title}" />
            <DataGridTextColumn Header="Artist" Width="*" Binding="{Binding artist}"/>
            <DataGridTextColumn Header="Value" Width="*" Binding="{Binding number}"/>
            <DataGridCheckBoxColumn Header="test" Width="*" Binding="{Binding test}"/>
        </DataGrid.Columns>
    </DataGrid>

Following is my C# code

public partial class MainWindow : Window
{
    public ObservableCollection<Track> data = new ObservableCollection<Track>();
    public MainWindow()
    {
        InitializeComponent();
        data.Add(new Track() { title = "Think", artist = "Aretha Franklin", number = 7, test=true });
        data.Add(new Track() { title = "Minnie The Moocher", artist = "Cab Calloway", number = 9, test = true });
        data.Add(new Track() { title = "Shake A Tail Feather", artist = "Ray Charles", number = 4, test = true });
        dgTest.ItemsSource = data;
    }

}
public class Track
{
    private String _t;
    private String _a;
    private int _n;
    private bool _fg;
    public String title
    {
        get { return _t; }
        set { _t = value; }
    }
    public String artist
    {
        get { return _a; }
        set { _a = value; }
    }
    public int number
    {
        get { return _n; }
        set { _n = value; }
    }

    public bool test
    {
        get { return _fg; }
        set { _fg = value;  }
    }
}

The data is imported when I start application but if I make changes in any of the cell the changes are not committed since when I try to read data from datagrid it still shows old data. How can I commit changes when user changes field in datagrid?

prattom
  • 1,625
  • 11
  • 42
  • 67
  • 1
    Implement INotifyPropertyChanged on class Track. See [how-to-implement-property-change-notification](https://learn.microsoft.com/en-us/dotnet/framework/wpf/data/how-to-implement-property-change-notification). Check this post which can help [how-do-i-get-a-wpf-datagrid-to-save-changes-back-to-the-database](https://stackoverflow.com/questions/1156731/how-do-i-get-a-wpf-datagrid-to-save-changes-back-to-the-database) – neelesh bodgal May 19 '20 at 08:30
  • 1st link solved my problem but changes are committed only when I click on next row or any other row if I don't select other row changes are committed. Is there any to change this behaviour? – prattom May 19 '20 at 09:25
  • 1
    Check this [link](https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.ieditableobject?redirectedfrom=MSDN&view=netframework-4.8). Also this thread [save-entity-directly-after-edit-in-datagrid](https://stackoverflow.com/questions/31263317/save-entity-directly-after-edit-in-datagrid) – neelesh bodgal May 19 '20 at 12:52

2 Answers2

1

Set the UpdateSourceTrigger property of the bindings to PropertyChanged:

<DataGrid x:Name="dgTest" HorizontalAlignment="Left" Height="296" Margin="184,115,0,0" VerticalAlignment="Top" Width="599" AutoGenerateColumns="False">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Title" Width="60" Binding="{Binding title, UpdateSourceTrigger=PropertyChanged}" />
        <DataGridTextColumn Header="Artist" Width="*" Binding="{Binding artist, UpdateSourceTrigger=PropertyChanged}"/>
        <DataGridTextColumn Header="Value" Width="*" Binding="{Binding number, UpdateSourceTrigger=PropertyChanged}"/>
        <DataGridCheckBoxColumn Header="test" Width="*" Binding="{Binding test, UpdateSourceTrigger=PropertyChanged}"/>
    </DataGrid.Columns>
</DataGrid>

This should cause the source property to get set immediately.

mm8
  • 163,881
  • 10
  • 57
  • 88
0

Implement INotifyPropertyChanged on class Track. like that

public class Track : INotifyPropertyChanged 
{
     public string title
     {
         get{return _t;}
         set
            {
                 _t = value;
                 OnPropertyChanged("title");
            }
     }
     public event PropertyChangedEventHandler PropertyChanged;
     public void OnPropertyChanged(string strCaller = null)
     {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(strCaller));
     }
}

In XAML code, you can modify a little bit:

Binding="{Binding title, Mode="TwoWay", UpdateSourceTrigger="PropertyChanged"}
NamNDT
  • 16
  • 2
  • 1
    This solved my problem but changes are committed only when I click on next row or any other row if I don't select other row changes are committed. Is there any to change this behaviour? – prattom May 19 '20 at 09:25