posts - 56,  comments - 12,  trackbacks - 0

          這個示例演示了怎么樣在DataGrid中篩選數據

          /*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: 該調用是 ASP.NET Web 窗體設計器所必需的。
             //
             InitializeComponent();
             base.OnInit(e);
            }
            
            /// <summary>
            /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
            /// 此方法的內容。
            /// </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;
            }
           }
          }

          苦笑枯 2007-01-19 00:16 發表評論

          文章來源:http://www.aygfsteel.com/kuxiaoku/articles/94809.html
          posted on 2007-01-19 00:16 苦笑枯 閱讀(430) 評論(0)  編輯  收藏 所屬分類: C#
          收藏來自互聯網,僅供學習。若有侵權,請與我聯系!

          <2007年1月>
          31123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          常用鏈接

          留言簿(2)

          隨筆分類(56)

          隨筆檔案(56)

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 西华县| 香港 | 柞水县| 东明县| 昌吉市| 斗六市| 樟树市| 高台县| 和政县| 大田县| 长乐市| 广宁县| 金川县| 高陵县| 山丹县| 巴里| 虎林市| 闸北区| 石楼县| 同德县| 易门县| 兖州市| 海门市| 台南县| 涪陵区| 阜宁县| 库尔勒市| 东港市| 鄯善县| 沛县| 保康县| 涞源县| 信宜市| 成武县| 南充市| 若羌县| 屏边| 时尚| 中方县| 上饶市| 兖州市|