C# DataGridView sorting with Generic List as underlying source

前端 未结 9 1849
情书的邮戳
情书的邮戳 2020-12-05 03:22

I\'m using a Windows Forms DataGridView to display a generic list of MyObject objects.

First of all I wrap this collection into a

9条回答
  •  臣服心动
    2020-12-05 03:31

    Another option to solve the sorting issue with DataGridView when binding to List is, if you are not dealing with huge data set then probably you can attempt to convert the List to DataTable, and then bind the resulting DataTable to the BindingSource/DataGridView.

    This would the need for a custom implementation of IComparer. In my case, I was dealing with a smaller list but there were more fields to display. So implementing IComparer meant writing too much boiler plate code.

    Check this for terse way of converting the List to DataTable: https://stackoverflow.com/a/34062898/4534493

提交回复
热议问题