Was not sure how to phrase my search for help on this, so I have failed miserably... I have a grid with 3 bands of related data (bucketed) and for example, a user drills-in and selects a record in the third band. Other processing takes place that changes the data in the database. The user is prompted if they want the data in the grid refreshed to see the impact of the action. Once refreshed, the user has to remember which record they had previously selected and they begin to hunt (drill back into the data) to get back to where they left off prior to the refresh.
Do to the complexity of the data, the calc engine, etc., it is not an option to update the data bound to the grid - the data must be re-fetched from the database.
In short, how I do programmatically get them back to the record they selected?
Many thanks!
Hi,
This is a pretty common question, actually. The answer is, there is no way the grid can do this.
When you re-bind the grid, you are giving it a whole new set of row objects. So there's no way for the grid to associate the old rows with the new ones.
If your data includes a Primary Key, then you could store the current state of the grid, then try to go find the same keys in the new data. Of course, there is always the possibility that the key no longer exists if that row was deleted, so I don't know what behavior you would want in that case.
Hey Mike,
Figured this would be the case, but the good news is that we do have a key that will never be deleted even after a refresh. It will also appear in the same band after the refresh. Does this mean the goal is within reach?
What you would have to do is store the key somewhere, then after you re-bind the grid, loop through the rows to find the correct one and then activate it.
This could be a bit tricky if you have multiple bands, since you will need to store the keys of the parent rows all the way up the chain.
If you are using OutlookGroupBy, then you will also need to store the value of each GroupByRow.
Having at most 3 bands, this is what works great for me.
private
List<string> _rowKeys = new List<string>();
List<string> _bandKeys = new List<string>();
The code within my InitializeRow event:
if (e.Row.Band.Index == 0){ e.Row.CellAppearance.BackColor = Color.LightBlue;
// Expand all levels at this level if the user had selected data so we can get back to what they were working on if (_bandKeys.Contains(e.Row.Cells[0].Text)) { e.Row.ExpandAll(); // This is where they were working, so expand all }}if (e.Row.Band.Index == 1 & _myDataSet.Tables.Count > 2){ e.Row.CellAppearance.BackColor = Color.PaleTurquoise;}
if (e.Row.Band.Columns.Exists("ID")){ if (_rowKeys.Contains(e.Row.Cells["ID"].Text)) { // mark what they just processed e.Row.Appearance.BackColor = Color.FromArgb(255, 255, 192); } else // reset back color { e.Row.Appearance.ResetBackColor(); }}