如何对DataTable进行检索和排序

显示结果

 

CustomerIDCompanyNameCountry
WHITCWhite Clover MarketsUSA
TRAIHTrail's Head Gourmet ProvisionersUSA
THECRThe Cracker BoxUSA
THEBIThe Big CheeseUSA
SPLIRSplit Rail Beer & AleUSA
SAVEASave-a-lot MarketsUSA
RATTCRattlesnake Canyon GroceryUSA
OLDWOOld World DelicatessenUSA
LONEPLonesome Pine RestaurantUSA
LETSSLet's Stop N ShopUSA
LAZYKLazy K Kountry StoreUSA
HUNGCHungry Coyote Import StoreUSA
GREALGreat Lakes Food MarketUSA


源代码

None.gif
None.gif
<%  @Import Namespace = " System.Data "   %>
None.gif
<%  @Import Namespace = " System.Data.SqlClient "   %>
None.gif
< HTML >
None.gif    
< HEAD >
None.gif        
< title > 使用DataTable进行检索和排序示例 </ title >
None.gif        
< script language = " C# "  runat = " server " >
None.gif
None.gif            
void  Page_Load( object  sender, System.EventArgs e)
ExpandedBlockStart.gif            
{
InBlock.gif                
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["ConnectionSqlServer"];
InBlock.gif                
string Sql = "SELECT CustomerID, CompanyName, Country FROM Customers";
InBlock.gif
InBlock.gif                SqlConnection thisConnection 
= new SqlConnection(ConnectionString);
InBlock.gif                SqlDataAdapter adapter 
= new SqlDataAdapter(Sql, thisConnection);
InBlock.gif
InBlock.gif                
// 创建DataTable对象
InBlock.gif
                DataTable table = new DataTable();
InBlock.gif
InBlock.gif                
// 填充数据到DataTable
InBlock.gif
                adapter.Fill(table);
InBlock.gif
InBlock.gif                
// 定义筛选条件字符串和排序字符串
InBlock.gif
                string strExpr = "Country = 'USA'";
InBlock.gif                
string strSort = "CompanyName DESC";
InBlock.gif
InBlock.gif                
// 获得经过筛选和排序后的数据
InBlock.gif
                DataRow [] resultRows = table.Select(strExpr, strSort);
InBlock.gif
InBlock.gif                
// 显示经过筛选和排序后的数据
InBlock.gif
                DisplayRows(resultRows, DisplayLabel);
ExpandedBlockEnd.gif            }

None.gif            
None.gif            
//  显示DataRow数组中的内容
None.gif
             public   void  DisplayRows(DataRow [] rows, Label label)
ExpandedBlockStart.gif            
{
InBlock.gif                
// 检查返回数据是否为空
InBlock.gif
                if(rows.Length <= 0)
ExpandedSubBlockStart.gif                
{
InBlock.gif                    label.Text 
= "没有数据";
InBlock.gif                    
return;
ExpandedSubBlockEnd.gif                }

InBlock.gif                label.Text 
= "";
InBlock.gif
InBlock.gif                
// 遍历DataRow数组的行和列,显示数据
InBlock.gif
                label.Text += "<Table border='1'>";
InBlock.gif                label.Text 
+= "<TR><TH>CustomerID</TH><TH>CompanyName</TH><TH>Country</TH></TR>";
InBlock.gif                
foreach(DataRow row in rows)
ExpandedSubBlockStart.gif                
{
InBlock.gif                    label.Text 
+= "<TR>";
InBlock.gif                    
for(int i=0; i<row.Table.Columns.Count; i++)
ExpandedSubBlockStart.gif                    
{
InBlock.gif                        label.Text 
+= "<TD>";
InBlock.gif                        label.Text 
+= row[i];
InBlock.gif                        label.Text 
+= "</TD>";
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    label.Text 
+= "</TR>";
ExpandedSubBlockEnd.gif                }

InBlock.gif                label.Text 
+= "</Table>";
ExpandedBlockEnd.gif            }

None.gif
None.gif        
</ script >
None.gif    
</ HEAD >
None.gif    
< body >
None.gif        
< form id = " Form1 "  method = " post "  runat = " server " >
None.gif            
< H3 > 使用DataTable进行检索和排序示例 </ H3 >
None.gif            
< asp:Label id = " DisplayLabel "  runat = " server " > Label </ asp:Label >
None.gif        
</ form >
None.gif    
</ body >
None.gif
</ HTML >


DataTable.Select 方法  [C#]

请参见

转载于:https://www.cnblogs.com/BoKeRen/archive/2006/03/02/341201.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值