How to search and sort DataTable

Source: Internet
Author: User

Display result

CustomerID CompanyName Country
WHITC White Clover Markets USA
TRAIH Trail's Head Gourmet Provisioners USA
THECR The Cracker Box USA
THEBI The Big Cheese USA
SPLIR Split Rail Beer & Ale USA
SAVEA Save-a-lot Markets USA
RATTC Rattlesnake Canyon Grocery USA
OLDWO Old World Delicatessen USA
LONEP Lonesome Pine Restaurant USA
LETSS Let's Stop N Shop USA
LAZYK Lazy K Kountry Store USA
HUNGC Hungry Coyote Import Store USA
GREAL Great Lakes Food Market USA

Source code


<% @ Import Namespace = "System. Data" %>
<% @ Import Namespace = "System. Data. SqlClient" %>
<HTML>
<HEAD>
<Title> example of searching and sorting by DataTable </title>
<Script language = "C #" runat = "server">

Void Page_Load (object sender, System. EventArgs e)
{
String ConnectionString = System. Configuration. ConfigurationSettings. etettings ["ConnectionSqlServer"];
String SQL = "SELECT CustomerID, CompanyName, Country FROM Customers ";

SqlConnection thisConnection = new SqlConnection (ConnectionString );
SqlDataAdapter adapter = new SqlDataAdapter (SQL, thisConnection );

// Create a able object
DataTable table = new DataTable ();

// Fill in the data to the DataTable
Adapter. Fill (table );

// Define the filter condition string and sort string
String strExpr = "Country = 'usa '";
String strSort = "CompanyName DESC ";

// Obtain filtered and sorted data
DataRow [] resultRows = table. Select (strExpr, strSort );

// Display filtered and sorted data
DisplayRows (resultRows, DisplayLabel );
}

// Display the content in the DataRow Array
Public void DisplayRows (DataRow [] rows, Label label)
{
// Check whether the returned data is empty
If (rows. Length <= 0)
{
Label. Text = "no data ";
Return;
}
Label. Text = "";

// Traverse the rows and columns of the DataRow array to display data
Label. Text + = "<Table border = '1'> ";
Label. Text + = "<TR> <TH> CustomerID </TH> <TH> CompanyName </TH> <TH> Country </TH> </TR> ";
Foreach (DataRow row in rows)
{
Label. Text + = "<TR> ";
For (int I = 0; I <row. Table. Columns. Count; I ++)
{
Label. Text + = "<TD> ";
Label. Text + = row [I];
Label. Text + = "</TD> ";
}
Label. Text + = "</TR> ";
}
Label. Text + = "</Table> ";
}

</Script>
</HEAD>
<Body>
<Form id = "Form1" method = "post" runat = "server">
<H3> example of searching and sorting by DataTable </H3>
<Asp: Label id = "DisplayLabel" runat = "server"> Label </asp: Label>
</Form>
</Body>
</HTML> for the DataTable. Select method [C #], see

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.