How To Move Selected Datagridview Row Up N Down In Visual Basic.Net
In This VB.Net Tutorial We Will See How To Move Selected Datagridview Row Up Or Down On Button Click, In Visual Basic.Net Programming Language And Visual Studio Editor.
Project Source Code:
Public Class datagridview_row_up_down
Dim table As New DataTable("table")
' selected row index
Dim rowIndex As Integer
Private Sub datagridview_row_up_down_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' populate dgv from dataTable
' add columns to dataTable
table.Columns.Add("Id", Type.GetType("System.Int32"))
table.Columns.Add("First Name", Type.GetType("System.String"))
table.Columns.Add("Last Name", Type.GetType("System.String"))
table.Columns.Add("Age", Type.GetType("System.Int32"))
' add rows to dataTable
table.Rows.Add(1, "AA", "AA", 21)
table.Rows.Add(2, "BB", "BB", 33)
table.Rows.Add(3, "CC", "CC", 53)
table.Rows.Add(4, "DD", "DD", 19)
table.Rows.Add(5, "EE", "EE", 36)
table.Rows.Add(6, "FF", "FF", 63)
table.Rows.Add(7, "HH", "HH", 70)
table.Rows.Add(8, "KK", "KK", 80)
table.Rows.Add(9, "GG", "GG", 90)
table.Rows.Add(10, "MM", "MM", 100)
DataGridView1.DataSource = table
End Sub
Private Sub ButtonUP_Click(sender As Object, e As EventArgs) Handles ButtonUP.Click
' get the index of selected row
rowIndex = DataGridView1.SelectedCells(0).OwningRow.Index
'create a new row
Dim row As DataRow
row = table.NewRow()
' add data to the row
row(0) = Integer.Parse(DataGridView1.Rows(rowIndex).Cells(0).Value.ToString())
row(1) = DataGridView1.Rows(rowIndex).Cells(1).Value.ToString()
row(2) = DataGridView1.Rows(rowIndex).Cells(2).Value.ToString()
row(3) = Integer.Parse(DataGridView1.Rows(rowIndex).Cells(3).Value.ToString())
If rowIndex > 0 Then
' remove the selected row
table.Rows.RemoveAt(rowIndex)
' insert the new row at a new position
table.Rows.InsertAt(row, rowIndex - 1)
DataGridView1.ClearSelection()
DataGridView1.Rows(rowIndex - 1).Selected = True
End If
End Sub
Private Sub ButtonDOWN_Click(sender As Object, e As EventArgs) Handles ButtonDOWN.Click
rowIndex = DataGridView1.SelectedCells(0).OwningRow.Index
Dim row As DataRow
row = table.NewRow()
row(0) = Integer.Parse(DataGridView1.Rows(rowIndex).Cells(0).Value.ToString())
row(1) = DataGridView1.Rows(rowIndex).Cells(1).Value.ToString()
row(2) = DataGridView1.Rows(rowIndex).Cells(2).Value.ToString()
row(3) = Integer.Parse(DataGridView1.Rows(rowIndex).Cells(3).Value.ToString())
If rowIndex < DataGridView1.Rows.Count - 2 Then
table.Rows.RemoveAt(rowIndex)
table.Rows.InsertAt(row, rowIndex + 1)
DataGridView1.ClearSelection()
DataGridView1.Rows(rowIndex + 1).Selected = True
End If
End Sub
End Class
Dim table As New DataTable("table")
' selected row index
Dim rowIndex As Integer
Private Sub datagridview_row_up_down_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' populate dgv from dataTable
' add columns to dataTable
table.Columns.Add("Id", Type.GetType("System.Int32"))
table.Columns.Add("First Name", Type.GetType("System.String"))
table.Columns.Add("Last Name", Type.GetType("System.String"))
table.Columns.Add("Age", Type.GetType("System.Int32"))
' add rows to dataTable
table.Rows.Add(1, "AA", "AA", 21)
table.Rows.Add(2, "BB", "BB", 33)
table.Rows.Add(3, "CC", "CC", 53)
table.Rows.Add(4, "DD", "DD", 19)
table.Rows.Add(5, "EE", "EE", 36)
table.Rows.Add(6, "FF", "FF", 63)
table.Rows.Add(7, "HH", "HH", 70)
table.Rows.Add(8, "KK", "KK", 80)
table.Rows.Add(9, "GG", "GG", 90)
table.Rows.Add(10, "MM", "MM", 100)
DataGridView1.DataSource = table
End Sub
Private Sub ButtonUP_Click(sender As Object, e As EventArgs) Handles ButtonUP.Click
' get the index of selected row
rowIndex = DataGridView1.SelectedCells(0).OwningRow.Index
'create a new row
Dim row As DataRow
row = table.NewRow()
' add data to the row
row(0) = Integer.Parse(DataGridView1.Rows(rowIndex).Cells(0).Value.ToString())
row(1) = DataGridView1.Rows(rowIndex).Cells(1).Value.ToString()
row(2) = DataGridView1.Rows(rowIndex).Cells(2).Value.ToString()
row(3) = Integer.Parse(DataGridView1.Rows(rowIndex).Cells(3).Value.ToString())
If rowIndex > 0 Then
' remove the selected row
table.Rows.RemoveAt(rowIndex)
' insert the new row at a new position
table.Rows.InsertAt(row, rowIndex - 1)
DataGridView1.ClearSelection()
DataGridView1.Rows(rowIndex - 1).Selected = True
End If
End Sub
Private Sub ButtonDOWN_Click(sender As Object, e As EventArgs) Handles ButtonDOWN.Click
rowIndex = DataGridView1.SelectedCells(0).OwningRow.Index
Dim row As DataRow
row = table.NewRow()
row(0) = Integer.Parse(DataGridView1.Rows(rowIndex).Cells(0).Value.ToString())
row(1) = DataGridView1.Rows(rowIndex).Cells(1).Value.ToString()
row(2) = DataGridView1.Rows(rowIndex).Cells(2).Value.ToString()
row(3) = Integer.Parse(DataGridView1.Rows(rowIndex).Cells(3).Value.ToString())
If rowIndex < DataGridView1.Rows.Count - 2 Then
table.Rows.RemoveAt(rowIndex)
table.Rows.InsertAt(row, rowIndex + 1)
DataGridView1.ClearSelection()
DataGridView1.Rows(rowIndex + 1).Selected = True
End If
End Sub
End Class
// OUTPUT :
Download Projects Source Code