关于在GridView模版里添加DropDownList实现根据下拉列表的不同选择gridview根据条件筛选显示。

 


                 runat="server"
        BackColor="White"
        BorderColor="#DEDFDE"
        BorderStyle="None"
        BorderWidth="1px"
        CellPadding="4"
        ForeColor="Black"
        GridLines="Vertical"
        OnRowCreated="GridView1_RowCreated">
           
               
                
                                     runat="server"
                   ID="dropdownlist1"
                   BackColor="NavajoWhite"
                   AutoPostBack="true"
                   >
                 
                 
                 
                 
                 

                

               

           

           
           
           
           
           
           
       

      

using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class Test_RowFilter : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["connectionString"]);
    DataView dv = new DataView();
    DropDownList ddl;
    protected void Page_Load(object sender, EventArgs e)
    {
       if (!Page.IsPostBack)
       {
           BindData();
          
       }
   }
   void BindData()
    {
        string sql = "select top 20 JobNo,DeptName,JobType,InputDate,AttnName from viewjobse";
        conn.Open();
        SqlDataAdapter myda = new SqlDataAdapter(sql, conn);
        DataSet ds = new DataSet();
        myda.Fill(ds, "RusTable");
        dv = ds.Tables["RusTable"].DefaultView;
        GridView1.DataSource = dv;
        GridView1.DataBind();
        conn.Close();
    }
   
    protected void dropdownlist1_SelectedIndexChanged(object sender, EventArgs e)
    {
        DropDownList ddl= sender as DropDownList;
        string selectedText = ddl.SelectedValue;
        BindData();
        switch (ddl.SelectedItem.Text)
        {
            case "整箱":
                dv.RowFilter = "jobtype='" + ddl.SelectedItem.Value + "'";
                GridView1.DataSource = dv;
                GridView1.DataBind();
                break;
            case "散货":
                dv.RowFilter = "jobtype='" + ddl.SelectedItem.Value + "'";
                GridView1.DataSource = dv;
                GridView1.DataBind();
                break;
            case "自拼":
                dv.RowFilter = "jobtype='" + ddl.SelectedItem.Value + "'";
                GridView1.DataSource = dv;
                GridView1.DataBind();
                break;
            default:
                BindData();
                break;
        }
    }

    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
       if (e.Row.RowType == DataControlRowType.Header)
        {
            DropDownList ddl = (DropDownList)e.Row.FindControl("dropdownlist1");
            ddl.SelectedIndexChanged += new EventHandler(dropdownlist1_SelectedIndexChanged);
        }
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].BackColor = System.Drawing.Color.White;
        }
    }

}

你可能感兴趣的:(关于在GridView模版里添加DropDownList实现根据下拉列表的不同选择gridview根据条件筛选显示。)