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

How to refresh datagrid rows using data virtualization

4 Answers 363 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Mechthild
Top achievements
Rank 1
Mechthild asked on 05 Oct 2011, 01:51 PM
Hi,

I am using data virtuialization with the help of VirtualQueryableCollectionView. Everything works allright. Now I change the content of some database items on a button click. How can I refresh the rows of my gridview. The grid only shows the right content of rows that are newly loaded. Some rows (I guess these are rows that are already displayed and still kept in memory) are not actual.
What do I have to do?

Here is a source code example:

 

 

public partial class MainWindow : Window
{
        WomanEntities _container = new WomanEntities();
  
        public MainWindow()
        {
            InitializeComponent();
            VirtualQueryableCollectionView view = new VirtualQueryableCollectionView(_container.Women.OrderBy(k => k.ID));
            view.LoadSize = 20;
            DataContext = view;
        }
  
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            WomanEntities _container = new WomanEntities();
            IQueryable<Woman> women = (from k in _container.Women
                                               where k.Age == 60
                                               select k);
            foreach (var item in women)
                item.HairColor = "white";
            _container.SaveChanges();
            ((VirtualQueryableCollectionView)DataContext).Refresh();
        }
    }
Thanks for help in advance

4 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 10 Oct 2011, 08:20 AM
Hi Mechthild,

Not sure what is the issue at your end however this worked automatically for me. You can check the attached project for reference. 

Kind regards,
Vlad
the Telerik team

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

0
Mechthild
Top achievements
Rank 1
answered on 10 Oct 2011, 11:14 AM
Hi Vlad,
thanks for your answer.

In your example you are changing the objects in memory within the same entity framework context as the gridview is bound to.
We change the items of the database and want a kind of refreshing that reloads the database items into the grid. This happens in another context. You can simulate the situation with your example by replacing the button click handler with the following:

private void Button_Click(object sender, RoutedEventArgs e)
{
     NorthwindEntities context2 = new NorthwindEntities();
     context2.Customers.Where(c => c.CustomerID == "ALFKI" || c.CustomerID == "ANATR")
          .ToList().ForEach(c => c.CompanyName = "Test1");
     context2.SaveChanges();
     ((VirtualQueryableCollectionView)DataContext).Refresh();
}

Imagine that this - for example - represents a change in the database made by another user.
Our problem is that the refresh command in the last row has not the effect we want to have. The list still contains the old values.

Regards
Mechthild
0
Accepted
Vlad
Telerik team
answered on 13 Oct 2011, 07:38 AM
Hi Mechthild,

 I see your point now. 

Generally Refresh() method of VirtualQueryableCollectionView will re-execute the query however EF does not have something like "Reject Changes" and the changes will be kept on the client. You can check this using SQL profiler. 

You can check also these threads for more info:
http://stackoverflow.com/questions/3515561/entity-framework-3-5-reject-changes
http://social.msdn.microsoft.com/forums/en-US/adodotnetentityframework/thread/7c3dae78-bffe-487a-b20f-63f8c2c7cf00/

The closest option I can offer you is the create new ObjectContext:

public partial class MainWindow : Window
{
    NorthwindEntities context;
 
    public MainWindow()
    {
        InitializeComponent();
 
        context = new NorthwindEntities();
 
        DataContext = new VirtualQueryableCollectionView(context.Customers.OrderBy(c=>c.CustomerID)) { LoadSize = 20 };
    }
 
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        context.Customers.Where(c => c.CustomerID == "ALFKI" || c.CustomerID == "ANATR").ToList().ForEach(c => c.CompanyName = "Test");
 
        DataContext = new VirtualQueryableCollectionView(new NorthwindEntities().Customers.OrderBy(c=>c.CustomerID)) { LoadSize = 20 };
    }
}
Greetings,
Vlad
the Telerik team

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

0
Mechthild
Top achievements
Rank 1
answered on 13 Oct 2011, 02:22 PM
Thank you very much. It works :)
Tags
GridView
Asked by
Mechthild
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Mechthild
Top achievements
Rank 1
Share this question
or