Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27251

Sorting DataBound DataGridView Columns

$
0
0
I have multiple runtime generated DataGridView, and I would like to sort columns with null values. These DataGridView are bound to a Sql Server 2008 database. I have tried using the ICompare Method, which did not work because the DataGridView was bound. Is there a better option?

How does the automatic sort work? The problem with automatic sorting is that the NULL values are placed at the top of an ascending list, and I would like them to be at the bottom of the list.

The code I have tried so far is:

Code:

Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) _
    Handles Button1.Click
        If RadioButton1.Checked = True Then
            DataGridView1.Sort(New RowComparer(SortOrder.Ascending))
        ElseIf RadioButton2.Checked = True Then
            DataGridView1.Sort(New RowComparer(SortOrder.Descending))
        End If
    End Sub

    Private Class RowComparer
        Implements System.Collections.IComparer

        Private sortOrderModifier As Integer = 1
        Private sortCompareModifier As Integer = 1

        Public Sub New(ByVal sortOrder As SortOrder)
            If sortOrder = sortOrder.Descending Then
                sortOrderModifier = -1
                sortCompareModifier = -1
            ElseIf sortOrder = sortOrder.Ascending Then
                sortOrderModifier = 1
                sortOrderModifier = 1
            End If
        End Sub

        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 intCompareResult As Integer = Nothing

            ' Try to sort based on the Last Name column.

            If DataGridViewRow1.Cells(1).Value.ToString = "" And Not DataGridViewRow2.Cells(1).Value.ToString = "" Then
                intCompareResult = 1 * sortCompareModifier
            ElseIf Not DataGridViewRow1.Cells(1).Value.ToString = "" And DataGridViewRow2.Cells(1).Value.ToString = "" Then
                intCompareResult = -1 * sortCompareModifier
            Else
                intCompareResult = System.String.Compare(DataGridViewRow1.Cells(1).Value.ToString(), DataGridViewRow2.Cells(1).Value.ToString())
            End If
           



            ' If the Last Names are equal, sort based on the First Name.
            If intCompareResult = 0 Then
                intCompareResult = System.String.Compare(DataGridViewRow1.Cells(0).Value.ToString(), DataGridViewRow2.Cells(0).Value.ToString())
            End If
            Return intCompareResult * sortOrderModifier
        End Function
    End Class

This code works well for a non-bound DataGridView.

I have also tried adding a column to my database, giving the column high values for Null Integers and maxdates for Null Dates for ascending, and the opposite for decending sorts, and then sorting with this column, but this does not seem to be the best way to achieve my goal.

I have also tried:

Code:

    Private Sub dgvOverview_SortCompare(sender As Object, e As System.Windows.Forms.DataGridViewSortCompareEventArgs) Handles dgvOverview.SortCompare
        Dim dgv As DataGridView = sender
        Dim intCheckpointNumberLength As Integer
        Dim intCheckpointNumber As Integer
        Dim strCheckpointName As String

        If strProjectType = strCircuit Then
            intCheckpointNumberLength = Len(dgv.Name) - 6
            intCheckpointNumber = Microsoft.VisualBasic.Right(dgv.Name, intCheckpointNumberLength)
        ElseIf strProjectType = strP2P Then
            intCheckpointNumberLength = Len(dgv.Name) - 13
            intCheckpointNumber = Microsoft.VisualBasic.Right(dgv.Name, intCheckpointNumberLength)
        End If

        strCheckpointName = "Checkpoint " & intCheckpointNumber & " "

        For Each col As DataGridViewColumn In dgv.Columns
            If col.Index = e.Column.Index Then
                If col.Name = strCheckpointName & "Time" Then
                    e.SortResult = System.DateTime.Compare(SortToNull(e.CellValue1, "Time"), SortToNull(e.CellValue2, "Time"))
                ElseIf col.Name.EndsWith("Position") Then
                    e.SortResult = System.DateTime.Compare(SortToNull(e.CellValue1, "Position"), SortToNull(e.CellValue2, "Position"))
                End If


                e.Handled = True

                Exit Sub
            End If
        Next

    End Sub

but I am not sure what event will call this procedure. I also don't think that the SortCompare method works with DataBound DataGridView.

Any ideas? I am pretty lost here...

Thanks

Viewing all articles
Browse latest Browse all 27251

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>