Atari2600
Atari2600

Reputation: 2813

C# DataGridView bound to SortedBindingList not showing sort

I have implemented the SortedBindingList class shown at http://www.martinwilley.com/net/code/forms/sortablebindinglist.html

I have then successfully added objects of the same type to it.

I have a DataGridView on a form with a BindingSource that is set to the list. When I click a column header, the list sorts in the SortedBindingList object, however that change is not reflected in the DataGridView. Is there a property on the DataGridView I need to set or an event I need to subscribe to?

SortedBindingList<CustomObject> list = new SortedBindingList<CustomObject>();
//Add Objects to list
CustomObjectBindingSource.DataSource = list; 
dataGridViewSource1.DataSource = CustomObjectBindingSource; 

If I put a breakpoint in the SortedBindingList code in the Compare method where it returns the result, I can see that the list is sorted appropriately, just the DataGridview does not show the list as being sorted. What am I missing?

Thanks

Upvotes: 0

Views: 3037

Answers (1)

Bala R
Bala R

Reputation: 109037

Edit: Sorry! for some reason I thought it was asp.net

In your case, you need

   BindingSource bs = new BindingSource();
   bs.DataSource = bs;
   dataGridView.DataSource = bs;

EDIT 2:

I just tried this using the SortableBindingList class like this

    SortableBindingList<Person> list = new SortableBindingList<Person>();
    list.Add(new Person{Name = "abc",Email = "def"});
    list.Add(new Person { Name = "bcd", Email = "aqz" });
    dataGridView1.DataSource = list;

for

    public class Person
    {
        public string Name { get; set; }
        public string Email { get; set; }
    }

and it works for me.

Upvotes: 1

Related Questions