Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Refreshing the view of grid control

0.00/5 (No votes)
3 Sep 2008 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here