How to sort row column as date column in gridview

I want to order a column as a grid in a view datetime

, but I feel like it is ordered as a row instead of:

My code:

if (SortExpression.ToString() == "TransDate")
{
    if (SortDirection == SortDirection.Ascending)
    {
        gv_Details1.DataSource = TransactionList.OrderBy(t =>  DateTime.Parse(t.TransDate)).ToList<UserTransactionDTO>();
    }
    else
    {
        gv_Details1.DataSource = TransactionList.OrderByDescending(t => DateTime.Parse(t.TransDate)).ToList<UserTransactionDTO>();
    }
}

      


My aspx:

   <asp:BoundField DataField="TransDate" HeaderText="Date" SortExpression="TransDate">

      


Part of the result ::

23/12/2012 09:51
27/9/2012 11:36
3/10/2012 12:28
2/10/2012 10:51

      

+3


source to share


3 answers


I think the problem is that you are using DateTime.Parse

linq in collation and there are cases in your line where the first and second parts could be day or month ... remember that the parsing happens on a per element basis, so only because that first one parsing in one direction does not mean that the other will parse the same way.

Your default region is probably month one, which doesn't work for the first pair, so it knows the first part is day, so it uses that format. For last couple dates, the first part works month, so it parses the default month first. Or vice versa:)

Try to use DateTime.ParseExact

like this:



CultureInfo provider = CultureInfo.InvariantCulture;
string format = "dd/MM/yyyy hh:mm";

gv_Details1.DataSource = TransactionList.
    OrderBy(t =>  DateTime.ParseExact(t.TransDate, format, provider)).
    ToList<UserTransactionDTO>();

      

I hope I chose the right format, but if not just fine-tune it.

+3


source


The result doesn't look like a string sorted to me ... Below is a quick example that sorting is correct which uses parsing and a list

static void Main(string[] args)
{
    List<String> datestrings = new List<string>()
    {
        "12/23/2012 09:51",
        "9/27/2012 11:36",
        "10/2/2012 12:28",
        "10/3/2012 10:51"
    };
    List<DateTime> dates = datestrings.Select(a => DateTime.Parse(a)).OrderBy(a => a).ToList();
    foreach (var d in dates)
    {
        Console.WriteLine(d);
    }

    Console.ReadLine();
}

      

The lines above are sorted.

I first have to make sure that the grid does not reorder you after setting the dataset for any reason by storing TransactionList.OrderBy(t => Da...

in a temporary variable and making sure it is properly ordered before setting up the datasource.



I would then check to see if there is a culture issue with your dates (or use ParseExact) (although nothing strikes me as if you posted in the current order).

Finally, and I am not ready for speeding up asp binding, but maybe the way you bind to the dataset you need to point out that a date like the following link eludes: http://forums.asp.net/t/ 1001482.aspx / 1

An excerpt from the site recommends:

< asp:boundfield datafield="Your_Date_Column" dataformatstring="{0:MMMM d, yyyy}" htmlencode="false" />

      

+2


source


When I did it with just SQL, I added an "order by" statement to the T-SQL attached to the grid view.

0


source







All Articles