step1 : create class and paste following in it
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
namespace E_Learning.Web.SessionWrapper
{
public class SortingClass
{
private static SortingClass _instance;
public static SortingClass Instance
{
get { return _instance = new SortingClass(); }
}
// This Function adds the sorting image in the Gridview's Column Header
public void AddSortImage(GridViewRow headerRow, object sortExp, object sortOrder, int ColumnCount, string[] ColumnName)
{
// ViewState["SortExp"] = sortExp;
//ViewState["SortOrder"] = sortOrder;
Int32 iCol = GetSortColumnIndex(sortExp.ToString(), ColumnCount, ColumnName);
if (-1 == iCol)
{
return;
}
// Create the sorting image based on the sort direction.
Image sortImage = new Image();
if (sortOrder == "ASC")
{
sortImage.ImageUrl = "../Images/asc.gif";
sortImage.AlternateText = "Ascending Order";
}
else
{
sortImage.ImageUrl = "../Images/desc.gif";
sortImage.AlternateText = "Descending Order";
}
// Add the image to the appropriate header cell.
headerRow.Cells[iCol].Controls.Add(sortImage);
}
// This Function Gets the index of the column selected For Sorting
public Int32 GetSortColumnIndex(string psortExp, int ColumnCount, string[] ColumnName)
{
Int32 iCol = 0;
for (int i = 0; i < ColumnCount; i++)
{
if (ColumnName[i] == psortExp)
{
iCol = i;
}
}
return iCol;
}
public string[] Sorting(object SortExp, object SortOrder, GridViewSortEventArgs e)
{
string[] strViewStateDetails = new string[2];
//ViewState["SortExp"] = SortExp;
//ViewState["SortOrder"] = SortOrder;
if (SortExp == null)
{
SortExp = e.SortExpression.ToString();
SortOrder = "ASC";
}
else
{
if (SortExp.ToString() == e.SortExpression.ToString())
{
if (SortOrder.ToString() == "ASC")
SortOrder = "DESC";
else
SortOrder = "ASC";
}
else
{
SortOrder = "ASC";
SortExp = e.SortExpression.ToString();
}
}
strViewStateDetails[0] = SortExp.ToString();
strViewStateDetails[1] = SortOrder.ToString();
return strViewStateDetails;
}
}
}
srep 2: aspx page
<asp:GridView ID="dgvContent" AllowSorting="true" CellPadding="5" BorderStyle="None"
AllowPaging="true" Width="100%" CellSpacing="5" PageSize="10" AutoGenerateColumns="false"
runat="server" OnPageIndexChanging="dgvContent_PageIndexChanging" PagerSettings-Mode="Numeric"
PagerSettings-PageButtonCount="4" OnRowCommand="dgvContent_RowCommand" OnRowDataBound="dgvContent_RowDataBound"
OnSorting="dgvContent_Sorting">
<RowStyle />
<AlternatingRowStyle />
<PagerStyle HorizontalAlign="Left" CssClass="pager" />
<HeaderStyle BackColor="#F1A626" />
<EmptyDataTemplate>
No Record Found
</EmptyDataTemplate>
<Columns>
<asp:BoundField DataField="LeadId" HeaderText="Lead Id" HeaderStyle-Height="35" HeaderStyle-ForeColor="Black" />
<asp:BoundField DataField="UserName" HeaderStyle-ForeColor="Black" HeaderStyle-Height="35"
HeaderText="UserName" Visible="false" />
<asp:BoundField DataField="Name" HeaderStyle-ForeColor="Black" HeaderStyle-Height="35"
HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Phone" HeaderText="Phone" HeaderStyle-Height="35" HeaderStyle-ForeColor="Black" />
<asp:BoundField DataField="LeadCategoryName" HeaderText="Status" HeaderStyle-Height="35"
HeaderStyle-ForeColor="Black" Visible="false" />
<asp:TemplateField HeaderText="Lead Status" HeaderStyle-ForeColor="Black">
<ItemTemplate>
<asp:Label ID="lblLeadId" runat="server" Text='<%# Eval("LeadId") %>' Visible="false" />
<asp:Label ID="lblleadstatus" runat="server" Text='<%# Eval("LeadCategoryId") %>'
Visible="false" />
<asp:DropDownList runat="server" ID="ddlleadstatus" AutoPostBack="true" OnSelectedIndexChanged="ddlleadstatus_SelectedIndexChanged">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="LeadCategoryId" HeaderText="LeadCategoryId" HeaderStyle-Height="35"
HeaderStyle-ForeColor="Black" Visible="false" />
<asp:BoundField DataField="ReferredBy" HeaderText="Referred By" HeaderStyle-ForeColor="Black" />
<asp:TemplateField HeaderText="Start Planning" HeaderStyle-ForeColor="Black">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="../images/track1.jpg"
CausesValidation="false" CommandName="start" CommandArgument='<%#Eval("LeadId")+","+ Eval("UserName")%>'
Text="Start Plan" ToolTip="Start Planning" />
<%-- <asp:LinkButton ID="lnkSetCredentials" runat="server" CommandName="Set" CommandArgument='<%#Eval("LeadId")+","+ Eval("Email")%>'
Text="Set"></asp:LinkButton>--%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" HeaderStyle-ForeColor="Black">
<ItemTemplate>
<asp:ImageButton ID="imgEdit" runat="server" ImageUrl="~/images/Edit.jpg" Height="23"
Width="23" CausesValidation="false" CommandName="Edit" CommandArgument='<%#Eval("LeadId")%>'
Text="Edit" ToolTip="Edit Lead" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<table style="height: 15px; text-align: left; vertical-align: top; width: 100%;">
<tr>
<td style="width: 200px; height: 10px; text-align: center; vertical-align: middle;">
<b><i>
<%=dgvContent.PageIndex + 1%>of<%=dgvContent.PageCount%></i></b>
</td>
</tr>
</table>
step 3: protected void dgvContent_Sorting(object sender, GridViewSortEventArgs e)
{
string[] ViewStateValues = SortingClass.Instance.Sorting(this.ViewState["SortExp"], this.ViewState["SortOrder"], e);
if (ViewStateValues.Length > 0)
{
this.ViewState["SortExp"] = ViewStateValues[0];
this.ViewState["SortOrder"] = ViewStateValues[1];
}
BindGrid();
}
step 4:
private void BindGrid()
{
DataSet ds = GetLeadData();
try
{
if (ds.Tables[0].Rows.Count > 0)
{
DataView dv = ds.Tables[0].DefaultView;
if (ViewState["SortExp"] != null)
{
dv.Sort = this.ViewState["SortExp"].ToString() + " " + this.ViewState["SortOrder"].ToString();
dgvContent.DataSource = dv.ToTable();
dgvContent.DataBind();
}
else
{
dgvContent.DataSource = ds;
dgvContent.DataBind();
}
}
else
{
dgvContent.DataSource = null;
dgvContent.DataBind();
}
}
catch (Exception ex)
{
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
namespace E_Learning.Web.SessionWrapper
{
public class SortingClass
{
private static SortingClass _instance;
public static SortingClass Instance
{
get { return _instance = new SortingClass(); }
}
// This Function adds the sorting image in the Gridview's Column Header
public void AddSortImage(GridViewRow headerRow, object sortExp, object sortOrder, int ColumnCount, string[] ColumnName)
{
// ViewState["SortExp"] = sortExp;
//ViewState["SortOrder"] = sortOrder;
Int32 iCol = GetSortColumnIndex(sortExp.ToString(), ColumnCount, ColumnName);
if (-1 == iCol)
{
return;
}
// Create the sorting image based on the sort direction.
Image sortImage = new Image();
if (sortOrder == "ASC")
{
sortImage.ImageUrl = "../Images/asc.gif";
sortImage.AlternateText = "Ascending Order";
}
else
{
sortImage.ImageUrl = "../Images/desc.gif";
sortImage.AlternateText = "Descending Order";
}
// Add the image to the appropriate header cell.
headerRow.Cells[iCol].Controls.Add(sortImage);
}
// This Function Gets the index of the column selected For Sorting
public Int32 GetSortColumnIndex(string psortExp, int ColumnCount, string[] ColumnName)
{
Int32 iCol = 0;
for (int i = 0; i < ColumnCount; i++)
{
if (ColumnName[i] == psortExp)
{
iCol = i;
}
}
return iCol;
}
public string[] Sorting(object SortExp, object SortOrder, GridViewSortEventArgs e)
{
string[] strViewStateDetails = new string[2];
//ViewState["SortExp"] = SortExp;
//ViewState["SortOrder"] = SortOrder;
if (SortExp == null)
{
SortExp = e.SortExpression.ToString();
SortOrder = "ASC";
}
else
{
if (SortExp.ToString() == e.SortExpression.ToString())
{
if (SortOrder.ToString() == "ASC")
SortOrder = "DESC";
else
SortOrder = "ASC";
}
else
{
SortOrder = "ASC";
SortExp = e.SortExpression.ToString();
}
}
strViewStateDetails[0] = SortExp.ToString();
strViewStateDetails[1] = SortOrder.ToString();
return strViewStateDetails;
}
}
}
srep 2: aspx page
<asp:GridView ID="dgvContent" AllowSorting="true" CellPadding="5" BorderStyle="None"
AllowPaging="true" Width="100%" CellSpacing="5" PageSize="10" AutoGenerateColumns="false"
runat="server" OnPageIndexChanging="dgvContent_PageIndexChanging" PagerSettings-Mode="Numeric"
PagerSettings-PageButtonCount="4" OnRowCommand="dgvContent_RowCommand" OnRowDataBound="dgvContent_RowDataBound"
OnSorting="dgvContent_Sorting">
<RowStyle />
<AlternatingRowStyle />
<PagerStyle HorizontalAlign="Left" CssClass="pager" />
<HeaderStyle BackColor="#F1A626" />
<EmptyDataTemplate>
No Record Found
</EmptyDataTemplate>
<Columns>
<asp:BoundField DataField="LeadId" HeaderText="Lead Id" HeaderStyle-Height="35" HeaderStyle-ForeColor="Black" />
<asp:BoundField DataField="UserName" HeaderStyle-ForeColor="Black" HeaderStyle-Height="35"
HeaderText="UserName" Visible="false" />
<asp:BoundField DataField="Name" HeaderStyle-ForeColor="Black" HeaderStyle-Height="35"
HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Phone" HeaderText="Phone" HeaderStyle-Height="35" HeaderStyle-ForeColor="Black" />
<asp:BoundField DataField="LeadCategoryName" HeaderText="Status" HeaderStyle-Height="35"
HeaderStyle-ForeColor="Black" Visible="false" />
<asp:TemplateField HeaderText="Lead Status" HeaderStyle-ForeColor="Black">
<ItemTemplate>
<asp:Label ID="lblLeadId" runat="server" Text='<%# Eval("LeadId") %>' Visible="false" />
<asp:Label ID="lblleadstatus" runat="server" Text='<%# Eval("LeadCategoryId") %>'
Visible="false" />
<asp:DropDownList runat="server" ID="ddlleadstatus" AutoPostBack="true" OnSelectedIndexChanged="ddlleadstatus_SelectedIndexChanged">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="LeadCategoryId" HeaderText="LeadCategoryId" HeaderStyle-Height="35"
HeaderStyle-ForeColor="Black" Visible="false" />
<asp:BoundField DataField="ReferredBy" HeaderText="Referred By" HeaderStyle-ForeColor="Black" />
<asp:TemplateField HeaderText="Start Planning" HeaderStyle-ForeColor="Black">
<ItemTemplate>
<asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="../images/track1.jpg"
CausesValidation="false" CommandName="start" CommandArgument='<%#Eval("LeadId")+","+ Eval("UserName")%>'
Text="Start Plan" ToolTip="Start Planning" />
<%-- <asp:LinkButton ID="lnkSetCredentials" runat="server" CommandName="Set" CommandArgument='<%#Eval("LeadId")+","+ Eval("Email")%>'
Text="Set"></asp:LinkButton>--%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" HeaderStyle-ForeColor="Black">
<ItemTemplate>
<asp:ImageButton ID="imgEdit" runat="server" ImageUrl="~/images/Edit.jpg" Height="23"
Width="23" CausesValidation="false" CommandName="Edit" CommandArgument='<%#Eval("LeadId")%>'
Text="Edit" ToolTip="Edit Lead" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<table style="height: 15px; text-align: left; vertical-align: top; width: 100%;">
<tr>
<td style="width: 200px; height: 10px; text-align: center; vertical-align: middle;">
<b><i>
<%=dgvContent.PageIndex + 1%>of<%=dgvContent.PageCount%></i></b>
</td>
</tr>
</table>
step 3: protected void dgvContent_Sorting(object sender, GridViewSortEventArgs e)
{
string[] ViewStateValues = SortingClass.Instance.Sorting(this.ViewState["SortExp"], this.ViewState["SortOrder"], e);
if (ViewStateValues.Length > 0)
{
this.ViewState["SortExp"] = ViewStateValues[0];
this.ViewState["SortOrder"] = ViewStateValues[1];
}
BindGrid();
}
step 4:
private void BindGrid()
{
DataSet ds = GetLeadData();
try
{
if (ds.Tables[0].Rows.Count > 0)
{
DataView dv = ds.Tables[0].DefaultView;
if (ViewState["SortExp"] != null)
{
dv.Sort = this.ViewState["SortExp"].ToString() + " " + this.ViewState["SortOrder"].ToString();
dgvContent.DataSource = dv.ToTable();
dgvContent.DataBind();
}
else
{
dgvContent.DataSource = ds;
dgvContent.DataBind();
}
}
else
{
dgvContent.DataSource = null;
dgvContent.DataBind();
}
}
catch (Exception ex)
{
}
}
No comments:
Post a Comment