C# DataGridView sorting with Generic List as underlying source

前端 未结 9 1838
情书的邮戳
情书的邮戳 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:54

    I find it hard to believe the grid doesn't provide basic sorting out of the box, no code needed. After all, it is pretty silly to have to handle a header click event and call DataGridView.Sort indicating the column (determined by what was clicked, tracked by the grid) and the sort direction (determined by current sort state, tracked by the grid).

    Why isn't there simply a SortMode or an AllowUserToSort property that does exactly the same thing by default?

    I've bound my grid to a List and the properties I've mapped columns to are all basic types like string, int, DateTime and so on. All of which are IComparable. So why on earth should I need to write even one line of code? Especially considering that the documentation reads:

    By default, users can sort the data in a DataGridView control by clicking the header of a text box column.

    MSDN

    That's the Framework 3.0 doc, and I'm targeting 3.5, but "other versions" all refer to versions of Visual Studio, not versions of the Framework. What on earth is going on here Microsoft?!?

提交回复
热议问题