Search functionality in asp.net with gridview? [Resolved]

Posted by Mandlaa under ASP.NET on 1/9/2014 | Points: 10 | Views : 2619 | Status : [Member] | Replies : 8
I am displaying data from database using gridview,
After that i want to search functionality based on condition,
How can i do?

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
EnableViewState="False"
Width="853px" CellPadding="4" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" CssClass="newStyle1" Height="202px" style="font-size: small" AllowPaging="True" PageSize="25">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField ShowHeader="false">
<ItemTemplate>
<asp:LinkButton ID="LnkEdit" runat="server" Text="Edit" CommandName="Edit" />
<asp:LinkButton ID="LnkDelete" runat="server" Text="Delete" CommandName="Delete" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="LnkUpdate" runat="server" Text="Update" CommandName="Update" />
<asp:LinkButton ID="LnkCancel" runat="server" Text="Cancel" CommandName="Cancel" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CampaignName">
<ItemTemplate>
<asp:Label ID="LblCampaignName" runat="server" Text="<%#Bind('CampaignName') %>" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TxtCampaignName" runat="server"
Text="<%# Bind('CampaignName') %>" />
</EditItemTemplate>

</asp:TemplateField>
<asp:TemplateField HeaderText="Taglocation">
<ItemTemplate>
<asp:Label ID="LblTaglocation" runat="server" Text="<%#Bind('Taglocation') %>" /> </ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TxtTaglocation" runat="server"
Text="<%# Bind('Taglocation') %>" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="TagType">
<ItemTemplate>
<asp:Label ID="LblTagType" runat="server" Text="<%#Bind('TagType') %>" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TxtTagType" runat="server"
Text="<%# Bind('TagType') %>" />
</EditItemTemplate>
</asp:TemplateField>




Responses

Posted by: Satyapriyanayak on: 1/22/2014 [Member] [MVP] Silver | Points: 50

Up
0
Down

Resolved
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="ResultGridView" runat="server" AutoGenerateColumns="False" ShowFooter="true"
DataKeyNames="VendorId"
AllowPaging="True"
CellPadding="3"
OnPageIndexChanging="ResultGridView_PageIndexChanging"
OnRowDeleting="ResultGridView_RowDeleting"
OnRowEditing="ResultGridView_RowEditing"
OnRowUpdating="ResultGridView_RowUpdating"
OnRowCancelingEdit="ResultGridView_RowCancelingEdit" PageSize="5"
BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
CellSpacing="2" OnRowCommand="ResultGridView_RowCommand" AllowSorting="true"
onsorting="ResultGridView_Sorting">
<Columns>
<asp:BoundField DataField="VendorId" HeaderText="VendorId" InsertVisible="False"
ReadOnly="True" SortExpression="VendorId" />
<asp:TemplateField HeaderText="FirstName" SortExpression="VendorFName">
<EditItemTemplate>
<asp:TextBox ID="txtFName" Width="100px" runat="server" Text='<%# Bind("VendorFName") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtFName1" runat="server" Width="100px"></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("VendorFName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="LastName" SortExpression="VendorLName">
<EditItemTemplate>
<asp:TextBox ID="txtLName" Width="100px" runat="server" Text='<%# Bind("VendorLName") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtLName1" Width="100px" runat="server" ></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("VendorLName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City" SortExpression="VendorCity">
<EditItemTemplate>
<asp:TextBox ID="txtCity" Width="100px" runat="server" Text='<%# Bind("VendorCity") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtCity1" Width="100px" runat="server" ></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("VendorCity") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="State" SortExpression="VendorState">
<EditItemTemplate>
<asp:TextBox ID="txtState" Width="100px" runat="server" Text='<%# Bind("VendorState") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtState1" Width="100px" runat="server" ></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("VendorState") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country" SortExpression="VendorCountry">
<EditItemTemplate>
<asp:TextBox ID="txtCountry" Width="100px" runat="server" Text='<%# Bind("VendorCountry") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtCountry1" Width="100px" runat="server" ></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
<asp:Label ID="Label5" runat="server" Text='<%# Bind("VendorCountry") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="VendorDescription" SortExpression="VendorDescription">
<EditItemTemplate>
<asp:TextBox ID="txtDescription" TextMode="MultiLine" runat="server" Text='<%# Bind("VendorDescription") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtDescription1" runat="server" TextMode="MultiLine" ></asp:TextBox>


If this post helps you mark it as answer
Thanks

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: kgovindarao523-21772 on: 1/24/2014 [Member] [MVP] Bronze | Points: 50

Up
0
Down

Resolved
Hi,

Refer following link.
http://www.c-sharpcorner.com/UploadFile/0c1bb2/searching-records-from-database-and-display-in-gridview-usin/

Or
Apply JQuery Data tables to your grid.
please refer this URL if you are ok with jquery plugin
http://datatables.net/release-datatables/examples/api/highlight.html


Thank you,
Govind

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Snaveen on: 2/13/2014 [Member] Starter | Points: 50

Up
0
Down

Resolved
Hi,

You want to get data based on search content then prepare SP as you need.

EX:

[code]
create procedure Searched_Content
(
@Search varchar(100)=null
)
as
begin
select * from tablename where colname like '%'+@Search+'%'
end
[/code]

and use this sp while binding data to gridview and in design time you just use on textbox control and based on textbox input you can search data and pass that textbox value as a input parameter for the above storedprocedure.

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mandlaa on: 1/9/2014 [Member] Starter | Points: 25

Up
0
Down
<asp:TemplateField HeaderText="TagData">
<ItemTemplate>
<asp:Label ID="LblTagData" runat="server" Text="<%#Bind('TagData') %>" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TxtTagData" Width ="400" runat="server"
Text="<%# Bind('TagData') %>" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Keywords">
<ItemTemplate>
<asp:Label ID="LblKeywords" runat="server" Text="<%#Bind('Keywords') %>" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TxtKeywords" Width="100" runat="server"
Text="<%# Bind('Keywords') %>" />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Tagusage">
<ItemTemplate>
<asp:Label ID="LblTagusage" runat="server" Text="<%#Bind('Tagusage') %>" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TxtTagusage" runat="server"
Text="<%# Bind('Tagusage') %>" />
</EditItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Tag Sno">
<ItemTemplate>
<asp:Label ID="LblTagId" runat="server" Text="<%#Bind('TagId') %>" />
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="TxtTagId" runat="server"
Text="<%# Bind('TagId') %>" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mandlaa on: 1/9/2014 [Member] Starter | Points: 25

Up
0
Down
My Cs code
protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["stgdbConnectionString"].ConnectionString);
con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["stgdbConnectionString"].ConnectionString);
da = new SqlDataAdapter("Select * from Tagrankingtable where ClientId =" + Session["ClientId"], con);
cb = new SqlCommandBuilder(da);
ds = new DataSet();
da.Fill(ds, "Tagrankingtable");
GridView1.DataSource = ds.Tables["Tagrankingtable"];
GridView1.DataBind();
//ShowData();

}

After that i want search functinality based on TagId?
For this one i am writting the below code
private void ShowData1()
{
SqlConnection con1 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["stgdbConnectionString"].ConnectionString);
SqlDataAdapter da1 = new SqlDataAdapter("Select CampaignName,Taglocation,TagType,TagData,Keywords,Tagusage,TagId from Tagrankingtable where ClientId =" + Session["ClientId"] + "and TagId=" + txtSearch.Text, con1);
SqlCommandBuilder cb1 = new SqlCommandBuilder(da1);
DataSet ds1 = new DataSet();
da1.Fill(ds1, "Tagrankingtable");
GridView1.DataSource = ds1.Tables["Tagrankingtable"];
GridView1.DataBind();

}

It is correct way for search records from gridview based on condition

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mandlaa on: 1/9/2014 [Member] Starter | Points: 25

Up
0
Down
protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["stgdbConnectionString"].ConnectionString);
con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["stgdbConnectionString"].ConnectionString);
da = new SqlDataAdapter("Select * from Tagrankingtable where ClientId =" + Session["ClientId"], con);
cb = new SqlCommandBuilder(da);
ds = new DataSet();
da.Fill(ds, "Tagrankingtable");
GridView1.DataSource = ds.Tables["Tagrankingtable"];
GridView1.DataBind();
//ShowData();

}

After that i want search functinality based on TagId?
For this one i am writting the below code
private void ShowData1()
{
SqlConnection con1 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["stgdbConnectionString"].ConnectionString);
SqlDataAdapter da1 = new SqlDataAdapter("Select CampaignName,Taglocation,TagType,TagData,Keywords,Tagusage,TagId from Tagrankingtable where ClientId =" + Session["ClientId"] + "and TagId=" + txtSearch.Text, con1);
SqlCommandBuilder cb1 = new SqlCommandBuilder(da1);
DataSet ds1 = new DataSet();
da1.Fill(ds1, "Tagrankingtable");
GridView1.DataSource = ds1.Tables["Tagrankingtable"];
GridView1.DataBind();

}

