這個示例演示了怎么樣在DataGrid中篩選數(shù)據(jù)
/*DataDridFilterForm.aspx
前臺程序
*/
<%@ Page language="c#" Codebehind="DataGridFilterForm.aspx.cs"
AutoEventWireup="false"
Inherits="DataDridFilterDemo.DataDridFilterForm" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="vs_snapToGrid" content="True">
<meta name="vs_showGrid" content="True">
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content=" </HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋體">
<asp:DropDownList id="ddlCategory" style="Z-INDEX: 101; LEFT: 120px; POSITION: absolute; TOP: 16px"
runat="server" Width="106px" Height="26px" AutoPostBack="True"></asp:DropDownList>
<asp:Label id="lblCategory" style="Z-INDEX: 102; LEFT: 32px; POSITION: absolute; TOP: 24px"
runat="server">Category:</asp:Label>
<asp:Label
id="lblPrice" style="Z-INDEX: 103; LEFT: 256px; POSITION: absolute;
TOP: 24px" runat="server">Price Range:</asp:Label>
<asp:DropDownList id="ddlPrice" style="Z-INDEX: 104; LEFT: 368px; POSITION: absolute; TOP: 16px" runat="server"
AutoPostBack="True">
<asp:ListItem Value="0" Selected="True">Any Price</asp:ListItem>
<asp:ListItem Value="1">Cheap</asp:ListItem>
<asp:ListItem Value="2">Moderate</asp:ListItem>
<asp:ListItem Value="3">Expensive</asp:ListItem>
<asp:ListItem Value="4">Absurdly Expensive</asp:ListItem>
</asp:DropDownList>
<asp:DataGrid id="dgProduct" style="Z-INDEX: 105; LEFT: 224px; POSITION: absolute; TOP: 96px"
runat="server">
<AlternatingItemStyle BackColor="#E8E6E6"></AlternatingItemStyle>
<ItemStyle BackColor="#F1F1F1"></ItemStyle>
<HeaderStyle BackColor="#C0C0FF"></HeaderStyle>
</asp:DataGrid></FONT>
</form>
</body>
</HTML>
/*DataDridFilterForm.aspx.cs
后臺處理程序
*/
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace DataDridFilterDemo
{
/// <summary>
/// WebForm1 的摘要說明。
/// </summary>
public class DataDridFilterForm : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList ddlCategory;
protected System.Web.UI.WebControls.Label lblCategory;
protected System.Web.UI.WebControls.Label lblPrice;
protected System.Web.UI.WebControls.DropDownList ddlPrice;
protected System.Web.UI.WebControls.DataGrid dgProduct;
public static string strCategory="CategoryID=1";
public static string strPrice="UnitPrice>0";
private void Page_Load(object sender, System.EventArgs e)
{
// 在此處放置用戶代碼以初始化頁面
if(!IsPostBack)
{
FillDropDownList();
DataFiller();
}
}
#region Web 窗體設計器生成的代碼
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 該調(diào)用是 ASP.NET Web 窗體設計器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 設計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </summary>
private void InitializeComponent()
{
this.ddlCategory.SelectedIndexChanged += new System.EventHandler(this.FilterChange);
this.ddlPrice.SelectedIndexChanged += new System.EventHandler(this.FilterChange);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void FillDropDownList()
{
string strCon="server=JOSEN;database=NorthWind;integrated security=true";
string strSqlCategory="select CategoryName,CategoryID from Categories";
SqlConnection objCon=new SqlConnection(strCon);
SqlDataAdapter objAdpt=new SqlDataAdapter(strSqlCategory,objCon);
DataSet ds=new DataSet();
objAdpt.Fill(ds);
ddlCategory.DataSource=ds;
ddlCategory.DataTextField="CategoryName";
ddlCategory.DataValueField="CategoryID";
ddlCategory.DataBind();
}
private void DataFiller()
{
string strCon="server=JOSEN;database=NorthWind;integrated security=true";
string strSqlProduct="select ProductID,ProductName,CategoryID,UnitPrice from Products";
SqlConnection objCon=new SqlConnection(strCon);
SqlDataAdapter objAdpt=new SqlDataAdapter(strSqlProduct,objCon);
DataSet objds=new DataSet();
objAdpt.Fill(objds,"dtProduct");
DataView dvUK=new DataView(objds.Tables["dtProduct"]);
dvUK.RowFilter=strCategory+" and "+strPrice;
this.dgProduct.DataSource=dvUK;
dgProduct.DataBind();
}
private void FilterChange(object sender, System.EventArgs e)
{
FilterByPrice(ddlPrice.SelectedItem.Text.ToString());
FilterByCategory(ddlCategory.SelectedItem.Value.ToString());//注意這里,用的是CategoryID而不是CategoryName
DataFiller();
}
private void FilterByPrice(string strChoice)
{
switch(strChoice)
{
case "Any Price":
strPrice="UnitPrice>0";
break;
case "Cheap":
strPrice="UnitPrice<20";
break;
case "Moderate":
strPrice="UnitPrice>19 and UnitPrice<50";
break;
case "Expensive":
strPrice="UnitPrice>=50";
break;
case "Absurdly Expensive":
strPrice="UnitPrice>100";
break;
}
}
private void FilterByCategory(string strChoice)
{
strCategory="CategoryID="+strChoice;
}
}
}
文章來源:
http://www.tkk7.com/kuxiaoku/articles/94809.html
posted on 2007-01-19 00:16
苦笑枯 閱讀(428)
評論(0) 編輯 收藏 所屬分類:
C#