Updating WPF DataGrid.

Jan 30, 2009 at 12:58 AM
How do you automatically update WPF DataGrid control if an object's value was changed that bound (DataGrid1.ItemsSource = lstMyValues) to it?  Let's say I have a list of a items ( shown below ).

public class MyValues
    public string One { get; set; }
    public string Two { get; set; }
    public string Three { get; set; }

One of values changes.  Two = ( new value ).  How do get the displayed info in datagrid to automatically update?

Also, what if I wanted to add another to list.  How could you add to list and update in datagrid?  Same for deleting an item from list?

Thanks in advance.
Jan 30, 2009 at 2:55 AM
You'll need to have your MyValues class implement INotifyPropertyChanged and add the associated plumbing.
Check out the following tutorial:  http://coredotnet.blogspot.com/2006/05/wpf-data-binding-tutorial.html

Listing #4 should guide you in the direction you want as far your class.

As for your DataGrid, have a look at some of these for some in depth info:

However, once you've implemented INotifyPropertyChanged, your data grid should start seeing the value updates automatically.

public class MyValues : INotifyPropertyChanged {
    private string one;
    private string two;
    private string three;

        public event PropertyChangedEventHandler PropertyChanged;

        public string One {
            get { return one; }
            set { one = value; OnPropertyChanged(new PropertyChangedEventArgs("One ")); }

        public string Two {
            get { return two; }
            set { two= value; OnPropertyChanged(new PropertyChangedEventArgs("Two")); }

        public string Three{
            get { return three; }
            set { three= value; OnPropertyChanged(new PropertyChangedEventArgs("Three")); }

        public void OnPropertyChanged(PropertyChangedEventArgs e) {
            if (PropertyChanged != null) {
                PropertyChanged(this, e);

in your XAML for your DataGrid's column definitions:
        <custom:DataGrid  x:Name="DataGrid1">
                <custom:DataGridTextColumn Header="One" Binding="{Binding Path=One}" />
                <custom:DataGridTextColumn Header="Two" Binding="{Binding Path=Two}" />
                <custom:DataGridTextColumn Header="Three" Binding="{Binding Path=Three}" />

Hope that gets you going.

Jan 30, 2009 at 1:54 PM
That did the trick.  Thanks for the info.