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

RadGridView not updating after Editing

2 Answers 337 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Syam
Top achievements
Rank 1
Syam asked on 23 Nov 2011, 05:34 PM
Hi,

I am having a RadGridView with one particular GridViewDataColumn having a Button and textblock as a cell template. On Click of any of this button, I want to edit the details of that particular row. The RadGridView is bound to an ObservableCollection. On click of the button, I am calling a DelegateCommand to do the action. I am passing the current row as the CommandParameter of that button. In the ViewModel, I am updating the Entity associated with the GridRow. But after I update, the changes are not reflected in the RadGridView. But if i click on that particular cell which is modified, the value gets updated.

I even tried using PagedCollectionView instead of ObservableCollection and tried using PagedCollectionView.Refresh() and  PagedCollectionView.CommitEdit() methods to trigger the change but with no success.


How do i fix this problem. I am using Prism & MVVM pattern for development. Below is my code

Note: I have set the DataContext of my View to the ViewModel in View.Xaml.cs

XAML
<UserControl.Resources>
        <local:MainViewModel x:Key="ViewModel"></local:MainViewModel>
  </UserControl.Resources>
 
<telerik:RadGridView x:Name="grdView"  AutoGenerateColumns="False" Height="400" Width="600" ItemsSource="{Binding Path=PeopleList, Mode=TwoWay}" ShowGroupPanel="False">
            <telerik:RadGridView.Columns>
<telerik:GridViewDataColumn DataMemberBinding="{Binding Path=FirstName, Mode=TwoWay}" UniqueName="First Name" Width="100"/>
                <telerik:GridViewDataColumn DataMemberBinding="{Binding Path=LastName, Mode=TwoWay}" UniqueName="Last Name" Width="100"/>
                    <telerik:GridViewDataColumn IsReadOnly="True" Header="Pick Up Job" UniqueName="Pick Up Job" Width="120">
                        <telerik:GridViewDataColumn.CellTemplate>
                            <DataTemplate>
                                <StackPanel Orientation="Horizontal" Width="120">
                                    <TextBlock x:Name="txtCode" Text="{Binding Path=PickUpJob, Mode=TwoWay}" Width="90"></TextBlock>
                                    <telerik:RadButton x:Name="btnEllp" HorizontalAlignment="Right" Content="..." Command="{Binding Path=ComponentCommand, Source={StaticResource ViewModel} }" CommandParameter="{Binding}"  Width="30">
                                    </telerik:RadButton>
                                </StackPanel>
                            </DataTemplate>
                        </telerik:GridViewDataColumn.CellTemplate>
                        
                    </telerik:GridViewDataColumn>
                </telerik:RadGridView.Columns>
        </telerik:RadGridView>


View Model Class

private People people;
 
        public People People
        {
            get { return people; }
            set
            {
                people = value;
                this.RaisePropertyChanged(() => this.People);
            }
        }
        private ObservableCollection<People> peopleList = new ObservableCollection<People>();
        public ObservableCollection<People> PeopleList
        {
            get { return peopleList; }
            set
            {
                peopleList = value;
                this.RaisePropertyChanged(() => this.PeopleList);
            }
        }
 
private ICommand componentCommand;
        
        public ICommand ComponentCommand
        {
            get
            {
                if (componentCommand == null)
                {
                    componentCommand = new DelegateCommand<People>(OnComponentCommand);
                }
                return componentCommand;
            }
            set { componentCommand = value; }
        }
 
        public MainViewModel()
        {
            LoadValues();
        }
 
private void OnComponentCommand(People  pe)
        {
            pe.LastName = "New";
this.RaisePropertyChanged(() => this.People);
          }
 
private void LoadValues()
        {
            foreach (var x in new People[] {
                                            new People { PeopleID=0, CountryID = 0, FirstName = "Sebastain", LastName = "Vettel", PickUpJob ="P1" },
                                            new People { PeopleID=1, CountryID = 1, FirstName = "Fernando", LastName = "Alonso", PickUpJob ="P2" },
                                            new People { PeopleID=2, CountryID = 2, FirstName = "James", LastName = "Button", PickUpJob ="P1" },
                                            new People { PeopleID=3, CountryID = 0, FirstName = "Michael", LastName = "Schumacher", PickUpJob ="P3" },
                                            new People { PeopleID=4, CountryID = 1, FirstName = "Felipe", LastName = "Masa", PickUpJob ="P4" },
                                            new People { PeopleID=5, CountryID = 2, FirstName = "David", LastName = "Hill", PickUpJob ="P1" },
                                            new People { PeopleID=6, CountryID = 2, FirstName = "Mark", LastName = "Strong", PickUpJob ="P2" }
                                           })
            {
                PeopleList.Add(x);
            }
        }

Model 
public class People
    {
        public int CountryID { get; set; }
        public int PeopleID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string PickUpJob { get; set; }
    }



Any help is appreciated

Thanks
Syam

2 Answers, 1 is accepted

Sort by
0
Dimitrina
Telerik team
answered on 25 Nov 2011, 01:20 PM
Hi Syam,

When you change the values of the properties from code behind, you will need to raise the CollectionChanged of the GridView.

You could use the built-in RadGridView's IEditableCollectionView support:

MyGridView.Items.EditItem(item);
//change the property
item.Age += 1000;
MyGridView.Items.CommitEdit();

This code will raise the CollectionChanged event of the GridView.

Regards,
Didie
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

1
Syam
Top achievements
Rank 1
answered on 25 Nov 2011, 03:22 PM
Hi Didie,

Thanks for the reply. How do i achieve this in MVVM pattern as i cant access my RadGridView directly. I am using a PagedCollectionView or ObservableCollection to bind data to RadGridView.

Thanks

Syam
Tags
GridView
Asked by
Syam
Top achievements
Rank 1
Answers by
Dimitrina
Telerik team
Syam
Top achievements
Rank 1
Share this question
or