0

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();
Matthias Meid
  • 12,455
  • 7
  • 45
  • 79
samiaj
  • 421
  • 1
  • 5
  • 15
  • Your problem for yet more answers ,try this blogs: http://stackoverflow.com/questions/138412/how-to-sort-columns-in-an-asp-net-gridview-if-using-a-custom-datasource , http://www.codeproject.com/Questions/448965/How-to-Sort-a-single-Column-in-Asp-net-Gridview – Elyor Apr 29 '13 at 11:07

1 Answers1

0

I think so too, as you're using this code in your Eval:

Convert.ToDateTime(Eval("CreatedOnDate"))

Could you alter your model so that CreatedOnDate is actually a DateTime? You can still use ToString with different formats, but the sortable data would be a DateTime and therefore be sorted properly. Your model could possibly look like this:

class MyModel
{
  public DateTime CreatedOnDate { get; set; } // DateTime instead of string
}

The sorting mechanism automatically sorts properly according to the IComparable implementation of the type (string or DateTime or whatever).

Matthias Meid
  • 12,455
  • 7
  • 45
  • 79
  • Thanks for your fast reply.i will try and let you know – samiaj Apr 29 '13 at 10:54
  • @sami You need to change your underlying model, not the markup. Sorting is done right automatically. See my edited answer. – Matthias Meid Apr 29 '13 at 10:57
  • sorry . i am not getting the logic to implement this.In My aspx.cs page i am just binding the datasource.Here is the code for that.dvSortedView.Sort = e.SortExpression + " " + getSortDirectionString(); ViewState["sortExpression"] = e.SortExpression; grdSearchResults.DataSource = dvSortedView; grdSearchResults.DataBind(); – samiaj Apr 29 '13 at 11:03
  • @sami You must change the *data source* rather than the markup. Please post your data source object for us, so we can suggest changes. – Matthias Meid Apr 29 '13 at 11:04
  • ... or the SQL statement or whatever gives you the data view ;) – Matthias Meid Apr 29 '13 at 11:05
  • Matthias Meid,Here is the code from code behind.can u suggest me the changes.DataView dvSortedView = new DataView(dtSortTable); dvSortedView.Sort = e.SortExpression + " " + getSortDirectionString(); ViewState["sortExpression"] = e.SortExpression; grdSearchResults.DataSource = dvSortedView; grdSearchResults.DataBind(); – samiaj Apr 29 '13 at 11:31
  • @sami I put this into your question. However, this is not the *actual data source*, but the *binding logic*. What we need to check the type is **how the data for `dvSortedView` is gathered**: If it is an SQL query, put the query and the SQL column types into your question (edit). If it is something else (like LINQ, or C# code), edit it into your edit. – Matthias Meid Apr 29 '13 at 11:47