user2357686
user2357686

Reputation: 11

Gridview filtering by textboxes

This is table with column names and textboxes for entering data to columns

<table border="1">
                <tr>
 <th>EmpId</th>
<th>EmpName</th>
 <th>EmpSalary</th>
 <th>EmpPhone</th>
<th>EmpAddress</th>
 </tr>
 <tr>
<td>
<asp:TextBox ID="TxtEmpId" runat="server" Width="134px" AutoPostBack="true"></asp:TextBox></td>
<td>
<asp:TextBox ID="TxtEmpName" runat="server" Width="121px"></asp:TextBox></td>
<td>
 <asp:TextBox ID="TxtEmpSalary" runat="server" Width="128px"></asp:TextBox></td>
  <td>
<asp:TextBox ID="TxtEmpPhone" runat="server" Width="117px"></asp:TextBox></td>
<td>
  <asp:TextBox ID="TxtEmpAddress" runat="server" Width="120px"></asp:TextBox></td>
</tr>
</table>

This is a grid with columns

<asp:GridView ID="GridView1" runat="server" AllowPaging="True"               DataSourceID="sqlDataSourceGridView" AutoGenerateColumns="False" ShowHeader="false"
CssClass="GridViewStyle" Width="650px" DataKeyNames="EmpId" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4">
 <Columns>
  <asp:BoundField DataField="EmpId" HeaderText="EmpId" ItemStyle-Width="200px" InsertVisible="False" ReadOnly="True" SortExpression="EmpId" >
  <ItemStyle Width="200px" />
 </asp:BoundField>
  <asp:BoundField DataField="EmpName" HeaderText="EmpName" ItemStyle-Width="125px" SortExpression="EmpName" >
  <ItemStyle Width="125px" />
</asp:BoundField>
<asp:BoundField DataField="EmpSalary" HeaderText="EmpSalary" ItemStyle-Width="125px" SortExpression="EmpSalary" >
  <ItemStyle Width="125px" />
</asp:BoundField>
 <asp:BoundField DataField="EmpPhone" HeaderText="EmpPhone" ItemStyle-Width="125px" SortExpression="EmpPhone" >
  <ItemStyle Width="125px" />
</asp:BoundField>
 <asp:BoundField DataField="EmpAddress" HeaderText="EmpAddress" ItemStyle-Width="125px" SortExpression="EmpAddress" >
  <ItemStyle Width="125px" />
 </asp:BoundField>
 </Columns>
 <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
 <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
 <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#330099" />
 <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
 <SortedAscendingCellStyle BackColor="#FEFCEB" />
<SortedAscendingHeaderStyle BackColor="#AF0101" />
 <SortedDescendingCellStyle BackColor="#F6F0C0" />
  <SortedDescendingHeaderStyle BackColor="#7E0000" />
 </asp:GridView>

The sqldatasourc with testingdb data and the filter parameters here if i set ConvertEmptystringToNull=false it displaying error as input string is not correct format, but using ConvertEmptyStringToNull="True" it is working means gridview is populated with data in browser

  <asp:SqlDataSource ID="sqlDataSourceGridView" runat="server" ConnectionString="<%$ ConnectionStrings:TestingDBConnectionString %>" SelectCommand="SELECT * FROM [Employees]"
                FilterExpression="[EmpName] like '{1}%' and [EmpSalary] like '{2}%' and [EmpPhone] like '{3}%' [EmpAddress] like '{4}%'"  >
 <FilterParameters>
  <asp:ControlParameter ControlID="TxtEmpId" Name="EmpId" DefaultValue="" 
   PropertyName="Text" Type="Int32" ConvertEmptyStringToNull="true" />
 <asp:ControlParameter ControlID="TxtEmpName" Name="EmpName" DefaultValue="" 
 PropertyName="Text" Type="String" ConvertEmptyStringToNull="true" />
 <asp:ControlParameter ControlID="TxtEmpSalary" Name="EmpSalary" DefaultValue=""
 PropertyName="Text" Type="Decimal" ConvertEmptyStringToNull="true" />
 <asp:ControlParameter ControlID="TxtEmpPhone" Name="EmpPhone" DefaultValue=""
    PropertyName="Text" Type="Int64" ConvertEmptyStringToNull="true" />
 <asp:ControlParameter ControlID="TxtEmpAddress" Name="EmpAddress" DefaultValue="" 
  PropertyName="Text" Type="String" ConvertEmptyStringToNull="true" />
 </FilterParameters>
</asp:SqlDataSource>

using above code data is binding to gridview with table textboxes when i am entering text in text boxes gridview data is not filltering. I want filter my gridview by entering data in textboxes. please solve my problem

Upvotes: 1

Views: 471

Answers (2)

DiederikEEn
DiederikEEn

Reputation: 743

Use control parameters to control the textfields. simple example:

the query

Select * from tbl_1 WHERE (([Titel] LIKE '%' + @Titel + '%')

De control

 <asp:ControlParameter ControlID="txtTitel" DefaultValue="*" Name="Titel" 
        PropertyName="Text" Type="String" ConvertEmptyStringToNull="False" />

Upvotes: 1

Chetan Sanghani
Chetan Sanghani

Reputation: 2111

Set the Property AutopostBack on Filter = True For filter your record. Try this.......

Upvotes: 0

Related Questions