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
On Up Button Click
On Down Button Click
Code:
swapRows(mode.down)
On Bottom Button Click
Code:
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..:rolleyes:
Re: Moving Rows in Datagridview
Quote:
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
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
On Up Button Click
On Down Button Click
Code:
swapRows(mode.down)
On Bottom Button Click
Code:
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..:rolleyes:
Quote:
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:):):):)
Re: Moving Rows in Datagridview
do you mean if you click top it has to perform like round trip...
Re: Moving Rows in Datagridview
Quote:
Originally Posted by
vijy
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
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.
Quote:
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