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

 <div>
        &nbsp;<asp:GridView ID="GridView1"
        runat="server"
        BackColor="White"
        BorderColor="#DEDFDE"
        BorderStyle="None"
        BorderWidth="1px"
        CellPadding="4"
        ForeColor="Black"
        GridLines="Vertical"
        OnRowCreated="GridView1_RowCreated">
            <Columns>
                <asp:TemplateField>
                 <HeaderTemplate>
                  <asp:DropDownList
                   runat="server"
                   ID="dropdownlist1"
                   BackColor="NavajoWhite"
                   AutoPostBack="true"
                   >
                  <asp:ListItem Value="0" Text=" "/>
                  <asp:ListItem Value="1" Text="整箱"/>
                  <asp:ListItem Value="2" Text="散货"/>
                  <asp:ListItem Value="3" Text="自拼"/>
                  </asp:DropDownList>
                 </HeaderTemplate>
                </asp:TemplateField>
            </Columns>
            <FooterStyle BackColor="#CCCC99" />
            <RowStyle BackColor="#F7F7DE" />
            <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
            <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
       </div>

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;
        }
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值