2
votes

the gridview column header takes the value like this from code behind protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { GridView grid = sender as GridView; DataRowView tableData = e.Row.DataItem as DataRowView;

    if (e.Row.RowType == DataControlRowType.Header)
    {
        e.Row.Cells[1].Text = System.DateTime.Now.AddMonths(-3).ToString("MMMM") ;
    }
}

How do I sort this particular column from code behind. file.asp

<asp:TemplateField ItemStyle-HorizontalAlign="Left" HeaderStyle-VerticalAlign="Bottom" ItemStyle-Wrap="False">
<ItemTemplate>
<asp:Label ID="lblMonth_1" runat="server"></asp:Label>
</ItemTemplate>
</asp:TemplateField>

Thanks.

1
Have you considered jQuery DataTable? - Josh C.
im sorry noo. i dont use jquery datatable.. - Sam

1 Answers

1
votes

You may not need to go to the code behind for this. What kind of database are you using?

Have you considered modifying your query to include another column?

select mydate, dateadd(m, -3, mydate) as olderdate from mytable;

Then you can just make your gridview field as:

<asp:BoundField DataField="olderdate" HeaderText="Older Date" SortExpression="olderdate" dataformatstring="{0:MMM}">
    <ItemStyle Wrap="False" />
</asp:BoundField>

And mark your gridview as AllowSorting="true".