Click here to Skip to main content
6,595,444 members and growing! (21,488 online)
Email Password   helpLost your password?
Platforms, Frameworks & Libraries » Windows Presentation Foundation » Data Binding     Beginner License: The Code Project Open License (CPOL)

Refreshing the view of grid control

By Ashish Sehajpal

How to reflect the changes made to the itemsource of grid control in wpf
XML, XHTML, WPF, LINQ, Dev
Posted:3 Sep 2008
Views:6,210
Bookmarked:11 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
9 votes for this article.
Popularity: 2.19 Rating: 2.29 out of 5
5 votes, 55.6%
1
1 vote, 11.1%
2

3

4
3 votes, 33.3%
5

Introduction

This is small article that will help solving the problem of refreshing the VIEW of listview/xceed grid etc after there is some modifications made to its itemsource

Using the code

Sometime WPF behaves very strange for normal developers as it is following newer phenomenon of coding standards. Same is the case when someone is trying to empty the grid [listview or xcdg grid or even the listbox] so as to show there are no records in the grid anymore.

The usual method followed for binding the grid control to data is something like this:

private void Bind()
{
// bind the grid with the globally declared data source i.e. List , List<T> or any generic collection
grdControl.ItemsSource = lDataSource;
}

But serves no purpose. In the beginning when we need to bind it first time i.e. in form constructor, it works well and fetched the result into the grid control. Fine.

But the very next time, like on some ADD NEW RECORD button, one may need to make the gridcontrol to show all the exisiting records as well as some new records that are added up into the DATASOURCE from UI i.e. on SUBMIT click, these records will be getting submitted to database. In that case, there is no chance that we are going to bind it with anything other than Current DATASOURCE collection object. So, one may need to make the control empty first and then re-bind the control to same object (new record has been added up into that object).

private void Bind()
{
grdControl.ItemsSource = null;
grdControl.ItemsSource = lDataSource;
}

But this will not make the grid control refresh its content. the main thing is that the ITEMSOURCE property only makes the control to have its source of data get declared, not the actual data gets fed into the control. Then the control read the data into its VIEW, purely xml like structure and that VIEW is reflected into the UI.

The simple solution to this problem is to bind the control to a locally declared collection instead if global one. So that the references to the datasource not get bound in actual to the control; means the control’s VIEW will be having the references to the locally declared collection only [If it is connected to global collection, it will not reflect the changes made to the collection].

So, the resultant code is:

private void Bind(CustomClass objCustom, bool flagClear)
{
  // declaring a local collection
  List<CustomClass> lLocalDS = new List<CustomClass>();
  // assigning the values from global datasource into the local one
  lLocalDS = lDataSource.ToList<CustomClas>();
  // if new record has been created from UI, then add it into both collections.
 if (objCustom != null)
 {
     lLocalDS.Add(objCustom);
     lDataSource.Add(objCustom);
 }
 // making the grid itemsource to point to NULL i.e. empty the grid
 grdControl.ItemsSource = null;
 // decide whether to bind the grid or not.
 // i.e. in RESET case, Object will be null & grid needs only empty refresh
 if (!flagClear)
  {
     grdControl.ItemsSource = lLocalDS;
  }
}

This worked for me. Hope it will help anyone in similar case. You can find this article here.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ashish Sehajpal


Member
http://www.linkedin.com/in/ashishsehajpal
Occupation: Web Developer
Company: http://www.linkedin.com/in/ashishsehajpal
Location: India India

Other popular Windows Presentation Foundation articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 3 of 3 (Total in Forum: 3) (Refresh)FirstPrevNext
GeneralOther way around PinmemberSnakiej9:20 28 Sep '08  
GeneralViewModel PinmemberwAnAec21:32 21 Sep '08  
GeneralObservableCollection PinmemberVlad Bezden9:06 10 Sep '08  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 3 Sep 2008
Editor:
Copyright 2008 by Ashish Sehajpal
Everything else Copyright © CodeProject, 1999-2009
Web09 | Advertise on the Code Project