It is correct way for search records from gridview based on condition

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mandlaa on: 1/21/2014 [Member] Starter | Points: 25

Up
0
Down
please help me

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Satyapriyanayak on: 1/22/2014 [Member] [MVP] Silver | Points: 25

Up
0
Down
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
//SqlConnection conn = new SqlConnection(connStr);
SqlDataAdapter ad = new SqlDataAdapter();
SqlCommand cmd = new SqlCommand();
DataTable dataTable;
SqlDataAdapter sqlda;
DataSet ds;
string str;
protected void Page_Load(object sender, EventArgs e)
{
Session["sortBy"] = null;
if (!IsPostBack)
{
FillVendorGrid();
}
}
private void FillVendorGrid()
{
SqlConnection conn = new SqlConnection(connStr);
dataTable = new DataTable();
cmd.Connection = conn;
cmd.CommandText = "SELECT * FROM Vendor";
ad = new SqlDataAdapter(cmd);
ad.Fill(dataTable);
ResultGridView.DataSource = dataTable;
ResultGridView.DataBind();

}

protected void ResultGridView_RowEditing(object sender, GridViewEditEventArgs e)
{
ResultGridView.EditIndex = e.NewEditIndex;
FillVendorGrid();
}


protected void ResultGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
ResultGridView.PageIndex = e.NewPageIndex;
FillVendorGrid();
}

protected void ResultGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SqlConnection conn = new SqlConnection(connStr);
cmd.Connection = conn;
cmd.CommandText = "DELETE FROM Vendor WHERE VendorId='" + ResultGridView.DataKeys[e.RowIndex].Values[0].ToString() + "'";
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
FillVendorGrid();

}

protected void ResultGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
TextBox txtFName = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtFName");
TextBox txtLName = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtLName");
TextBox txtCity = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtCity");
TextBox txtState = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtState");
TextBox txtCountry = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtCountry");
TextBox txtDescription = (TextBox)ResultGridView.Rows[e.RowIndex].FindControl("txtDescription");

SqlConnection conn = new SqlConnection(connStr);
cmd.Connection = conn;
cmd.CommandText = "UPDATE Vendor SET VendorFName ='" + txtFName.Text + "',VendorLName ='" + txtLName.Text + "',VendorCity ='" + txtCity.Text + "',VendorState ='" + txtState.Text + "',VendorCountry ='" + txtCountry.Text + "',VendorDescription ='" + txtDescription.Text + "' WHERE VendorId='" + ResultGridView.DataKeys[e.RowIndex].Values[0].ToString() + "'";
conn.Open();
cmd.ExecuteNonQuery();
ResultGridView.EditIndex = -1;
FillVendorGrid();
conn.Close();

}

protected void ResultGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
ResultGridView.EditIndex = -1;
FillVendorGrid();

}

protected void ResultGridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("AddNew"))
{

TextBox txtFName = (TextBox)ResultGridView.FooterRow.FindControl("txtFName1");
TextBox txtLName = (TextBox)ResultGridView.FooterRow.FindControl("txtLName1");
TextBox txtCity = (TextBox)ResultGridView.FooterRow.FindControl("txtCity1");
TextBox txtState = (TextBox)ResultGridView.FooterRow.FindControl("txtState1");
TextBox txtCountry = (TextBox)ResultGridView.FooterRow.FindControl("txtCountry1");
TextBox txtDescription = (TextBox)ResultGridView.FooterRow.FindControl("txtDescription1");
SqlConnection conn = new SqlConnection(connStr);
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO Vendor(VendorFName, VendorLName,VendorCity,VendorState,VendorCountry,VendorDescription) Values('" + txtFName.Text + "', '" + txtLName.Text + "', '" + txtCity.Text + "', '" + txtState.Text + "', '" + txtCountry.Text + "' , '" + txtDescription.Text + "')";
conn.Open();
cmd.ExecuteNonQuery();
FillVendorGrid();
conn.Close();
}
}
protected void btn_search_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection(connStr);
conn.Open();
str = "select * from Vendor where VendorFName like '" + TextBox1.Text + "%'";
cmd = new SqlCommand(str, conn);
sqlda = new SqlDataAdapter(cmd);
ds = new DataSet();
sqlda.Fill(ds, "Vendor");
conn.Close();

ResultGridView.DataSource = ds;
ResultGridView.DataMember = "Vendor";
ResultGridView.DataBind();

}
protected void ResultGridView_Sorting(object sender, GridViewSortEventArgs e)
{
Session["sortBy"] = e.SortExpression;
FillVendorGrid();
}
}

If this post helps you mark it as answer
Thanks

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response