使用Asp.net C#在Gridview中搜索/过滤选项

时间:2015-02-25 08:48:06

标签: c# asp.net gridview

我想在Gridview中提供搜索选项,以便用户可以从gridview中的大数据中找到完全匹配。当我们将Autogenerate属性设置为“true”并在.aspx页面中设置sqlDataSource时,有可用的解决方案。但我在代码中动态生成列。怎么做。请帮忙......! GridView:

 <asp:GridView ID="ItemGrid" runat="server" BackColor="White" Width="90%" 
     onrowcreated="GridView1_RowCreated" BorderStyle="Solid" onrowdatabound="GridView1_RowDataBound" AllowPaging="True" 
                 onpageindexchanging="ItemGrid_PageIndexChanging">
 <HeaderStyle BackColor="#FF9900" BorderColor="White" Height="40px"  HorizontalAlign="Center"  Font-Bold="True" />
 <RowStyle BorderColor="White"  Width="30px" HorizontalAlign="Center" BackColor="#CCCCCC" />
 <AlternatingRowStyle HorizontalAlign="Center" />
  <FooterStyle BackColor="Silver" />                    
             </asp:GridView>

在代码后面生成列:

 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e){

//生成gridview标题单元格 }

任何想法如何在这种情况下添加搜索/过滤选项..?

1 个答案:

答案 0 :(得分:0)

我在我的代码中做了

public void filterd5()
    {
        SqlDataSource1.FilterExpression = "";
        SqlDataSource1.FilterParameters.Clear();
        //GridView1.DataBind();
        if (ddlfilter.SelectedValue == "1")
        {
            SqlDataSource1.FilterExpression = "toad1 LIKE '%{0}%' or toad2 LIKE '%{0}%' or toad3 LIKE '%{0}%' ";
            SqlDataSource1.FilterParameters.Add("toad1", TextBox1.Text);
            SqlDataSource1.FilterParameters.Add("toad2", TextBox1.Text);
            SqlDataSource1.FilterParameters.Add("toad3", TextBox1.Text);
        }
        else if (ddlfilter.SelectedValue == "2")
        {
            SqlDataSource1.FilterExpression = "fromad1 LIKE '%{0}%' or fromad2 LIKE '%{0}%' or fromad3 LIKE '%{0}%' ";
            SqlDataSource1.FilterParameters.Add("fromad1", TextBox1.Text);
            SqlDataSource1.FilterParameters.Add("fromad2", TextBox1.Text);
            SqlDataSource1.FilterParameters.Add("fromad3", TextBox1.Text);
        }

        else if (ddlfilter.SelectedValue == "3")
        {
            SqlDataSource1.FilterExpression = "fromtaluk LIKE '%{0}%' ";
            SqlDataSource1.FilterParameters.Add("fromtaluk", TextBox1.Text);
        }
        else if (ddlfilter.SelectedValue == "4")
        {
            SqlDataSource1.FilterExpression = "totaluk LIKE '%{0}%'";
            SqlDataSource1.FilterParameters.Add("totaluk", TextBox1.Text);
        }

        GridView1.DataBind();
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        filterd5();
        // GridData();
    }

其中fromad,fromtaluk,totaluk是列标题。在下拉菜单中,我绑定了所有标题