Sort a DataGridView with zeros in a DateTime column

I have a DataGridView control in a windows forms application. There are four columns with string data and three with DateTime data. I add lines programmatically using the Rows.Add () method. For all columns, the SortMode parameter is set to Automatic. Clicking on column headers to sort just works, with the exception of one DateTime column, which has multiple zeros. When the user clicks on the header of this column, he throws an ArgumentException: Object must be of type DateTime.

I know a difficult way around this: setting all SortModes to NotSortable, handling the ColumnHeaderMouseClick event, and sorting it all manually. I am looking for an easy way.

Is there a property or something I can set, or some other relatively simple way to allow this column to sort with zeros in it?

+3
source share
5 answers

Here is the solution I came up with. The DataGridView raises the SortCompare event, which you can use to enter custom sorting. I handle this event and make the null value higher than non-zero values ​​(you can just as easily make nulls lower than non-zero). Here is the VB code. I assume that each cell value is IComparable (unless it is handled by the usual error handling logic).

Try
    If e.CellValue1 Is Nothing OrElse e.CellValue1.Equals(DBNull.Value) Then
        If e.CellValue2 Is Nothing OrElse e.CellValue2.Equals(DBNull.Value) Then
            e.SortResult = 0
        Else
            e.SortResult = 1
        End If
    Else
        If e.CellValue2 Is Nothing OrElse e.CellValue2.Equals(DBNull.Value) Then
            e.SortResult = -1
        Else
            e.SortResult = DirectCast(e.CellValue1, IComparable).CompareTo(DirectCast(e.CellValue2, IComparable))
        End If
    End If
    e.Handled = True
Catch ex As Exception
    HandleError("Error sorting result grid values", ex)
    Close()
End Try

If anyone has any improvements, please feel free to post them.

+2

"tonull", e.cellvalue1 2 , . "", 01/01/1001, , 01/01/3001 - , , .

Private Sub dgvTable_SortCompare(ByVal sender As Object, ByVal e As DataGridViewSortCompareEventArgs) Handles dgvTable.SortCompare

    If e.Column.Index = 4 Then

        e.SortResult = System.DateTime.Compare(todatenull(e.CellValue1), todatenull(e.CellValue2))

    End If

    e.Handled = True
End Sub


Function todatenull(ByVal cellvalue)
    If cellvalue = "" Then
        Return "01/01/1001"
    Else
        Return cellvalue
    End If
End Function
+4

, , DataGridView . , - (- , , - , , ) , , .

- DataGridView , ( ) , ?

+1

: , , DBNull.Value . , DBNull.Value DateTime. , DBNull.Value null (Nothing).


: , DateTime, :

myDateTimeColumn.ValueType = GetType(DateTime)

MSDN, ValueType . " , .

With this set of properties, the grid can be sorted by a column with zeros and cause the inserted / updated cells to convert to DateTime.

+1
source

Just to add code to this question ... I did the following so that sorting works correctly for DateTime (and other non-string types) if there are zeros:

public MyFormCTor() {
    ...
    m_dataGridView.SortCompare += MySortCompare;
    ...
}

...

static void MySortCompare(object sender, DataGridViewSortCompareEventArgs e)
{
    if (e.CellValue1 == e.CellValue2)
    {
        e.SortResult = 0;
        return;
    }

    if (e.CellValue1 == null)
    {
        e.SortResult = -1;
        return;
    }

    if (e.CellValue2 == null)
    {
        e.SortResult = 1;
        return;
    }

    e.SortResult = ((IComparable)e.CellValue1).CompareTo(e.CellValue2);
}
0
source

Source: https://habr.com/ru/post/1708454/


All Articles