posts - 56,  comments - 12,  trackbacks - 0

          這個(gè)示例演示了怎么樣在DataGrid中篩選數(shù)據(jù)

          /*DataDridFilterForm.aspx

          前臺(tái)程序

          */

          <%@ 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

          后臺(tái)處理程序

          */

          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 的摘要說(shuō)明。
           /// </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)
            {
             // 在此處放置用戶(hù)代碼以初始化頁(yè)面
             if(!IsPostBack)
             {
              FillDropDownList();
              DataFiller();
             }
            }

            #region Web 窗體設(shè)計(jì)器生成的代碼
            override protected void OnInit(EventArgs e)
            {
             //
             // CODEGEN: 該調(diào)用是 ASP.NET Web 窗體設(shè)計(jì)器所必需的。
             //
             InitializeComponent();
             base.OnInit(e);
            }
            
            /// <summary>
            /// 設(shè)計(jì)器支持所需的方法 - 不要使用代碼編輯器修改
            /// 此方法的內(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;
            }
           }
          }

          苦笑枯 2007-01-19 00:16 發(fā)表評(píng)論

          文章來(lái)源:http://www.aygfsteel.com/kuxiaoku/articles/94809.html
          posted on 2007-01-19 00:16 苦笑枯 閱讀(429) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): C#
          收藏來(lái)自互聯(lián)網(wǎng),僅供學(xué)習(xí)。若有侵權(quán),請(qǐng)與我聯(lián)系!

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

          常用鏈接

          留言簿(2)

          隨筆分類(lèi)(56)

          隨筆檔案(56)

          搜索

          •  

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 义乌市| 宾阳县| 德兴市| 武城县| 峨眉山市| 阳曲县| 札达县| 安徽省| 常熟市| 湘西| 明溪县| 慈利县| 桑日县| 漾濞| 迁西县| 东台市| 新丰县| 常山县| 台前县| 会泽县| 和静县| 静乐县| 九龙城区| 华亭县| 西林县| 淮阳县| 清水河县| 阿尔山市| 辉南县| 卢龙县| 灵宝市| 丰城市| 万山特区| 合江县| 本溪| 临沂市| 正安县| 克拉玛依市| 革吉县| 扶风县| 平阳县|