Solved

DataGridView Date Sorting

Posted on 2007-04-10
1
1,181 Views
Last Modified: 2012-06-21
I am using a datagridview in a project.  If I insert a date using Convert.ToDateTime(x).ToShortTimeString, if I click on the column header to sort, it sorts it by the month, rather than the full date.  How do I correct this??

example of sort

4/1/2006
3/1/2001
1/1/2008
0
Comment
Question by:Rowdyone52
1 Comment
 
LVL 96

Accepted Solution

by:
Bob Learned earned 500 total points
ID: 18884858
You can use the DataGridView.SortCompare event to customize the sorting of the DataGridView control:

How to: Customize Sorting in the Windows Forms DataGridView Control  
http://msdn2.microsoft.com/en-us/library/ms171608(VS.80).aspx

Example:

private void dataGridView1_SortCompare(object sender,
        DataGridViewSortCompareEventArgs e)
    {
        // Try to sort based on the cells in the current column.
        e.SortResult = System.String.Compare(
            e.CellValue1.ToString(), e.CellValue2.ToString());

        // If the cells are equal, sort based on the ID column.
        if (e.SortResult == 0 && e.Column.Name != "ID")
        {
            e.SortResult = System.String.Compare(
                dataGridView1.Rows[e.RowIndex1].Cells["ID"].Value.ToString(),
                dataGridView1.Rows[e.RowIndex2].Cells["ID"].Value.ToString());
        }
        e.Handled = true;

Bob
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

837 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question