This is a migrated thread and some comments may be shown as answers.

EditItem() doesn't update until I edit the cell?

2 Answers 43 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Pierre
Top achievements
Rank 1
Pierre asked on 06 Jan 2016, 02:28 PM

I call EditItem(), make my changes and then call CommitEdit().  Although the documentation says "That's it - the RadGridView will show the updated data immediately.", that doesn't seem to work for me.  The change only appears when I enter edit mode in the grid by double-clicking the cell.

I've created a small project to demonstrate my issue.

<Window x:Class="RadGridViewUpdate.MainWindow"
                xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
                Title="MainWindow" Height="350" Width="525">
   <StackPanel>
      <telerik:RadGridView x:Name="grid" />
      <Button Content="Change" Click="OnChange" />
   </StackPanel>
</Window>
 

public partial class MainWindow : Window
{
   public class Test
   {
      public string first { get; set; }
      public string second { get; set; }
      public string third { get; set; }
   }
 
   Test entry1 = new Test() { first = "1", second = "2", third = "3" };
   Test entry2 = new Test() { first = "1", second = "2", third = "3" };
 
   public MainWindow()
   {
      InitializeComponent();
      grid.Items.Add(entry1);
      grid.Items.Add(entry2);
   }
 
   private void OnChange(object sender, RoutedEventArgs e)
   {
      grid.Items.EditItem(entry1);
      entry1.second = "5";
      grid.Items.CommitEdit();
   }
}

When I click "Change", nothing is changed in the grid until I double-click the second column of the first row.

Note that this is just an example.  In my full application, I use MVVM and did try to "RaisePropertyChange".

 

 

2 Answers, 1 is accepted

Sort by
0
Accepted
Dilyan Traykov
Telerik team
answered on 08 Jan 2016, 01:06 PM
Hello Pierre,

As stated in this article which I believe you're referring to, the business object must implement the INotifyPropertyChanged interface in order to notify the GridView for any changes made to the object.

Here's the full code to make this example work:
public partial class MainWindow : Window
{
    public class Test : INotifyPropertyChanged
    {
        private string first { get; set; }
        private string second { get; set; }
        private string third { get; set; }
 
        public string First
        {
            get
            {
                return this.first;
            }
            set
            {
                if (this.First == value)
                {
                    return;
                }
 
                this.first = value;
                this.OnPropertyChanged("First");
            }
        }
 
        public string Second
        {
            get
            {
                return this.second;
            }
            set
            {
                if (this.Second == value)
                {
                    return;
                }
 
                this.second = value;
                this.OnPropertyChanged("Second");
            }
        }
 
        public string Third
        {
            get
            {
                return this.third;
            }
            set
            {
                if (this.Third == value)
                {
                    return;
                }
 
                this.third = value;
                this.OnPropertyChanged("Third");
            }
        }
 
        public event PropertyChangedEventHandler PropertyChanged;
 
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
 
    Test entry1 = new Test() { First = "1", Second = "2", Third = "3" };
    Test entry2 = new Test() { First = "1", Second = "2", Third = "3" };
                                                           
    public MainWindow()
    {
        InitializeComponent();
        grid.Items.Add(entry1);
        grid.Items.Add(entry2);
    }
 
    private void OnChange(object sender, RoutedEventArgs e)
    {
        grid.Items.EditItem(entry1);
        entry1.Second = "5";
        grid.Items.CommitEdit();
    }
}

Please let me know if you have any further questions.

Regards,
Dilyan Traykov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
Pierre
Top achievements
Rank 1
answered on 08 Jan 2016, 02:13 PM

As I said, this was just a very quick example since the real application is MVVM and I had already tried a "OnPropertyChanged" without success.

But your code made me realize my error.  My RadGridView uses the ItemSource property which is binded to an ObservableCollection.  I was doing an OnPropertyChanged on my ObservableCollection instead of the specific item member that was changed.  Now that I do that, the cell is immediately updated as expected.

Thanks!

Tags
GridView
Asked by
Pierre
Top achievements
Rank 1
Answers by
Dilyan Traykov
Telerik team
Pierre
Top achievements
Rank 1
Share this question
or