Solved

DataGridView Date Sorting

Posted on 2007-04-10
1
1,180 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

775 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