Click here to Skip to main content
12,949,858 members (96,669 online)
Click here to Skip to main content
Add your own
alternative version


15 bookmarked
Posted 3 Sep 2008

Refreshing the view of grid control

, 3 Sep 2008 CPOL
Rate this:
Please Sign up or sign in to vote.
How to reflect the changes made to the itemsource of grid control in wpf


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)
 // 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.


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


About the Author

Ashish Sehajpal
Web Developer
India India

You may also be interested in...


Comments and Discussions

GeneralOther way around Pin
Snakiej28-Sep-08 8:20
memberSnakiej28-Sep-08 8:20 
GeneralViewModel Pin
wAnAec21-Sep-08 20:32
memberwAnAec21-Sep-08 20:32 
GeneralObservableCollection<t></t> Pin
Vlad Bezden10-Sep-08 8:06
memberVlad 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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170524.1 | Last Updated 3 Sep 2008
Article Copyright 2008 by Ashish Sehajpal
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid