steps :
1) Create the simple Web application (ASP.Net with c#).
2) put the open the page (default.aspx) and put the below code in to that.
<div> <asp:Panel ID="pnlDisplay" runat="server" BackColor="GradientInactiveCaption" BorderStyle="Solid" Font-Size="Larger" ForeColor="#E0E0E0" Width="701px"> <center>Filter In GridView </center> </asp:Panel> <br /> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="UserID" CellPadding="4" ForeColor="#333333" GridLines="None"> <Columns> <asp:TemplateField HeaderText="UserID" InsertVisible="False" SortExpression="UserID"> <ItemTemplate> <asp:Label ID="lblUserID" runat="server" Text='<%# Eval("UserID") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="UserName" SortExpression="UserName"> <%--Create the header template and put the textbox ,button and requiredfieldvalidator --%> <HeaderTemplate> <table> <tr> <td colspan="2"> UserName </td> </tr> <tr> <td> <asp:TextBox ID="txtUserName" runat="server"></asp:TextBox> </div> </td> <td> <asp:Button ID="btnUserNameSubmit" runat="server" Text="Find" OnClick="btnUserNameSubmit_Click" /> </td> </tr> </table> </HeaderTemplate> <ItemTemplate> <asp:Label ID="lblUserName" runat="server" Text='<%# Eval("UserName") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Address1" SortExpression="Address1"> <HeaderTemplate> <table> <tr> <td colspan="2"> Address1 </td> </tr> <tr> <td> <asp:TextBox ID="txtAddress1" runat="server"></asp:TextBox> </div> </td> <td> <asp:Button ID="btnAddress1Submit" runat="server" Text="Find" OnClick="btnAddress1Submit_Click" /> </td> </tr> </table> </HeaderTemplate> <ItemTemplate> <asp:Label ID="lblAddress1" runat="server" Text='<%# Eval("Address1") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="EmailAddress" SortExpression="EmailAddress"> <HeaderTemplate> <table> <tr> <td colspan="2"> Email Address </td> </tr> <tr> <td> <asp:TextBox ID="txtEmailAddress" runat="server"></asp:TextBox> </td> <td> <asp:Button ID="btnEmailAddressSubmit" runat="server" Text="Find" OnClick="btnEmailAddressSubmit_Click" /> </td> </tr> </table> </HeaderTemplate> <ItemTemplate> <asp:Label ID="lblEmailAddress" runat="server" Text='<%# Eval("EmailAddress") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <RowStyle BackColor="#EFF3FB" /> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <EditRowStyle BackColor="#2461BF" /> <AlternatingRowStyle BackColor="White" /> </asp:GridView> <asp:Label ID="lblRecordNotFound" runat="server" BackColor="#FF8080" Text="Record Not Found" Visible="False" Width="279px"></asp:Label></div>
3) Open The Code Behind File and on the page load event put the below code.
protected void Page_Load(object sender, EventArgs e) { //check whether page is postback or not //if page is not postback at that time we bind the gridview if (!Page.IsPostBack) { //here write connection string string strsql = DBConnection.sqlstr; //create object for sqlconnection
//add namespace using System.Data.SqlClient; SqlConnection sqlcon = new SqlConnection(strsql); //here i use the query //create the object of sqlcommand SqlCommand sqlcmd = new SqlCommand("select * from [User]", sqlcon); //create sqldataadapter object and give the sqlcommand as parameter SqlDataAdapter adp = new SqlDataAdapter(sqlcmd); //declare the dataset DataSet ds = new DataSet(); //fill dataset using fill method of SqlDataAdapter adp.Fill(ds); //bind the GridView1 GridView1.DataSource = ds.Tables[0]; GridView1.DataBind(); } }
4) here i am done this code for only one field UserName and other do your own thanks for your efforts. on the click event of the btnUserNameSubmit put the below code or directly write the code.
/// <summary> /// when Apply the filter for UserName /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnUserNameSubmit_Click(object sender, EventArgs e) { //here write connection string string strsql = DBConnection.sqlstr; //create object for sqlconnection SqlConnection sqlcon = new SqlConnection(strsql); //get the txtUserName value //and cast that in to the TextBox string str = ((TextBox)GridView1.HeaderRow.FindControl("txtUserName")).Text; //here i use the query //create the object of sqlcommand SqlCommand sqlcmd = new SqlCommand("select * from [User] where UserName like '%" + str + "%'", sqlcon); //create sqldataadapter object and give the sqlcommand as parameter SqlDataAdapter adp = new SqlDataAdapter(sqlcmd); //declare the dataset DataSet ds = new DataSet(); //fill dataset using fill method of SqlDataAdapter adp.Fill(ds); if (ds.Tables[0].Rows.Count > 0) { lblRecordNotFound.Visible = false; //bind the GridView1 GridView1.DataSource = ds.Tables[0]; GridView1.DataBind(); } else { //display message record not found lblRecordNotFound.Visible = true; } }
/// <summary> /// when apply the filter for address /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnAddress1Submit_Click(object sender, EventArgs e) { //do your own } /// <summary> /// when apply the filter for EmailAddress /// </summary> /// <param name="sender"></param> /// <param name="e"></param> protected void btnEmailAddressSubmit_Click(object sender, EventArgs e) { //do your own }
5) Database Table Information.
TableName : User
UserID | int |
UserName | varchar(50) |
Password | varchar(50) |
Address1 | varchar(50) |
EmailAddress | varchar(50) |
Thank you.
Leave a comment