static SortDirection GridViewSortDirection;
protected void chequegrid_Sorting(object sender, GridViewSortEventArgs e)
{
string sortExpression = e.SortExpression;
ViewState["SortExpression"] = sortExpression;
if (GridViewSortDirection == SortDirection.Ascending)
{
GridViewSortDirection = SortDirection.Descending;
SortGridView(sortExpression, "DESCENDING");
}
else
{
GridViewSortDirection = SortDirection.Ascending;
SortGridView(sortExpression, "ASCENDING");
}
}
public void SortGridView(string sortExpression, string direction)
{
if (sortExpression == "ss")
{
string s;
if(direction =="DESCENDING")
{
s = "order by dbo.registration.id desc";
}
else
{
s = "order by dbo.registration.id";
}
bindbysort(s);
}
}
public void bindbysort(string expre)
{
DataSet ds = new DataSet();
ds = ck.bindgrid(expre);
if (ds.Tables[0].Rows.Count > 0)
{
chequegrid.DataSource = ds;
Label1.Text = ds.Tables[0].Rows.Count.ToString() + " Result Found";
chequegrid.DataBind();
}
else
{
chequegrid.DataSource = null;
chequegrid.DataBind();
}
}
aspx
<asp:gridview id="chequegrid" runat="server" autogeneratecolumns="False" allowsorting="true" xmlns:asp="#unknown">
CellPadding="4" ForeColor="#333333" GridLines="None" EmptyDataText="There is no any cheque generated yet for this time duration."
CssClass="table-bordered table g" Width="100%" onsorting="chequegrid_Sorting">
<alternatingrowstyle backcolor="White" forecolor="#284775" />
<columns>
<asp:templatefield headertext="Customer Id" sortexpression="ss">
<itemtemplate>
<asp:label id="Label1" runat="server">
Text='<%# Eval("customer_reg_id") %>'></asp:label>
</itemtemplate>
</asp:templatefield>
<asp:templatefield headertext="Customer Name">
<itemtemplate>
<asp:label id="Label1" runat="server">
Text='<%# Eval("name") %>'></asp:label>
</itemtemplate>
</asp:templatefield>
<asp:templatefield headertext="Date From">
<itemtemplate>
<asp:label id="Label1" runat="server" text="<%# Convert.ToDateTime(Eval("date_from")).ToString("dd MMMM, yyyy") %>"></asp:label>
</itemtemplate>
</asp:templatefield>
<asp:templatefield headertext="Date To">
<itemtemplate>
<asp:label id="Label2" runat="server" text="<%# Convert.ToDateTime(Eval("date_to")).ToString("dd MMMM, yyyy") %>"> </asp:label>
</itemtemplate>
</asp:templatefield>
<asp:templatefield headertext="Cheque Amount">
<itemtemplate>
<asp:label id="Label3" runat="server" text="<%# Eval("income") +"/-" %>"></asp:label>
</itemtemplate>
</asp:templatefield>
<asp:templatefield headertext="Generated On">
<itemtemplate>
<asp:label id="Label4" runat="server" text="<%# Convert.ToDateTime(Eval("date_of_clear")).ToString("dd MMMM, yyyy") %>"></asp:label>
</itemtemplate>
</asp:templatefield>
</columns>
</asp:gridview>