简体   繁体   中英

Datagridview in C# - custom sorting of column

I have a databound (Datatable) datagridview in C#. The datatable is filled with the Load(IDataReader) method. The user is able to sort the columns of the DGV by clicking the header. Now I have one column (which originally in the database is a text-column) for which I would like to implement a custom sort method.

I know about the possibilities described in the MSDN-article about custom sorting of datagridviews and as the SortCompare event is not useful for databound grids, I would like to run another path:

I thought of implementing a simple own datatype which implements the IComparable-interface. Then I would set the datatype for the concerning column in the datatable to this new datatype. If the user clicks the header, the compare-method of the datatype would be called. I like this method because it would be reusable for other datagridviews/ datatables too.

Is this possible so far or have I misunderstood something? The problem for me now is how to tell the datareader to convert the text-values from the database to my new datatype and fill the datatable with objects of this datatype.

see this: DataGridView Column sorting with Business Objects

or: http://www.timvw.be/2007/02/22/presenting-the-sortablebindinglistt/ http://www.timvw.be/2008/08/02/presenting-the-sortablebindinglistt-take-two/

either one of them will get you where you want to go. you'r idea is basicly right, only that you need the IComparer as well, the IComparable alone is not enough.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM