We help IT Professionals succeed at work.
Get Started

Sorting on datagridview column loses the backcolor of the selected rows

3,257 Views
Last Modified: 2012-05-09
Hi,
I have programmatic sorting on datagridview columns and the code is :

    Private Sub SortDgColumn(ByVal sender As Object, ByVal intColindex As Integer, ByVal DoSorting As Boolean)
        'Dosorting is flag to allow descending sorting on the grid columns this is used for Resetting or Selecting all fields as the datagridview should not sort if the selection column is not sorted.
        Dim int As Integer
        int = intColindex


        Me.Cursor = Cursors.WaitCursor
        Dim newColumn As DataGridViewColumn = _
               sender.Columns(int)
        Dim oldColumn As DataGridViewColumn = sender.SortedColumn

        Dim direction As ListSortDirection
        'todo
        ' If oldColumn is null, then the DataGridView is not currently sorted.
        If Not oldColumn Is Nothing Then

            ' Sort the same column again, reversing the SortOrder.
            If oldColumn Is newColumn AndAlso sender.SortOrder = _
                SortOrder.Ascending Then
                If DoSorting = True Then direction = ListSortDirection.Descending
            Else
                ' Sort a new column and remove the old SortGlyph.
                direction = ListSortDirection.Ascending
                oldColumn.HeaderCell.SortGlyphDirection = SortOrder.None
            End If
        Else
            direction = ListSortDirection.Ascending
        End If
        ' Sort the selected column.
        If IsNothing(sender.CurrentRow) Then Exit Sub


        sender.Sort(newColumn, direction)
        If direction = ListSortDirection.Ascending Then
            newColumn.HeaderCell.SortGlyphDirection = SortOrder.Ascending
        Else
            If DoSorting = True Then newColumn.HeaderCell.SortGlyphDirection = SortOrder.Descending
        End If
        Me.Cursor = Cursors.Default

    End Sub


But as soon as I sort a column I lose all the selected rows backcolor.
Any suggestions on how can I retain it?

Cheers
Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 14 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE