Click here to Skip to main content
15,884,298 members
Articles / Web Development / XHTML
Article

Refreshing the view of grid control

Rate me:
Please Sign up or sign in to vote.
2.29/5 (9 votes)
3 Sep 2008CPOL2 min read 27.5K   15   3
How to reflect the changes made to the itemsource of grid control in wpf

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)


Written By
Web Developer http://www.linkedin.com/in/ashishsehajpal
India India
http://www.linkedin.com/in/ashishsehajpal

Comments and Discussions

 
GeneralOther way around Pin
Snakiej28-Sep-08 8:20
Snakiej28-Sep-08 8:20 
GeneralViewModel Pin
wAnAec21-Sep-08 20:32
wAnAec21-Sep-08 20:32 
GeneralObservableCollection<t></t> Pin
Vlad Bezden10-Sep-08 8:06
Vlad Bezden10-Sep-08 8:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.