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()
{
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)
{
List<CustomClass> lLocalDS = new List<CustomClass>();
lLocalDS = lDataSource.ToList<CustomClas>();
if (objCustom != null)
{
lLocalDS.Add(objCustom);
lDataSource.Add(objCustom);
}
grdControl.ItemsSource = null;
if (!flagClear)
{
grdControl.ItemsSource = lLocalDS;
}
}
This worked for me. Hope it will help anyone in similar case. You can find this article here.