Hello i have a praticular problem actually the problem is that i have a grid having some data in it and i want my grid to be filter with text box i mean tto say when i enter some text in textbox so grid should only show the relevant data.
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:
Use a
FilterExpression and pass the Text value of the TextBox in the FilterParameters of the SelectCommand that provides the data to the GridView
<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>
Do anyone know how to code the click event of the button so that when the user enter the information and clicks on the button, the page will display the search results?
shajiuddin
Member
13 Points
7 Posts
Filter GridView With TextBox
Oct 11, 2006 05:51 AM|LINK
Hello i have a praticular problem actually the problem is that i have a grid having some data in it and i want my grid to be filter with text box i mean tto say when i enter some text in textbox so grid should only show the relevant data.
Fredrik N
All-Star
29674 Points
5334 Posts
MVP
Re: Filter GridView With TextBox
Oct 11, 2006 06:01 AM|LINK
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>
MVP, ASPInsider, WCF RIA Services Insider
My Blog
ReyN
Contributor
2148 Points
413 Posts
Re: Filter GridView With TextBox
Oct 11, 2006 06:30 AM|LINK
And here is one other way of doing it.
Use a FilterExpression and pass the Text value of the TextBox in the FilterParameters of the SelectCommand that provides the data to the GridView
<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>
aspxtreme
shajiuddin
Member
13 Points
7 Posts
Re: Filter GridView With TextBox
Oct 11, 2006 07:15 AM|LINK
soyneutron
Member
5 Points
9 Posts
Re: Filter GridView With TextBox
Jan 30, 2009 07:28 PM|LINK
Do anyone know how to code the click event of the button so that when the user enter the information and clicks on the button, the page will display the search results?
meharooftp
Member
6 Points
13 Posts
Re: Filter GridView With TextBox
Aug 13, 2011 05:31 AM|LINK
Following is the code for Searching gridview using sql datasource and FilterExpression and with text box
The important things to notice is:
don't forget to set "ConvertEmptyStringToNull" property of ControlParameter in FilterParameters to "false"
This ensures the gridview shows all data when no text in all filter text boaxes
The code uses NorthWind database.
The code is:
<table border="0" cellspacing="0"> <tr > <th>Company</th> <th>Name</th> <th>City</th> <td ></td> </tr> <tr > <td><asp:TextBox ID="txtCompany" runat="server" Width="96px"></asp:TextBox></td> <td ><asp:TextBox ID="txtName" runat="server" Width="104px"></asp:TextBox></td> <td><asp:TextBox ID="txtCity" runat="server" Width="96px"></asp:TextBox></td> <td ><asp:Button ID="btnSearch" runat="server" Text="Search" /></td> </tr> </table> <asp:GridView ID="GridView1" runat="server" AllowPaging="True" DataSourceID="sqlDataSourceGridView" AutoGenerateColumns="False" CssClass="GridViewStyle" GridLines="None" Width="650px" > <Columns> <asp:BoundField DataField="CompanyName" HeaderText="Company" ItemStyle-Width="200px" /> <asp:BoundField DataField="ContactName" HeaderText="Name" ItemStyle-Width="125px"/> <asp:BoundField DataField="City" HeaderText="city" ItemStyle-Width="125px" /> <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="125px" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="sqlDataSourceGridView" runat="server" ConnectionString="<%$ ConnectionStrings:northWindConnectionString %>" SelectCommand="SELECT [CustomerID], [CompanyName], [ContactName], [City], [Country] FROM [Customers]" FilterExpression="[CompanyName] like '{0}%' and [ContactName] like '{1}%' and [City] like '{2}%'"> <FilterParameters> <asp:ControlParameter ControlID="txtCompany" Name="CompanyName" PropertyName="Text" Type="String" ConvertEmptyStringToNull="false" /> <asp:ControlParameter ControlID="txtName" Name="ContactName" PropertyName="Text" Type="String" ConvertEmptyStringToNull="false" /> <asp:ControlParameter ControlID="txtCity" Name="City" PropertyName="Text" Type="String" ConvertEmptyStringToNull="false" /> </FilterParameters> </asp:SqlDataSource>No code required in the button click event of search button.(It fills the gridview when post back)