I have a grid with a lot of things happening(formulas etc...)... unfortunately there are times when the grid can only do so much and I need to dump the data source, massage it, then put it back into the grid. I do this in the following way:
var productLines = (List<CostModel>)grdMain.DataSource; // ... do some data massaging...grdMain.ResetLayouts();grdMain.DisplayLayout.Bands[0].Reset();grdMain.DataSource = null;grdMain.DataSource = productLines;
This seems to work, but I am wondering if this is the correct way of dealing with my data needs? My concern is making sure all the formulas and stuff are reset. Plus I dont want to cause any potential problems using the grid as this grid is heavily used and other grids are dependent upon the data contained therein.
So I guess I just need to know the preferred method of refreshing a grids data source.
thanks
Hi,
This seems like overkill to me. But it really depends on exactly what you are doing to the data source.
In a typical case, the grid will automatically respond to changes in the data source, so you shouldn't need to do this. In your case, however, I noticed that your DataSource is a List<T>, which is not an ideal data source to use and doesn't provide all of the proper notifications for data binding. If you can, I recommend using BindingList<T>, instead. It derives from List<T>, so you shouldn't have to change any of our code, and it will provide a better experience when binding to the grid.
Of course, there are still some things you could do to your data source that the grid might not be able to detect (like sorting, for example). So in that case, a less destructive way of refreshing the grid is to call:
grid.Rows.Refresh(ReloadData)
This will tell the grid to reload all of the data (but will not affect the data structure, bands, or columns). It's a lot more performant than what you are currently doing.
Hi Mike,
I'm Using a windows form Ultragrid with data table as Datasource and when user clicks refresh Data table will be refreshed and I'm binding datatable back to Grid and not getting refreshed after binding data source.
regards,
harish'
I did not know about the BindingList<T> so I will look into that. The other .Refresh(ReloaData) I will definitely put to use. Thank you for the insight!!