Filter gridview

被刻印的时光 ゝ 提交于 2019-12-21 05:47:09

问题


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:


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




回答2:


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.




回答3:


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>


来源:https://stackoverflow.com/questions/2739778/filter-gridview

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!