2
votes

I am trying to write a 2 way binding application using C# winforms I have a BindingNavigator and a DataGridView bound to the same data source

So that I can have 2 way binding ( yes in winforms ) I want to be able to detect when the bindingSource sort order has changed.

When I click the column header of the DataGridView the sort order of the grid does change and the BindingSource.ListChanged event does fire

However the bindingSource.Sort remains null

When the grid column header is clicked the BindingSource.ListChanged event does fire

  private void bindingSource_ListChanged(object sender, ListChangedEventArgs e)
        {
            if (e.ListChangedType == ListChangedType.Reset)
            {
                if (bindingSource.Sort != null)
                {
                    controller.Sort = bindingSource.Sort;   // never gets here. Why?
                }
            }
        }

Why isn't clicking the DataGridView Column header setting the bindingSource.Sort property? What event should I be listening to in order to detect that the bindingSource sort has changed?

1

1 Answers

1
votes

Having come across this problem (for a different reason as in multi column sorting) I found it amazing that the Bindingsource doesn't have a sort changed event nor that the Datagridview's own Sorted event doesn't fire with it (it just seems to fire when it does the sorting).

The datagridview "knows" and even changes its sort glyph position.

The way I solved this won't suit most because it is specific to the task I am coding. Within my multi column sort routine I keep track of the bindingsource.sort string and then on a regular event within the Datagridview (currently trying SelectionChanged) I compare my sort string with the current Bindingsource one and switch off my multi-column sort indicators when it is different. This is ok for me because all my Datagrids are based on a common custom control.

The only other way I thought of would be to create a custom control based upon the Bindingsource and add a sort method (that calls the bindingsource sort method) and then trigger my own event. This would be fine if starting out on a new project but changing a large program becomes a pain.

It is sad given that I made a custom control from the start for every UI control (Winforms) but never thought to make a general one for Data Access.

Hope this helps someone facing the same issues.