Here I go:
I have a Winform Ultragrid databound to a bindingsource which is also bound to a bindingnavigator. Anything code related uses the bindingsource.current object. The Grid is set to SelectTypeRow = none so I only have to deal with one row at a time (the active row). Since it seems when you sort the Grid it does it independent of the underlying datasource, I have captured the sort event and perform a sort on the datasource as well.
When I advance the binding navigator the grid shows the correct active row highlighted. However, when I sort, the bindingnavigator shows lets say record 10 selected and the bindingsource.current shows record 10 selected, but the active row in the Grid is showing the "OLD" record 10 in the Grid. When you navigate up or down, the grid refreshes correctly to the next active record.
Example:
Sorted asc shows "BOB" active
Sorted desc should show "zippy" now, but still shows "bob" until you navigate which then selects the next record which is now "zippy 2" not "bob 2" (Hope I make sense here).
I guess my problem is: How do I get the grid to show the active row in sync with the current bindingsource and bindingnavigator on sort?
This problem does not exist if you use a DataGridView. Although I think the UltraGrid is superior in every way :)
Hi,
What do you mean when you say the grid "shows" the active record.Are you reading out the ActiveRow property of the grid programmatically, or are you talking about the visible highlighted row in the grid?
Sorting the grid will have no effect on the order of the rows in the data source, as you said. Sorting the grid rows also has no effect on the ActiveRow. The same row stays the ActiveRow. So it sounds to me like maybe you are doing something in your code to change the active row to the new row with the same index as the old row, but that you are not doing it in a way that updates the ActiveRow in the grid.
Hey Mike, thanks for your quick response. I guess I’m talking about the visible highlighted row.
What I did after I posted this message to correct the issue:
Programmatically grab the current row index, like you said during the BeforeSortChange event. Then during the AfterSortChange event, I set the ActiveRow in the Grid back to the same index. This is in an effort to keep everything in sync. BindingNavigator before sort is on index "x" Grid Before sort is on index “x” as well as the bindingsource. After sort the BindingNavigator is still on index "x" as with the bindingsource, now the "ActiveRow" in the grid is on index "y" because the ActiveRow moved to a new index on sort (which makes sense now)
I should point out that during the BeforeSortChange event I also sort the underlying DataSource. This also seemed necessary to keep the navigation in sync. (To prevent hoping around the grid on navigating)
Your right though, this is a display issue (visible highlighted row) and not a data issue since I only ever reference the BindingSource.Current record and pass that to my tabManager listener object.
The thing that bothers me is that after sort I have to run the following line of code to keep everything in sync. I guess it's not a big issue since there is no performance issue. I guess I'm wondering if there is a better way to do it.
//Active row index captured during BeforeSortChange
this.ultraGridEmployees.Rows[ActiveRowIndex].Activate();
And with this line of code and a smile from me, everything is well in the programming world again.
Thanks again Mike,
Sorry if I’m not explaining myself all that well.
As a side thought: I guess the debate is if it's better to keep the active row the same after sort and correct the navigator to prevent the hoping around during navigation, or if it's better to change the ActiveRow to keep in line with navigator (DataGridView behavior). If the user isn't watching, he\she might not notice the Active record changed and begin modifying the wrong person.
Hi Roy,
I'm still a little fuzzy on what you want to achieve. As I sad earlier, when you sort the grid, the ActiveRow does not change, nor does the current Position of the BindingManager. The ActiveRow might be in a new location, but it's still the same row.
So it sounds like what's happening here is that the grid is correct, but when you sort the data source, the BindingNavigator is not showing the correct new index of the same active row.
So to solve that problem, what you are doing is changing the ActiveRow to the row that has the same index as the previous active row. This way the index stays the same and the BindingManager is correct, but you changed the ActiveRow.
It's not my application, of course, but if it were, I would probably not go that route. A user probably doesn't expect the active row to change when he sorts the grid. If the user was in the middle of editing the row and sorted, they probably would not expect their changes to be committed at that point. And what if an error occurs because the data they changed is invalid? Maybe I am wrong, maybe the issues I just mentioned would exist anyway. The grid might force an update when you sort regardless. But it just seems like an unusual UI, in my opinion.
I would try to explore why the BindingNavigator is out of synch with the data and see if there some way to refresh it so it shows the correct index.