I am trying to sort a DataGridView control by a column that contains DBNull values. This DataGridView control is bound to an SQL Server 2012 Database.

When I click the headercell (to sort in ascending order), the DBNull values are sorted ahead of the rest of the integer values, so that the top rows in the column are all blank, and then after that the integer values 1,2,3 etc are in ascending order. (DBNull, DBNull, 1, 2, 3)

How do I get around this? I would rather the DataGridView control sort the rows with values at the top, followed by the DBNulls. (1, 2, 3, DBNull, DBNull)

I have tried to insert a higher value into the blank cells, which then sorts them correctly, but when I return those values back to System.DBNull.value, the sort order reverts back to the way above.

My code is as follows:

Code:
If dgv.Columns(e.ColumnIndex).Name.EndsWith("Position") Then
    intSortingRunningPosition = 1000
    dgv.Sort(baseColumn, System.ComponentModel.ListSortDirection.Ascending)
    newColumn.HeaderCell.SortGlyphDirection = Windows.Forms.SortOrder.Ascending
    For Each dr As DataGridViewRow In dgv.Rows
        If dr.Cells(e.ColumnIndex).Value Is System.DBNull.Value Then 
            dr.Cells(e.ColumnIndex).Value = intSortingRunningPosition
            intSortingRunningPosition += 1
        End If
    Next
    dgv.Sort(newColumn, System.ComponentModel.ListSortDirection.Ascending)
    For Each dr As DataGridViewRow In dgv.Rows
        If dr.Cells(e.ColumnIndex).Value >= 1000 Then
            dr.Cells(e.ColumnIndex).Value = System.DBNull.Value
        End If
    Next
End If
I have also tried the following, using the Sort(IComparer) Method, but had little success:

Code:
Public Function Compare(ByVal x As Object, ByVal y As Object) As Integer _
     Implements System.Collections.IComparer.Compare

     Dim DataGridViewRow1 As DataGridViewRow = CType(x, DataGridViewRow)
     Dim DataGridViewRow2 As DataGridViewRow = CType(y, DataGridViewRow)
     Dim CompareResult As Integer
     Dim intDGV1Cell0Value As Integer = Nothing
     Dim intDGV1Cell1Value As Integer = Nothing
     Dim intDGV2Cell0Value As Integer = Nothing
     Dim intDGV2Cell1Value As Integer = Nothing

     Try
         intDGV1Cell0Value = CInt(DataGridViewRow1.Cells(0).Value)
     Catch ex As Exception
         intDGV1Cell0Value = Int32.MaxValue
     End Try
     Try
         intDGV1Cell1Value = CInt(DataGridViewRow1.Cells(1).Value)
     Catch ex As Exception
         intDGV1Cell1Value = Int32.MaxValue
     End Try
     Try
         intDGV2Cell0Value = CInt(DataGridViewRow2.Cells(0).Value)
     Catch ex As Exception
         intDGV2Cell0Value = Int32.MaxValue
     End Try
     Try
         intDGV2Cell1Value = CInt(DataGridViewRow2.Cells(1).Value)
     Catch ex As Exception
         intDGV2Cell1Value = Int32.MaxValue
     End Try

     ' Try to sort based on the Last Name column.
     CompareResult = System.String.Compare(intDGV1Cell1Value, intDGV2Cell1Value)

     ' If the Last Names are equal, sort based on the First Name. 
         If CompareResult = 0 Then
             CompareResult = System.String.Compare(intDGV1Cell0Value, intDGV2Cell0Value)
         End If

     Return CompareResult * sortOrderModifier
 End Function
Any ideas on what the best way to achieve this may be?

Thanks