i am using girdview to display a search results and implemented sort functionality in it.But it's not working for column that display date.I am displaying the date in different format .for today's date just display time like(10:00 PM or 08:45 AM),for any date in present year display like(APR 22,DEC 15,JAN 30) and for other years display in this format(dd/mm/yyyy).Below is the code for this.
<asp:TemplateField HeaderText="CREATED DATE" SortExpression="CreatedOnDate" ItemStyle-Width="150" ItemStyle-Font-Bold="false" ItemStyle-Font-Size="Larger" HeaderStyle-HorizontalAlign="Left" HeaderStyle-VerticalAlign="Top" ItemStyle-HorizontalAlign="Left" ItemStyle-VerticalAlign="Top" >
<HeaderTemplate >
<asp:LinkButton ID="LinkButton4" runat="server" Text="CREATED DATE" CommandName="Sort" style="text-decoration:none" CommandArgument="CreatedOnDate" ></asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%# Convert.ToDateTime(Eval("CreatedOnDate")).ToString("yyyy")==DateTime.Today.ToString("yyyy")?(Convert.ToDateTime(Eval("CreatedOnDate")).ToString("MMMM dd") == DateTime.Today.ToString("MMMM dd") ? Convert.ToDateTime(Eval("CreatedOnDate")).ToString("hh:mm t.\\M.") : Convert.ToDateTime(Eval("CreatedOnDate")).ToString("MMM dd")):Convert.ToDateTime(Eval("CreatedOnDate")).ToString("dd/MM/yyyy") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
I think here date is treating as string.Can anybody help me to sort this column. This is the code behind:
dvSortedView.Sort = e.SortExpression + " " + getSortDirectionString();
ViewState["sortExpression"] = e.SortExpression;
grdSearchResults.DataSource = dvSortedView;
grdSearchResults.DataBind();