Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
880
Grid goes nuts when refreshing the grids sort on BindingSource ListChanged event
posted

Hi,

When I add a new row to a grid I need to refresh the grid sort so that the new row appears in the correct position instead of it just being appended to the end of the grid.

Since I'm binding using a BindingSource object I'm using the ListCahnged event to detect that a new row has been added and then call ...DisplayLayout.Bands(0).SortColumns.RefreshSort(True) to perform the resort.

This works in that the row appears in the correct position, however it then shows one or more 'ghost' rows at the end of the grid. These rows are displayed but appear to be copies of the real rows.

I've attached a very simple example project which shows the problem, just press the Add Row button twice to see it go nuts.

Is there another event I can use that won't cause the grid problems or do I need a fix?

Thanks,

John.

  • 469350
    Verified Answer
    Offline posted

    Hi John,

    I ran your sample and I am seeing the same problem you are reporting.

    This may be a bug in the grid, but it's probably not something that can be easily addressed.

    I did not look into this in great detail, but the code you have here will never really be reliable, anyway. The reason is that the grid is responding to the same ListChanged notification that you are. The order of the event notifications is arbitrary - so there is no way to tell if the grid will get the notification first or if your code will.

    It's pretty obvious that in this sample, the grid is getting the notification first. If it was not, then your code to refresh the sort would have no effect at all. But it's still not a good idea to rely on the event order.

    To further complicate things, the grid responds to the ListChanged notifications asynchronously. So in your sample, the grid is probably just marking the rows collection dirty and then waiting for the next paint in order to retrieve the new data. And when you call RefreshSort, the grid is in a bad state and that's what is causing the problem. At least, that's my best guess to explain this behavior.

    So - the fact that the grid is not updating itself when you call RefreshSort may be a bug. But since the order of events is unreliable, anyway, this is probably not something we can address.

    The good news is that all you have to do to make this work is institute a little delay between the ListChanged notification and the refreshing of the sort order. You can do this very easily using a BeginInvoke.


        Private Sub BindingSource1_ListChanged(sender As Object, e As System.ComponentModel.ListChangedEventArgs) Handles BindingSource1.ListChanged
            Select Case e.ListChangedType
                Case System.ComponentModel.ListChangedType.ItemAdded

                    Me.UltraGrid1.BeginInvoke(New MethodInvoker(AddressOf Me.RefreshSort))

                    'Me.UltraGrid1.DisplayLayout.Bands(0).SortedColumns.RefreshSort(True)

            End Select
        End Sub

        Public Sub RefreshSort()
            Me.UltraGrid1.DisplayLayout.Bands(0).SortedColumns.RefreshSort(True)
        End Sub