prattom
prattom

Reputation: 1753

Editing and saving data in datagrid

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?

Upvotes: 0

Views: 1649

Answers (2)

mm8
mm8

Reputation: 169420

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.

Upvotes: 1

NamNDT
NamNDT

Reputation: 16

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"}

Upvotes: 0

Related Questions