Results 1 to 5 of 5

Thread: Moving Rows in Datagridview

  1. #1

    Thread Starter
    Fanatic Member vijy's Avatar
    Join Date
    May 2007
    Location
    India
    Posts
    548

    Moving Rows in Datagridview

    This is to move Datagridview Rows up,Down,Top and Bottom.


    Here the main thing is, i binded value in datagridview using datatable.

    Code:
    Datagridview.Datasource=DataTable
    Note:If the datagridview is binded in Row by Row method, its easy to move the Rows.

    Follow the Steps.
    1.Add a Datagridview and Four Buttons in a Form.
    2.On Form_Load
    Code:
        'Bind a DataTable in Datagridview.
         Button1.Text="Top"
         Button2.Text="Up"
         Button3.Text="Down" 
         Button4.Text="Bottom"
    3.Create an Enum.
    Code:
      Enum mode
        top = 0
        up = 1
        down = 2
        bottom = 3
      End Enum
    5.Add the Below three Sub -->swapRows,toSelect,reshuffleRows
    Code:
     Private Sub swapRows(ByVal range As mode)
                Dim iSelectedRow As Integer = -1
                For iTmp As Integer = 0 To Datagridview.Rows.Count - 1
                    If Datagridview.Rows(iTmp).Selected Then
                        iSelectedRow = iTmp
                        Exit For
                    End If
                Next
    
                If iSelectedRow <> -1 Then
                    Dim sTmp(4) As String
                    For iTmp As Integer = 0 To Datagridview.Columns.Count - 1
                        sTmp(iTmp) = Datagridview.Rows(iSelectedRow).Cells(iTmp).Value.ToString
                    Next
                    
                    Dim iNewRow As Integer
                    If range = mode.down Then
                        iNewRow = iSelectedRow + 1
                    ElseIf range = mode.up Then
                        iNewRow = iSelectedRow - 1
                    End If
                    
                    If range = mode.up Or range = mode.down Then
                        For iTmp As Integer = 0 To Datagridview.Columns.Count - 1
                            Datagridview.Rows(iSelectedRow).Cells(iTmp).Value = Datagridview.Rows(iNewRow).Cells(iTmp).Value
                            Datagridview.Rows(iNewRow).Cells(iTmp).Value = sTmp(iTmp)
                        Next
                        toSelect(iNewRow)
                    ElseIf range = mode.top Or range = mode.bottom Then
                        reshuffleRows(sTmp, iSelectedRow, range)
                    End If
                End If
        End Sub
    
    
     Private Sub toSelect(ByVal iNewRow As Integer)
            Datagridview.Rows(iNewRow).Selected = True
        End Sub
    
    
    
    Private Sub reshuffleRows(ByVal sTmp() As String, ByVal iSelectedRow As Integer, ByVal Range As mode)
            If Range = mode.top Then
                Dim iFirstRow As Integer = 0
                If iSelectedRow > iFirstRow Then
                    For iTmp As Integer = iSelectedRow To 1 Step -1
                        For iCol As Integer = 0 To Datagridview.Columns.Count - 1
                            Datagridview.Rows(iTmp).Cells(iCol).Value = Datagridview.Rows(iTmp - 1).Cells(iCol).Value
                        Next
                    Next
                    For iCol As Integer = 0 To Datagridview.Columns.Count - 1
                        Datagridview.Rows(iFirstRow).Cells(iCol).Value = sTmp(iCol).ToString
                    Next
                    toSelect(iFirstRow)
                End If
            Else
                Dim iLastRow As Integer = Datagridview.Rows.Count - 1
                If iSelectedRow < iLastRow Then
                    For iTmp As Integer = iSelectedRow To iLastRow - 1
                        For iCol As Integer = 0 To Datagridview.Columns.Count - 1
                            Datagridview.Rows(iTmp).Cells(iCol).Value = Datagridview.Rows(iTmp + 1).Cells(iCol).Value
                        Next
                    Next
                    For iCol As Integer = 0 To Datagridview.Columns.Count - 1
                        Datagridview.Rows(iLastRow).Cells(iCol).Value = sTmp(iCol).ToString
                    Next
                    toSelect(iLastRow)
                End If
            End If
        End Sub

    Finally, on the Button Click..

    On Top Button Click
    Code:
    swapRows(mode.top)
    On Up Button Click
    Code:
    swapRows(mode.up)
    On Down Button Click
    Code:
    swapRows(mode.down)
    On Bottom Button Click
    Code:
    swapRows(mode.bottom)



    There might be lot of ways to do this.. if there anything simply to do this means, share with VBF..
    Visual Studio.net 2010
    If this post is useful, rate it


  2. #2
    New Member
    Join Date
    Mar 2009
    Posts
    2

    Re: Moving Rows in Datagridview

    It works fine except it gives the error to move the row up beyond the first row and so implies with the last row for row down so if we provide check at to points it will work find
    Code:
       If range = mode.up Or range = mode.down Then
    
                    For iTmp As Integer = 0 To datagridview.Columns.Count - 1
                        If Not iNewRow = datagridview.Rows.Count And Not iNewRow = -1 Then
                            datagridview.Rows(iSelectedRow).Cells(iTmp).Value = datagridview.Rows(iNewRow).Cells(iTmp).Value
                            datagridview.Rows(iNewRow).Cells(iTmp).Value = sTmp(iTmp)
                        End If
                    Next
                    If Not iNewRow = datagridview.Rows.Count And Not iNewRow = -1 Then
                        toSelect(iNewRow, datagridview)
                    End If
                ElseIf range = mode.top Or range = mode.bottom Then
                    reshuffleRows(sTmp, iSelectedRow, range, datagridview)
                End If
    Any ways Thanks Alot for this code it had help me alot ,look forward with ur interactions
    Quote Originally Posted by vijy View Post
    This is to move Datagridview Rows up,Down,Top and Bottom.


    Here the main thing is, i binded value in datagridview using datatable.

    Code:
    Datagridview.Datasource=DataTable
    Note:If the datagridview is binded in Row by Row method, its easy to move the Rows.

    Follow the Steps.
    1.Add a Datagridview and Four Buttons in a Form.
    2.On Form_Load
    Code:
        'Bind a DataTable in Datagridview.
         Button1.Text="Top"
         Button2.Text="Up"
         Button3.Text="Down" 
         Button4.Text="Bottom"
    3.Create an Enum.
    Code:
      Enum mode
        top = 0
        up = 1
        down = 2
        bottom = 3
      End Enum
    5.Add the Below three Sub -->swapRows,toSelect,reshuffleRows
    Code:
     Private Sub swapRows(ByVal range As mode)
                Dim iSelectedRow As Integer = -1
                For iTmp As Integer = 0 To Datagridview.Rows.Count - 1
                    If Datagridview.Rows(iTmp).Selected Then
                        iSelectedRow = iTmp
                        Exit For
                    End If
                Next
    
                If iSelectedRow <> -1 Then
                    Dim sTmp(4) As String
                    For iTmp As Integer = 0 To Datagridview.Columns.Count - 1
                        sTmp(iTmp) = Datagridview.Rows(iSelectedRow).Cells(iTmp).Value.ToString
                    Next
                    
                    Dim iNewRow As Integer
                    If range = mode.down Then
                        iNewRow = iSelectedRow + 1
                    ElseIf range = mode.up Then
                        iNewRow = iSelectedRow - 1
                    End If
                    
                    If range = mode.up Or range = mode.down Then
                        For iTmp As Integer = 0 To Datagridview.Columns.Count - 1
                            Datagridview.Rows(iSelectedRow).Cells(iTmp).Value = Datagridview.Rows(iNewRow).Cells(iTmp).Value
                            Datagridview.Rows(iNewRow).Cells(iTmp).Value = sTmp(iTmp)
                        Next
                        toSelect(iNewRow)
                    ElseIf range = mode.top Or range = mode.bottom Then
                        reshuffleRows(sTmp, iSelectedRow, range)
                    End If
                End If
        End Sub
    
    
     Private Sub toSelect(ByVal iNewRow As Integer)
            Datagridview.Rows(iNewRow).Selected = True
        End Sub
    
    
    
    Private Sub reshuffleRows(ByVal sTmp() As String, ByVal iSelectedRow As Integer, ByVal Range As mode)
            If Range = mode.top Then
                Dim iFirstRow As Integer = 0
                If iSelectedRow > iFirstRow Then
                    For iTmp As Integer = iSelectedRow To 1 Step -1
                        For iCol As Integer = 0 To Datagridview.Columns.Count - 1
                            Datagridview.Rows(iTmp).Cells(iCol).Value = Datagridview.Rows(iTmp - 1).Cells(iCol).Value
                        Next
                    Next
                    For iCol As Integer = 0 To Datagridview.Columns.Count - 1
                        Datagridview.Rows(iFirstRow).Cells(iCol).Value = sTmp(iCol).ToString
                    Next
                    toSelect(iFirstRow)
                End If
            Else
                Dim iLastRow As Integer = Datagridview.Rows.Count - 1
                If iSelectedRow < iLastRow Then
                    For iTmp As Integer = iSelectedRow To iLastRow - 1
                        For iCol As Integer = 0 To Datagridview.Columns.Count - 1
                            Datagridview.Rows(iTmp).Cells(iCol).Value = Datagridview.Rows(iTmp + 1).Cells(iCol).Value
                        Next
                    Next
                    For iCol As Integer = 0 To Datagridview.Columns.Count - 1
                        Datagridview.Rows(iLastRow).Cells(iCol).Value = sTmp(iCol).ToString
                    Next
                    toSelect(iLastRow)
                End If
            End If
        End Sub

    Finally, on the Button Click..

    On Top Button Click
    Code:
    swapRows(mode.top)
    On Up Button Click
    Code:
    swapRows(mode.up)
    On Down Button Click
    Code:
    swapRows(mode.down)
    On Bottom Button Click
    Code:
    swapRows(mode.bottom)



    There might be lot of ways to do this.. if there anything simply to do this means, share with VBF..
    Code:
    Code:
    HTML Code:
    [HTML][HTML]
    [/HTML][/HTML]
    It works fine except it gives the error to move the row up beyond the first row and so implies with the last row for moving the row down so if we can provide check that it is the first row or the last at to points it will work fine
    Code:
       If range = mode.up Or range = mode.down Then
    
                    For iTmp As Integer = 0 To datagridview.Columns.Count - 1
                        If Not iNewRow = datagridview.Rows.Count And Not iNewRow = -1 Then
                            datagridview.Rows(iSelectedRow).Cells(iTmp).Value = datagridview.Rows(iNewRow).Cells(iTmp).Value
                            datagridview.Rows(iNewRow).Cells(iTmp).Value = sTmp(iTmp)
                        End If
                    Next
                    If Not iNewRow = datagridview.Rows.Count And Not iNewRow = -1 Then
                        toSelect(iNewRow, datagridview)
                    End If
                ElseIf range = mode.top Or range = mode.bottom Then
                    reshuffleRows(sTmp, iSelectedRow, range, datagridview)
                End If
    As this is my first post ,I tried my best to correct the code.Any ways Thanks Alot for this code it had help me alot ,look forward with ur interactions
    Any ways Thanks Alot for this code it had help me alot ,look forward with ur interactions

  3. #3

    Thread Starter
    Fanatic Member vijy's Avatar
    Join Date
    May 2007
    Location
    India
    Posts
    548

    Re: Moving Rows in Datagridview

    do you mean if you click top it has to perform like round trip...
    Visual Studio.net 2010
    If this post is useful, rate it


  4. #4
    New Member
    Join Date
    Mar 2009
    Posts
    2

    Re: Moving Rows in Datagridview

    Quote Originally Posted by vijy View Post
    do you mean if you click top it has to perform like round trip...
    Hey I had modified ur code already,it will not perform like round trip instead it will stop at 2nd last as well as 2nd from top on move up and move down

  5. #5
    New Member
    Join Date
    Jan 2012
    Posts
    1

    Re: Moving Rows in Datagridview

    Change the top of Sub Swap Rows.
    So now if you selected on a cell it works. Instead of having to select the Entire row.

    Private Sub swapRows(ByVal range As mode)
    If Not DataGridView1.Rows(DataGridView1.CurrentCellAddress.Y).Selected Then DataGridView1.Rows(DataGridView1.CurrentCellAddress.Y).Selected = True
    Dim iSelectedRow As Integer = DataGridView1.CurrentCellAddress.Y

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width