# VBForums CodeBank > CodeBank - Visual Basic .NET >  Moving Rows in Datagridview

## vijy

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


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



```
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


```
    'Bind a DataTable in Datagridview.
     Button1.Text="Top"
     Button2.Text="Up"
     Button3.Text="Down" 
     Button4.Text="Bottom"
```

3.Create an Enum.


```
  Enum mode
    top = 0
    up = 1
    down = 2
    bottom = 3
  End Enum
```

5.Add the Below three Sub -->swapRows,toSelect,reshuffleRows


```

 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


```
swapRows(mode.top)
```

On Up Button Click


```
swapRows(mode.up)
```

On Down Button Click


```
swapRows(mode.down)
```

On Bottom Button Click


```
swapRows(mode.bottom)
```


 :Thumb:  :Thumb:  

There might be lot of ways to do this.. if there anything simply to do this means, share with VBF.. :Roll Eyes (Sarcastic):

----------


## asif28

> 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
> 
> 
> ```
>    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
> ...





> This is to move Datagridview Rows up,Down,Top and Bottom.
> 
> 
> Here the main thing is, i binded value in datagridview using datatable.
> 
> 
> 
> ```
> Datagridview.Datasource=DataTable
> ...




```

	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
> 
> 
> ```
>    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
> ...


Any ways Thanks Alot for this code it had help me alot ,look forward with ur interactions :Smilie:  :Smilie:  :Smilie:  :Smilie:

----------


## vijy

do you mean if you click top it has to perform like round trip...

----------


## asif28

> 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

----------


## Jackwalkerii

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

----------

