sort the rows of a datagridview but not the last two

Respondida sort the rows of a datagridview but not the last two

  • domingo, 12 de agosto de 2012 18:15
     
      Tiene código

    Hi,

    with this code, the last row isn't sorted:

    Protected Overrides Sub OnSortCompare(ByVal e As System.Windows.Forms.DataGridViewSortCompareEventArgs)
            MyBase.OnSortCompare(e)
            If e.RowIndex1 = Me.RowCount - 1 Then e.Handled = True
            If e.RowIndex2 = Me.RowCount - 1 Then e.Handled = True
    End Sub

    How can I expand this code to do not sort the second last row too?

    This doesn't work:

    Protected Overrides Sub OnSortCompare(ByVal e As System.Windows.Forms.DataGridViewSortCompareEventArgs)
            MyBase.OnSortCompare(e)
            If e.RowIndex1 = Me.RowCount - 1 Then e.Handled = True
            If e.RowIndex2 = Me.RowCount - 1 Then e.Handled = True
            If e.RowIndex1 = Me.RowCount - 2 Then e.Handled = True
            If e.RowIndex2 = Me.RowCount - 2 Then e.Handled = True
    End Sub


    • Editado DerStauner domingo, 12 de agosto de 2012 18:15
    •  

Todas las respuestas

  • domingo, 12 de agosto de 2012 18:24
    Moderador
     
     
    Can you simply take the last two rows out, perform your sort, and then append the last two rows again?

    If you want something you've never had, you need to do something you've never done.

  • lunes, 13 de agosto de 2012 17:28
     
     
    unfortunatelly, no I can't.
  • martes, 14 de agosto de 2012 6:55
    Moderador
     
      Tiene código

    Hi DerStauner,

    I’m afraid that your code is far enough for us to reappearance your issue. So would you like to share more information about your issue?  Here is my sample to test your code, it seems work well.

       Dim DGV1 As New DGV
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
            Dim c1 As New DataGridViewTextBoxColumn()
            c1.Name = "ID"
            Dim c2 As New DataGridViewTextBoxColumn()
            c2.Name = "name"
            DGV1.Columns.Add(c1)
            DGV1.Columns.Add(c2)
            DGV1.Rows.Add("12", "a")
            DGV1.Rows.Add("10", "c")
            DGV1.Rows.Add("9", "d")
            DGV1.Rows.Add("12", "b")
            DGV1.Rows.Add("2", "f")
            DGV1.VirtualMode = False
            Me.Controls.Add(DGV1)
            'End Using
        End Sub
        Public Class DGV
            Inherits DataGridView
            Protected Overrides Sub OnSortCompare(ByVal e As System.Windows.Forms.DataGridViewSortCompareEventArgs)
                MyBase.OnSortCompare(e)
                If e.RowIndex1 = Me.RowCount - 1 Then e.Handled = True
                If e.RowIndex2 = Me.RowCount - 1 Then e.Handled = True
            End Sub
        End Class
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
            DGV1.Sort(DGV1.Columns("ID"), System.ComponentModel.ListSortDirection.Ascending)
        End Sub

    I look forward your reply.


    Mark Liu-lxf [MSFT]
    MSDN Community Support | Feedback to us

  • martes, 14 de agosto de 2012 19:35
     
     

    Hi Mark Liu,

    your code is of course correct and works fine. But as I wrote, this case works for me too. In your code you fix the last row. But I would like to fix the last two row. And that is, what doesn't work for me. Could you complete your code according to this?

  • miércoles, 15 de agosto de 2012 3:40
    Moderador
     
      Tiene código

    Hi DerStauner,

    I have no idea about what do you mean “But I would like to fix the last two row”.  My code seems sort correct all of the data in the datagridview. Would you like to share a sample about this?

    Please pay attention that the sort method of ID is according to the string, so”10” will be before than “2”. If you want to compare the ID as integer, you need to change to your code to compare with integer instead of string. Here is a sample (change the OnSortCompare method with following code):

    Protected Overrides Sub OnSortCompare(ByVal e As System.Windows.Forms.DataGridViewSortCompareEventArgs)
                MyBase.OnSortCompare(e)
                'If e.RowIndex1 = Me.RowCount - 1 Then e.Handled = True
                Dim a As Integer = CInt(e.CellValue1.ToString())
                Dim b As Integer = CInt(e.CellValue2.ToString())
                e.SortResult = a.CompareTo(b)
                e.Handled = True
                If e.RowIndex2 = Me.RowCount - 1 Then e.Handled = True
            End Sub

    Hope this helps.


    Mark Liu-lxf [MSFT]
    MSDN Community Support | Feedback to us

  • miércoles, 15 de agosto de 2012 14:15
     
     
    IMHO, the index of a row is the result of sorting so it can not be the criterion for sorting. In other words, these two rows must be something special why you don't want to sort them. Is there is anything stored in them that makes you recognize them as "do not sort"? Do they contain a special value?

    Armin

  • miércoles, 15 de agosto de 2012 18:00
     
     
    I have a dgv, I populate it with data and then I add two empty rows to the dgv. I have created a disable style for this rows too. This two rows should remain always on bottom, regardless, what column the user clicked on. So they contain no data, but I can use the tag property. Aware of this, how should be look the code?
  • miércoles, 15 de agosto de 2012 18:30
     
     Respuesta propuesta
    • "I have created a disable style"

    Check that style during sorting to keep those rows at the bottom. 

    • "but I can use the tag property"

    Yes, also an option.


    Armin

  • domingo, 19 de agosto de 2012 6:44
     
     

    "Check that style during sorting to keep those rows at the bottom. "

    (sorry for the late answer, I was ill.)

    But exactly that's my problem: how can I keep those rows at the bottom?

    Can you provide me a simple sample code?
    • Editado DerStauner domingo, 19 de agosto de 2012 6:45
    •  
  • domingo, 19 de agosto de 2012 9:21
     
     Respondida Tiene código

    Untested:

          Protected Overrides Sub OnSortCompare(ByVal e As System.Windows.Forms.DataGridViewSortCompareEventArgs)
    
             Dim row1 = Rows(e.RowIndex1)
             Dim row2 = Rows(e.RowIndex2)
    
             Dim tag1 = DirectCast(row1.Tag, Boolean)
             Dim tag2 = DirectCast(row2.Tag, Boolean)
    
             If tag1 Then
                e.SortResult = If(tag2, 0, 1)
             Else
                If tag2 Then
                   e.SortResult = -1
                Else
                   MyBase.OnSortCompare(e)
                End If
             End If
    
          End Sub

    I assume that Tag is True if it's one of the last two rows. I don't know how you want to sort these two rows among each other.

    Maybe you also have to set e.handled = true in the first three cases. 

    Possibly you have to swap the values of -1 and 1 for e.SortResult. Try it to find out.


    Armin

    • Marcado como respuesta DerStauner martes, 21 de agosto de 2012 15:35
    •  
  • martes, 21 de agosto de 2012 15:34
     
     
    unfortunatelly, it doesn't work (or I have made something wrong). At the end, I removed one row from the end and so only one row stays on bottom.