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.