2

How can I filter the results shown in an asp.net Datagrid?

html

    <asp:TextBox ID="SearchText" runat="server" OnTextChanged="UpdateFilter" />
    <asp:DataGrid ID="gvProjList" runat="server" CellPadding="4" 
        ForeColor="#333333" GridLines="None" style="text-align: left" 
        Width="400px" AllowSorting="true">
        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    </asp:DataGrid>

c#

    protected void Page_Load(object sender, EventArgs e)
    {
        if(!IsPostBack)
        {
            DataTable dt ....
            ....
            gvProjList.DataSource = dt;
            gvProjList.DataBind();
         }
     }


    protected void UpdateFilter(object sender, EventArgs e)
    {
        // ????
        // ViewState["LastFilter"] = SearchText.Text;
        // gvProjList.DataBind();

    }

3 Answers 3

1

It the easy way but something like this should work

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        BindGrid();
    }
}

protected void UpdateFilter(object sender, EventArgs e)
{
    BindGrid();
}

private void BindGrid()
{
    DataTable dt = new DataTable();
    //...
    dt.DefaultView.RowFilter = "SomeColumnInTheDataTable = '" + SearchText.Text + "'";
    gvProjList.DataSource = dt.DefaultView;
    gvProjList.DataBind();
}
3
  • Of course you're going to be loading all the data in -- potentially very bad. Apr 27, 2011 at 20:32
  • Indeed, not ideal but depending on the situation it's the quick and easy route
    – MikeM
    Apr 27, 2011 at 20:34
  • Yep agreed. You answered the literal question accurately -- just want to make sure someone doesn't end up maxing out a server somewhere. ;) Apr 27, 2011 at 20:35
1

Well, it's all about the DataTable.

Filtering in General

You can either handle this by changing the SQL/store procedure/etc you use to load data into the DataTable, or (and be careful here) by loading all the data and then using ADO.Net's built-in filtering capabilities.

For the latter case, DataTable.Select springs to mind (Article on DataTable.Select and Filtering in ADO.Net).

Search in Particlar

However, in the context of a search, the best practice is to do the work in the database, meaning in your SQL. Otherwise you have to pull all the data across the wire each time a search occurs, and then filter it on the client. Unless your table is always going to be very small, that's a very real performance problem. And even then it smells pretty bad, becuase if it's big enough to need a search, you're already dealing with the type of table that could grow.

It's much better to load just what you need. This can lead to some annoyingly involved stored procedures. This is one of the problems where ORM tools really shine because they make it so easy to do very dynamic queries without necessarily having to mess with big sprocs with optional parameters and so on.

Hope that helps!

-1

Is there a reason you are executing your SQL query in Page_Load(...)? If not I would highly recommend using a SqlDataSource control configured with a SQL statement including a parameter for your filter and a ControlParameter object in the SqlDataSource.SelectParameters collection. All of this can be done declaratively it and is much more efficient for both the SQL and ASP.NET servers.

Best part is, it can all be done in markup:

<asp:TextBox ID="SearchText" runat="server" OnTextChanged="UpdateFilter" />
<asp:DataGrid ID="gvProjList" runat="server" CellPadding="4" ForeColor="#333333"
    GridLines="None" Style="text-align: left" Width="400px" AllowSorting="true" DataSourceID="SqlDataSource1">
    <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
    <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
</asp:DataGrid>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" SelectCommand="SELECT * FROM [Products] WHERE [Description] LIKE '%' + @SearchText + '%';">
    <SelectParameters>
        <asp:ControlParameter DbType="String" ControlID="SearchText" PropertyName="Text"
            DefaultValue="" />
    </SelectParameters>
</asp:SqlDataSource>
5
  • Lesson learned with the -1: don't answer a question that wasn't asked, right? Apr 27, 2011 at 20:47
  • The main reason I voted you down was because I have a strong negative reaction to having concatenated SQL mixed in with markup. I hate to be critical since I know there are people out there who would certainly think some of /my/ practices are too lightweight, but to me this is a maintenance problem and I don't think it should be presented as an ok way to do things. I know you're trying to be helpful -- sorry. :) Apr 27, 2011 at 20:52
  • To give you a little context, at my shop inline/concatenated SQL isn't allowed at all, and all data access is supposed to happen in a data access layer (a seperate project). So that's where my knee-jerk reaction comes from. Apr 27, 2011 at 20:58
  • @Brian - I understand the architecture issues you're talking about but for brevity I just included the inline SQL. I don't think it makes much sense to get into DAL concepts in every post that includes data access. If what you're saying is that nobody should ever post an example with a SqlDataSource with an inline SQL statement for example's sake, I don't agree with that statement. Having said all that, thanks for the feedback. I'm still new here and hoping to learn in the process of posting answers and solving problems. Apr 28, 2011 at 13:43
  • @pseduocoder: I think it's okay for brevity. But you did say you "highly recommend" doing it this particular way. Anyway, not trying to be a jerk here, I know there is a line around here somewhere and it can be hard to identify. I hope you stick around and keep helping people solve their problems. :) Apr 28, 2011 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.