Gridview uses DataView objects to sort and filter data

Source: Internet
Author: User
// Open the database con. Open ();
// SQL statement
String oSql = "select id, name, station, level, dept from verify ";
// Extract data from the database
SqlDataAdapter da = new SqlDataAdapter (oSql, con );
// Create a set object
DataSet ds = new DataSet ();
// Load the extracted data from the database to the record set
Da. Fill (ds, "verify ");
// Close the database
Con. Close ();
// Obtain the table object from the record set
DataTable dt = ds. Tables ["verify"];
// Set the condition string to be filtered
String filterExpression = "dept = 'group '";
// Set the fields to be sorted and the sorting method string
String sortExpression = "name DESC ";
// Set row state filtering to the original row
DataViewRowState
RowStateFilter = DataViewRowState. OriginalRows;

// DataView dv = new DataView (dt, filterExpression, sortExpression, rowStateFilter );
// Create a DataView object and PASS Parameters to its constructor
// Set the attributes of Table, RowFilter, Sort, and RowStateFilter after DataView is generated.
// Step-by-step settings
// Create a DataView object
DataView dv = new DataView ();
// Obtain data
Dv. Table = dt;
// Set filter attributes
Dv. RowFilter = filterExpression;
// Set the sorting attribute
Dv. Sort = sortExpression;
// Perform Filtering
Dv. RowStateFilter = rowStateFilter;
// Cyclically retrieve filtered data and output it on the page
Foreach (DataRowView drv in dv)
{
For (int I = 0; I <drv. Row. Table. Columns. Count; I ++)
{
Response. Write (drv [I] + "<br> ");
}
}

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.