简体   繁体   中英

multi column sorting of datagrid view:

I have a datagridview in a windows form with 3 columns: Serial number, Name and Date-Time.

The Name column will always have either of the two values: "name1" or "name2".

I need to sort these columns such that the grid displays all the rows with name values in a specific order (first display all the "name1" rows and then all the "name2" rows). Within the "name1" rows, I want the rows to be sorted by the Date-Time. Please note programmatically, all the 3 columns are strings.

For example, if I have the rows:

01 |Name1 | 2010-05-05 10:00 PM
02 |Name2 | 2010-05-02 08:00 AM
03 |Name2 | 2010-05-01 08:00 AM
04 |Name1 | 2010-05-01 11:00 AM
05 |Name1 | 2010-05-04 07:00 AM

needs to be sorted as

04 |Name1 | 2010-05-01 11:00 AM
05 |Name1 | 2010-05-04 07:00 AM
01 |Name1 | 2010-05-05 10:00 PM
03 |Name2 | 2010-05-01 08:00 AM
02 |Name2 | 2010-05-02 08:00 AM

I am not sure how to go about using the below:

    myGrid.Sort(.....,ListSortDirection.Ascending)

Define your sort expression as string that contains a comma-separated list of field names. Have a look for example here http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.sort.aspx

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