How do I implement automatic sorting of DataGridView?

前端 未结 3 2291
一个人的身影
一个人的身影 2020-11-27 19:53

I am programmatically adding columns to a DataGridView and then binding to a list. By default, the SortMode of the columns are Automatic. But when I run my app, clicking o

3条回答
  •  孤独总比滥情好
    2020-11-27 20:15

    We use BindingListView to bind Lists to DataGridViews, and it's worked beautifully for us.

    Here is a very simple example of creating a view of a list of objects (in C#):

    List customers = GetCustomers();
    BindingListView view = new BindingListView(customers);
    dataGridView1.DataSource = view;
    

    Check out https://stackoverflow.com/a/17952576/116891 for a lot more details about DGV sorting and databinding.

    If you don't want to add something that heavy, you can try this implementation of a SortableBindingList (with updates).

    Both give you sorting right out of the box, and BindingListView is even faster than DataViews, according to their benchmarks.

提交回复
热议问题