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
560
Sync Problem with BindingList Reorder
posted

Problem:

I am having problems getting the WinGrid to update after I reorder the BindingList that is bound to the grid. 

 

Background:

I have a WinGrid with a BindingSource as its data source. 

                ultraGrid1.DataSource = BindingSource

I am using the BindingSource so that I can properly notify the grid that values in the BindingList objects have changed using INotifyPropertyChanged.

 

The BindingSource has a DataSource of a BindingList<mySimpleObject>

BindingSource.DataSource = BindingList<mySimpleObject>

 

I have 15 rows in the grid that are number sequentially from 1 – 15:

 

I select the first row and drag/drop it down between the 8th and 9th rows:

 

I added code to loop through my BindingList<mySimpleObject> to write out the sequence numbers.  Before the changes from the move, this is the ordering of the items in my BindingList:

****************** Before Move

Seq: 1

Seq: 2

Seq: 3

Seq: 4

Seq: 5

Seq: 6

Seq: 7

Seq: 8

Seq: 9

Seq: 10

Seq: 11

Seq: 12

Seq: 13

Seq: 14

Seq: 15

 

 

Now I need to reorder the items in the list.  I first store the mySimpleObject that I am moving in a local variable.  Then I remove the item from the BindingList<mySimpleObject>:

                BindingList.Remove(objectBeingMoved);

Next, I insert the object into the correct location:

                BindingList.Insert(locIndex, objectBeingMoved);

 

After this, I loop across the BindingList again, writing out the sequence numbers.  I get this output, which is what I would expect:

****************** After Move

Seq: 2

Seq: 3

Seq: 4

Seq: 5

Seq: 6

Seq: 7

Seq: 8

Seq: 1

Seq: 9

Seq: 10

Seq: 11

Seq: 12

Seq: 13

Seq: 14

Seq: 15

 

My problem, though, is this:

As you can see, even though the sequence of the mySimpleObject items in the BindingList are ordered as I would expect when looping through the list, the moved item is moved all the way to the bottom of the grid.  I am not doing any row refreshes, although I did try grid.Rows.Refresh(RefreshRow.ReloadData, true), but to no avail.

 

At this point, the only way that I can get the grid to refresh properly is to completely dump and then rebind the grid, like this:

Grid.DataSource = null;

Grid.DataSource = BindingSource;

There are at least two problems with this.  First, It is terribly inefficient, and second it causes all of the sorts, filters, and column sizing to be reset. 

 

One more data point…  I wrote a simple application where I just put a grid and a button on a form to mimic this.  I wanted to distill this to its simplest elements.  Here is the relevant code:

        public Form1()

        {

            InitializeComponent();

            dataItems = new BindingList<DataItem>();

            dataSource = new BindingSource();

            dataSource.DataSource = dataItems;

        }

 

        private void Form1_Load(object sender, EventArgs e)

        {

            for (int i = 1; i < 11; i++)

            {

                var dataItem = new DataItem(i);

                dataItems.Add(dataItem);

            }

 

            ultraGrid1.DataSource = dataSource;

        }

 

        private void ultraButton1_Click(object sender, EventArgs e)

        {

            DataItem temp = dataItems[0];

            dataItems.RemoveAt(0);

            dataItems.Insert(5, temp);

        }

 

This code worked as I would have expected and the grid did reflect the new ordering of the objects in the BindingList.

 

Questions:

At this point, I am perplexed as to why my BindingList order looks correct, but the moved item in the BindingList always shows up at the bottom of the grid.  So, I have three questions:

1.       Does anyone know what might be causing my grief with the BindingList and the grid not being in sync?

2.       If I have to rebind the BindingList to the grid after every drag/drop row operation (worst-case scenario), is there some easy way to save off the filters, column widths, etc so that I can reapply them after rebinding the BindingList?

3.       (Best case scenario) – Is there some hidden functionality in the WinGrid that I have overlooked where I just flip a bit and row moving comes for free, automagically updating my BindingList for me? If not, why not?  This would seem to be common functionality.

Parents Reply Children
No Data