Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9193537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T21:11:21+00:00 2026-06-17T21:11:21+00:00

I am trying to sort a DataGridView control by a column that contains DBNull

  • 0

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.

How do I get around this? I would rather the DataGridView control sort the rows with values at the top, followed by the DBNulls.

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:

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

Sorry if this is confusing. Thanks!

Update:

I have modified my code to work with the IComparer method, and have come up with the following:

 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

The null value is now not sorting at all. Any ideas on what I am doing wrong here? I am sure I am a bit out of the ballpark as it is…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T21:11:22+00:00Added an answer on June 17, 2026 at 9:11 pm

    I was fairly unsucessful using the Sort(ICompare) method to sort this database because it was bound to an external dataset. I ended up modifying my connection string and refilling the datatable.

    My code is as follows:

    If DataType = "Integer" Then
        If Column = "Vehicle Number" Then
            strConnectionStringCase = "Select * From Timing ORDER BY CAST([Class] AS NVARCHAR(MAX)) " & Direction & ", CONVERT(int, RTRIM(CAST([" & Column & "] AS NVARCHAR(MAX)))) " & Direction
        End If        
    ElseIf DataType = "DateTime" Then
        strConnectionStringCase = "Select * From Timing ORDER BY CASE WHEN [" & Column & "] is NULL THEN 1 ELSE 0 END, [" & Column & "] " & Direction
    ElseIf DataType = "String" Then
        If Column = "Vehicle Number" Then
            strConnectionStringCase = "Select * From Timing ORDER BY LEN(CAST([" & Column & "] AS NVARCHAR(MAX))) " & Direction & ", CAST([Class] AS NVARCHAR(MAX)) " & Direction & ", CAST([" & Column & "] AS NVARCHAR(MAX)) " & Direction
        End If
    End If
    

    I call this subroutine on the header cell click event. I send the Column name, sort direction, and datatype to this subroutine.

    This code also allows me to sort the column depending on the type of data. If there are alphanumeric strings, I can sort them in a similar order as if they were only integers. The DBNull values are always placed at the end of the sort.

    Thanks for the help!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a data-bound DataGridView that I am trying to sort using an IComparer.
I am trying to sort a two-dimensional array by a timestamp column Descending. This
I currently have a DataGridView bound to a LIST that contains 4 fields. The
Trying to sort this out... I need a query that will combine both tables
I'm trying to sort a string array that contains numbers just by the letters
I'm trying to sort a column in my JTable. The column contains Long's (j
Trying to sort this array of objects according to (1) depth and (2) weight,
Been trying to sort this one out for a while. I'd really appreciate any
I am trying to sort pixel values of an image (example 80x20) from lowest
I am trying to sort a Vector list using the Java Collections frameworks. This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.