I've a master data form with an XtraGrid and some editors to edit the data of the currently selected item. This form also has a "Refresh" button which reloads the displayed items from the database to reflect changes made by other users. The XtraGrid has a System.Windows.Forms.BindingSource as DataSource.
Now the problem. Let's say row 2 out of 5 if currently selected (focused). After hitting refresh I would expect that the same item (probably but not always at row 2) is still focused. This indeed works as expected as long as no sorting is active. But with sorting in place a wrong item is focused.
I've reproduced the problem with a small demo program and also attached a flash video trying to explain what's going on.
As far as I understand the XtraGrid does try to find the previously focused item. But it seems to me that the wrong item it used for comparison. Let's say I have an unsorted datasource with the items 'A', 'Z' and 'M'. Sorted it is displayed as 'A', 'M' and 'Z'. Now if the second row is focused (which displays 'M') and the datasource is updated (e.g. newly created and assigned but with the same elements) the focused row will be the one that displays 'Z'. Interestingly, 'Z' was the second item of the unordered datasource. So apparently the wrong item is used to restore the focused wrong.
Steps to Reproduce:
Start attached sample program. It shows an unsorted grid. Now sort the column and select the item 'M'. Then click on "Update DataSource". Now, item 'O' is focused. In displayed order, 'M' was at index 2. In the underlying, unordered bindingsource, 'O' was at index 2 which is now the focused item.
Focused item before updating the datasource is still the focused item after the update.
I'm sorry but I disagree. As far as I understand your code (and I can only look at it using reflector) there is a class CurrentAndSelectedRowsKeeper that's specifically trying to store the selected and focused rows and to restore them after an update. And it does work as long as the view displays the data unordered (thus it's the same order as in the underlying datasource). The focused row is maintained.
It does not work as soon as the view displays the data ordered (e.g. alphabetically). The CurrentAndSelectedRowsKeeper seems to get confused. It doesn't seem to respect the ordered state. In fact I think I'm able to describe whats going on. Let's say I focus the 3nd row (= index 2 of the ordered elements). But it looks like the value that is stored before the update is the one on the second index of the original (unordered) datasource. After the update of the datasource the then focused row is always exactly the one with _this_ previously stored value.
I'm sorry, it's hard to describe it but I was hoping that the attached flash video would show.
This is why I think that the gridview is trying to focus the "correct" row after an update but it just uses a wrong value to find the desired row.
Could you please look into that again?
BTW: I agree that the "correct" row cannot be determined as long as the data is not unique. But in my case the objects in the datasource are unique so I could write code that finds the row which displays the desired data object and set it as the focused row after an update. But I would expect the grid to do this for me. Or at least there should be an option for this.
A colleague using your Delphi components told me that there exists a function called "IsRecordIdSupported" (along with "GetRecordId") on TcxCustomDataSource for this purpose.
Thanks a lot,
Please note that in WinForms record key values are not provided. It's not a grid's bug definitely. In your instance please handle the GridView.FocusedRowChanged event obtain an underlying record using the GridView.GetRow() method and restore the cursor as you need when a grid's data source is changed.
Is your intention to post an answer to your own question?
- If so, then proceed.
- If you simply wanted to post additional information, ask for further clarification, or to just say "Thanks!", please click Leave a Comment.
- If you wish to edit your original question, please use the Edit button in the Toolbox at the top right corner of that entry.