views:

112

answers:

3
+1  Q: 

Filter gridview

Hi All,

I have a gridview which I am binding through code behind, I want to filter the gridview based on the value given by the user in textbox.

It would be great if I'll be able to filter the gridview without any postback.

PLease help!

Thanks in advance

+1  A: 

The grid view is made for manipulation during post back. If you were to do this entirely on the client side, you'd use a JavaScript suite that would work on any table, not confined to the grid view. If it were me, I would simply use AJAX by wrapping the grid view and text box in an Update Panel. To the end user, the behavior is the same.

EDIT to include Sample code:

<asp:ScriptManager ID="ScriptManager1" AllowCustomErrorsRedirect="false"  runat="server"></asp:ScriptManager>

  <asp:UpdatePanel ID="UpdatePanel1"  runat="server">
    <ContentTemplate>

            <asp:TextBox runat="server" ID="txtFilterString" ></asp:TextBox>
            <asp:Button runat="server" ID="btnFilter" Text="FilterResults" onclick="btnFilter_Click" /> 

            <asp:GridView runat="server" ID="grdResults"
                DataKeyNames="Id"
                AllowSorting="true" AllowPaging="true" PageSize="20" 
                PagerSettings-Mode="NumericFirstLast">
                    <Columns>
                        .....
                    </Columns>          
                </asp:GridView>


  </asp:UpdatePanel>
MrGumbe
Seems like a solution to me. Can you just briefly explain me what I have to do .
Zerotoinfinite
Sure, I've edited the post to include the general approach
MrGumbe
+1 This is exactly what I do.
Matthew Jones
A: 

you could run a filter expression

<asp:sqldatasource id="articles" runat="server"
   connectionstring="<%$ ConnectionStrings:aspa %>" 
   selectcommand="SELECT title, url, added, updated FROM aspx_articles ORDER BY title" 
   filterexpression="title LIKE '%{0}%' or url LIKE '%{0}%'">
   <filterparameters>
      <asp:controlparameter controlid="searchBox" propertyname="Text" />
   </filterparameters>
</asp:sqldatasource>

or this way

Do you have a TextBox outside the GridView, and when you enter data into it and press a button, the GirdView should be filter on that data?

If so, make sure your select command can taka a parameter with the value you want to filter on. Add a ControlParameter to the SelectParameters colelction of the DataSource control (if you use a DataSource control).

Here is an example that uses the Northwind database, maybe this will help you:

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Button" />
        <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
            DataKeyNames="ProductID" DataSourceID="SqlDataSource1" ShowFooter="True">
            <Columns>
                <asp:CommandField ShowSelectButton="True" />
                <asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False"
                    ReadOnly="True" SortExpression="ProductID" />
                <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
                <asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock" SortExpression="UnitsInStock" />
                <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" SortExpression="UnitPrice" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT [ProductID], [ProductName], [UnitsInStock], [UnitPrice] FROM [Alphabetical list of products] WHERE ([ProductName] LIKE '%' + @ProductName + '%')">
            <SelectParameters>
                <asp:ControlParameter ControlID="TextBox1" DefaultValue="%" Name="ProductName" PropertyName="Text"
                    Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>

codes found here http://forums.asp.net/p/1034014/2904713.aspx

Justin Gregoire
Hi Justin,Thanks for your quick response, but I am binding gridview on server side and not with sql data source.
Zerotoinfinite
@Zerotoinfinite http://www.c-sharpcorner.com/UploadFile/khaledrawy/ICallBack05102008175418PM/ICallBack.aspx has a response to your problem with ajax update panel. how it is used im not sure, im still reading up on it. but sounds a lot like the post by MrGumbe
Justin Gregoire
+1  A: 

If you're using paging, I would recommend using something else (like the Microsoft Ajax Library's dataView). Because gridview paging and client-side filtering wouldn't mesh too well. But if you're not doing paging, you could do something similar to this or this.

Doc Hoffiday