使用DataTable进行检索和排序示例
2006-11-18 15:10
501 查看
DataBind.aspx如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<form id="form1" runat="server">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<div>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<asp:Label ID="DisplayLabel" runat="server" Text="Label"></asp:Label></div>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</form>
DataBind.aspx.cs如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Data;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Configuration;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Collections;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.Security;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.WebControls;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.WebControls.WebParts;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.HtmlControls;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Data.SqlClient;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public partial class DataTableExample : System.Web.UI.Page
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected void Page_Load(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string connStr = ConfigurationManager.ConnectionStrings["northwind"].ConnectionString;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SqlConnection conn = new SqlConnection(connStr);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
conn.Open();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SqlDataAdapter da = new SqlDataAdapter("SELECT CustomerID, CompanyName, Country FROM Customers", conn);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DataTable dt = new DataTable();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
da.Fill(dt);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 定义筛选条件字符串和排序字符串
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string strExpr = "country = 'usa'";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string strSort = "companyname desc";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 获得经过筛选和排序后的数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DataRow[] resultRows = dt.Select(strExpr, strSort);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 显示经过筛选和排序后的数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Display_Label(resultRows, DisplayLabel);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 显示DataRow数组中的内容
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void Display_Label(DataRow[] rows, Label label)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 检查返回数据是否为空
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (rows.Length < 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
label.Text = "没有数据";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
label.Text = "";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 遍历DataRow数组的行和列,显示数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
label.Text += "<Table border='1'>";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
label.Text += "<TR><TH>CustomerID</TH><TH>CompanyName</TH><TH>Country</TH></TR>";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
foreach (DataRow row in rows)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
label.Text += "<tr>";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (int i = 0; i < row.Table.Columns.Count; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
label.Text += "<td>";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
label.Text += row[i];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
label.Text += "</td>";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
label.Text += "</tr>";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
label.Text += "</table>";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<form id="form1" runat="server">
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<div>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
<asp:Label ID="DisplayLabel" runat="server" Text="Label"></asp:Label></div>
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
</form>
DataBind.aspx.cs如下:
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Data;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Configuration;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Collections;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.Security;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.WebControls;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.WebControls.WebParts;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Web.UI.HtmlControls;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
using System.Data.SqlClient;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
public partial class DataTableExample : System.Web.UI.Page
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
protected void Page_Load(object sender, EventArgs e)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string connStr = ConfigurationManager.ConnectionStrings["northwind"].ConnectionString;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SqlConnection conn = new SqlConnection(connStr);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
conn.Open();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
SqlDataAdapter da = new SqlDataAdapter("SELECT CustomerID, CompanyName, Country FROM Customers", conn);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DataTable dt = new DataTable();
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
da.Fill(dt);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 定义筛选条件字符串和排序字符串
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string strExpr = "country = 'usa'";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
string strSort = "companyname desc";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 获得经过筛选和排序后的数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
DataRow[] resultRows = dt.Select(strExpr, strSort);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 显示经过筛选和排序后的数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
Display_Label(resultRows, DisplayLabel);
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 显示DataRow数组中的内容
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
public void Display_Label(DataRow[] rows, Label label)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 检查返回数据是否为空
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
if (rows.Length < 0)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
label.Text = "没有数据";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
return;
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
label.Text = "";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
// 遍历DataRow数组的行和列,显示数据
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
label.Text += "<Table border='1'>";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
label.Text += "<TR><TH>CustomerID</TH><TH>CompanyName</TH><TH>Country</TH></TR>";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
foreach (DataRow row in rows)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
label.Text += "<tr>";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
for (int i = 0; i < row.Table.Columns.Count; i++)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif)
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif)
...{
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
label.Text += "<td>";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
label.Text += row[i];
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
label.Text += "</td>";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
label.Text += "</tr>";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif)
label.Text += "</table>";
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif)
}
![](http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif)
相关文章推荐
- 如何对DataTable进行检索和排序
- DataTable数据进行排序、检索、合并、分页、统计 [转]
- 如何对DataTable进行检索和排序
- asp.net中对DataTable数据进行排序、检索、合并、分页、统计
- DataTable数据进行排序、检索、合并、分页、统计
- DataTable数据进行排序、检索、合并、分页、统计
- [转]asp.net中对DataTable数据进行排序、检索、合并、分页、统计
- asp.net中对DataTable数据进行排序、检索、合并、分页、统计
- Java使用二分法进行查找和排序的示例
- asp.net中对DataTable数据进行排序、检索、合并、分页、统计
- 如何对DataTable进行检索和排序
- 如何对DataTable进行检索和排序
- DataTable数据进行排序、检索、合并、分页、统计
- 如何对DataTable 进行检索和排序
- [转]asp.net中对DataTable数据进行排序、检索、合并、分页、统计
- 如何使用 DataView 进行数据排序和检索
- C#中使用基数排序算法对字符串进行排序的示例
- 如何对DataTable进行检索和排序
- asp.net中对DataTable数据进行排序、检索、合并、分页、统计
- 如何使用DataView进行数据排序和检索