C # Sort data by columns or columns

Source: Internet
Author: User

I have been joining csdn for a while and have never written anything on my blog. Today, I am weak enough to write a simple question about my learning, this is just for the kids shoes that are just like me it cainiao, so you don't have to read them. When binding data, if you do not want to use an SQL statement to sort the bound data, you want to use the sort method of dataview. In winform, The datagridview is sorted by a column. Although it is simple, I hope you do not make a brick. After all, it is also knowledge. This method does not need to write the sorting code by yourself. You only need to set the sort attribute of defaultable's custom view defaultview to sort the fields you want to sort when binding data, you can set not only one column, but also multiple columns. This sorting method is actually the sort method of dataview, which is completed by fast sorting. A simple example is provided,

Sort by Column
Datatable dt = new datatable ();
String selsql = string. Format ("select serialnum as serial number, patientname as patient name, checkdate as check date from checkrecord ");
// Sort by the check date column. Here is just an alias for the checkdate name. If you do not have an alias DT. defaultview. Sort = "checkdate ";
Dt = business. BLL. runsqldataset (selsql). Tables [0];
DT. defaultview. Sort = "check date ASC ";
This. dgv_patientname.datasource = DT;

Sort by multiple columns. They are all the same. Column fields are separated by commas.
Datatable dt = new datatable ();
String selsql = string. Format ("select serialnum as serial number, patientname as patient name, checkdate as check date from checkrecord ");
// Sort the two columns by the serial number and check the date
Dt = business. BLL. runsqldataset (selsql). Tables [0];
DT. defaultview. Sort = "serial number ASC, check date ASC ";
This. dgv_patientname.datasource = DT;

In short, you can sort by which column or which column you want, and sometimes the SQL statement is too long to be sorted by using the above method, and this is suitable for Multiple Data Control bindings.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.