Hi Waghmare,
Refer the below sample.
HTML
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
<style type="text/css">
body
{
font-family: Arial;
font-size: 10pt;
}
table
{
border: 1px solid #ccc;
}
table th
{
background-color: #F7F7F7;
color: #333;
font-weight: bold;
}
table th, table td
{
padding: 5px;
border-color: #ccc;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<asp:ScriptManager runat="server" />
<asp:UpdatePanel runat="server">
<ContentTemplate>
<br />
<br />
<div>
<asp:HiddenField ID="hfClassApplied" runat="server" />
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false" AllowPaging="true"
PageSize="5" OnPageIndexChanging="gvCustomers_PageIndexChanging" OnRowDataBound="RowDataBound">
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="lblCustomerID" Text="CustomerID" runat="server" />
<asp:TextBox ID="txtCustomerID" runat="server" AutoPostBack="true" OnTextChanged="TextChanged" />
<asp:LinkButton ID="lnkCustomerID" runat="server" OnClick="Sort" CssClass="glyphicon glyphicon-arrow-up"></asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label2" Text='<%# Eval("CustomerID") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="lblCompanyName" Text="CompanyName" runat="server" />
<asp:TextBox ID="txtCompanyName" runat="server" AutoPostBack="true" OnTextChanged="TextChanged" />
<asp:LinkButton ID="lnkCompanyName" runat="server" OnClick="Sort" CssClass="glyphicon glyphicon-arrow-up"></asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label1" Text='<%# Eval("CompanyName") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="lblContactName" Text="ContactName" runat="server" />
<asp:TextBox ID="txtContactName" runat="server" AutoPostBack="true" OnTextChanged="TextChanged" />
<asp:LinkButton ID="lnkContactName" runat="server" OnClick="Sort" CssClass="glyphicon glyphicon-arrow-up"></asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label2" Text='<%# Eval("ContactName") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="lblCity" Text="City" runat="server" />
<asp:TextBox ID="txtCity" runat="server" AutoPostBack="true" OnTextChanged="TextChanged" />
<asp:LinkButton ID="lnkCity" runat="server" OnClick="Sort" CssClass="glyphicon glyphicon-arrow-up"></asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label3" Text='<%# Eval("City") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<HeaderTemplate>
<asp:Label ID="lblCountry" Text="Country" runat="server" />
<asp:TextBox ID="txtCountry" runat="server" AutoPostBack="true" OnTextChanged="TextChanged" />
<asp:LinkButton ID="lnkCountry" runat="server" OnClick="Sort" CssClass="glyphicon glyphicon-arrow-up"></asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label ID="Label4" Text='<%# Eval("Country") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:TextBox runat="server" ID="txtPageSize" AutoPostBack="true" OnTextChanged="Change" />
<asp:Label ID="lblRecordDetails" runat="server" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
C#
public string sortDirection
{
get
{
return ViewState["sortDirection"] == null ? "ASC" : ViewState["sortDirection"].ToString(); // your default sort direction i.e. ASC.
}
set
{
ViewState["sortDirection"] = value;
}
}
public string sortExpression
{
get
{
return ViewState["SortExpression"] == null ? "CustomerID" : ViewState["SortExpression"].ToString(); //Your by default sort expression. i.e. ColumnName
}
set
{
ViewState["SortExpression"] = value;
}
}
static int totalRecord = 0;
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
lblRecordDetails.Text = gvCustomers.Rows.Count.ToString() + " records out of " + totalRecord;
}
}
protected void RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.Header)
{
LinkButton lnkCustomerID = e.Row.FindControl("lnkCustomerID") as LinkButton;
LinkButton lnkCompanyName = e.Row.FindControl("lnkCompanyName") as LinkButton;
LinkButton lnkContactName = e.Row.FindControl("lnkContactName") as LinkButton;
LinkButton lnkCity = e.Row.FindControl("lnkCity") as LinkButton;
LinkButton lnkCountry = e.Row.FindControl("lnkCountry") as LinkButton;
Label lblCustomerID = e.Row.FindControl("lblCustomerID") as Label;
Label lblCompanyName = e.Row.FindControl("lblCompanyName") as Label;
Label lblContactName = e.Row.FindControl("lblContactName") as Label;
Label lblCity = e.Row.FindControl("lblCity") as Label;
Label lblCountry = e.Row.FindControl("lblCountry") as Label;
if (!string.IsNullOrEmpty(hfClassApplied.Value))
{
if (sortExpression == lblCustomerID.Text)
{
lnkCustomerID.CssClass = hfClassApplied.Value;
}
if (sortExpression == lblCompanyName.Text)
{
lnkCompanyName.CssClass = hfClassApplied.Value;
}
if (sortExpression == lblContactName.Text)
{
lnkContactName.CssClass = hfClassApplied.Value;
}
if (sortExpression == lblCity.Text)
{
lnkCity.CssClass = hfClassApplied.Value;
}
if (sortExpression == lblCountry.Text)
{
lnkCountry.CssClass = hfClassApplied.Value;
}
}
}
}
public void BindGrid()
{
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "SELECT TOP 29 [CustomerID],[CompanyName],[ContactName],[City],[Country] FROM [Customers]";
cmd.Connection = con;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
totalRecord = dt.Rows.Count;
}
if (ViewState["GridView"] == null)
{
ViewState["GridView"] = dt;
}
gvCustomers.DataSource = ViewState["GridView"] as DataTable;
gvCustomers.DataBind();
}
}
}
protected void gvCustomers_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvCustomers.PageIndex = e.NewPageIndex;
SortGridView(sortExpression);
lblRecordDetails.Text = gvCustomers.Rows.Count.ToString() + " records out of " + totalRecord;
}
protected void Sort(object sender, EventArgs e)
{
string assignedClass = (sender as LinkButton).CssClass.Split('-')[2];
if (assignedClass.ToUpper() == "UP")
{
sortDirection = "ASC";
hfClassApplied.Value = "glyphicon glyphicon-arrow-down";
}
else
{
sortDirection = "DESC";
hfClassApplied.Value = "glyphicon glyphicon-arrow-up";
}
string selectedId = (sender as LinkButton).ID;
Label lblCustomerID = (sender as LinkButton).FindControl("lblCustomerID") as Label;
Label lblCompanyName = (sender as LinkButton).FindControl("lblCompanyName") as Label;
Label lblContactName = (sender as LinkButton).FindControl("lblContactName") as Label;
Label lblCity = (sender as LinkButton).FindControl("lblCity") as Label;
Label lblCountry = (sender as LinkButton).FindControl("lblCountry") as Label;
LinkButton lnkCustomerID = (sender as LinkButton).FindControl("lnkCustomerID") as LinkButton;
LinkButton lnkCompanyName = (sender as LinkButton).FindControl("lnkCompanyName") as LinkButton;
LinkButton lnkContactName = (sender as LinkButton).FindControl("lnkContactName") as LinkButton;
LinkButton lnkCity = (sender as LinkButton).FindControl("lnkCity") as LinkButton;
LinkButton lnkCountry = (sender as LinkButton).FindControl("lnkCountry") as LinkButton;
if (lnkCustomerID.ID == selectedId)
{
sortExpression = lblCustomerID.Text;
}
if (lnkCompanyName.ID == selectedId)
{
sortExpression = lblCompanyName.Text;
}
if (lnkContactName.ID == selectedId)
{
sortExpression = lblContactName.Text;
}
if (lnkCity.ID == selectedId)
{
sortExpression = lblCity.Text;
}
if (lnkCountry.ID == selectedId)
{
sortExpression = lblCountry.Text;
}
SortGridView(sortExpression);
}
private void SortGridView(string sortExpression)
{
DataTable dt = new DataTable();
if (ViewState["GridView"] != null)
{
dt = ViewState["GridView"] as DataTable;
}
DataView dv = new DataView(dt);
if (sortDirection == "ASC")
{
dv.Sort = sortExpression + " " + "ASC";
}
else
{
dv.Sort = sortExpression + " " + "DESC";
}
gvCustomers.DataSource = dv;
gvCustomers.DataBind();
}
protected void TextChanged(object sender, EventArgs e)
{
GridViewRow row = ((sender as TextBox).NamingContainer as GridViewRow);
TextBox txtCustomerID = row.FindControl("txtCustomerID") as TextBox;
TextBox txtCompanyName = row.FindControl("txtCompanyName") as TextBox;
TextBox txtContactName = row.FindControl("txtContactName") as TextBox;
TextBox txtCity = row.FindControl("txtCity") as TextBox;
TextBox txtCountry = row.FindControl("txtCountry") as TextBox;
string query = "SELECT [CustomerID],[CompanyName],[ContactName],[City],[Country] FROM [Customers]";
string whereQuery = string.Empty;
using (SqlConnection con = new SqlConnection(constr))
{
if (!string.IsNullOrEmpty(txtCustomerID.Text))
{
whereQuery += " CustomerID LIKE '" + txtCustomerID.Text.Trim() + "%'";
}
if (!string.IsNullOrEmpty(txtCompanyName.Text))
{
if (!string.IsNullOrEmpty(whereQuery))
{
whereQuery += " AND CompanyName LIKE '" + txtCompanyName.Text.Trim() + "%'";
}
else
{
whereQuery += " CompanyName LIKE '" + txtCompanyName.Text.Trim() + "%'";
}
}
if (!string.IsNullOrEmpty(txtContactName.Text))
{
if (!string.IsNullOrEmpty(whereQuery))
{
whereQuery += " AND ContactName LIKE '" + txtContactName.Text.Trim() + "%'";
}
else
{
whereQuery += " ContactName LIKE '" + txtContactName.Text.Trim() + "%'";
}
}
if (!string.IsNullOrEmpty(txtCity.Text))
{
if (!string.IsNullOrEmpty(whereQuery))
{
whereQuery += " AND City LIKE '" + txtCity.Text.Trim() + "%'";
}
else
{
whereQuery += " City LIKE '" + txtCity.Text.Trim() + "%'";
}
}
if (!string.IsNullOrEmpty(txtCountry.Text))
{
if (!string.IsNullOrEmpty(whereQuery))
{
whereQuery += " AND Country LIKE '" + txtCountry.Text.Trim() + "%'";
}
else
{
whereQuery += " Country LIKE '" + txtCountry.Text.Trim() + "%'";
}
}
whereQuery = !string.IsNullOrEmpty(whereQuery) ? " WHERE " + whereQuery : whereQuery;
query += whereQuery;
using (SqlCommand cmd = new SqlCommand(query, con))
{
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
if (ViewState["GridView"] != null)
{
ViewState["GridView"] = dt;
}
gvCustomers.DataSource = ViewState["GridView"] as DataTable;
gvCustomers.DataBind();
}
else
{
ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "Alert", "alert('No Records Found!!')", true);
}
}
}
}
protected void Change(object sender, EventArgs e)
{
gvCustomers.PageSize = Convert.ToInt32(txtPageSize.Text.Trim());
BindGrid();
gvCustomers.PageIndex = 0;
gvCustomers.DataBind();
lblRecordDetails.Text = gvCustomers.Rows.Count.ToString() + " records out of " + totalRecord;
}
Screenshot
