Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DataGridView Row Ordering 1

Status
Not open for further replies.

ousoonerjoe

Programmer
Jun 12, 2007
925
US
Using Vb.Net 2005
DataGridView with 3 columns.

With a great help from RiverGuy, the ListView item reordering was made quite simple and rather straight forward. When adjusting the same procedure to utilize an unbound DataGridView with 3 columns, it's not as pretty.

The results of the following delete the selected row, but inserts a blank line with only the default values 2 down from the selected row. The intent is to copy the selected row, insert it after the next row, delete the original selected row. Most of this process is working except the actual copying of the row. CurrentItem does get the row data from the .Clone call. It is given an .Index of -1. Not a problem since the index will be reassigned during the .Insert. The problem is the row data. When pushing the column data to a message box, it is there in the CurrentItem object, but does not seem to carry through on the .Insert.

Any assistance or guidance is appreciated.

Code:
            If dgJobStages.SelectedRows.Count > 0 Then
                Dim CurrentPosition As Integer = dgJobStages.SelectedRows(0).Index
                Dim PreviousPosition As Integer = IIf(CurrentPosition > 0, CurrentPosition - 1, CurrentPosition)
                Dim NextPosition As Integer = IIf(CurrentPosition < dgJobStages.Rows.Count - 1, CurrentPosition + 2, CurrentPosition)
                Dim CurrentItem As DataGridViewRow = dgJobStages.SelectedRows(0).Clone
                Dim SelectedItem As DataGridViewRow = dgJobStages.SelectedRows(0)
                dgJobStages.Rows.Insert(NextPosition, CurrentItem)
                dgJobStages.Rows.Remove(SelectedItem)
                CurrentItem.Selected = True
            End If

--------------------------------------------------
Bluto: What? Over? Did you say "over"? Nothing is over until we decide it is! Was it over when the Germans bombed Pearl Harbor? No!
Otter: Germans?
Boon: Forget it, he's rolling.
--------------------------------------------------
 
I'm not sure I really like this solution for the DataGridView, but here is what I could come up with as an edit to the ListView code I made.
Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'DataGridView
        Dim DGV1 As New DataGridView
        Me.Controls.Add(DGV1)
        With DGV1
            .Height = 400
            .Width = 400
            .Left = 4
            .Top = 4
            .SelectionMode = DataGridViewSelectionMode.FullRowSelect
            .MultiSelect = False
            .Columns.Add("Column1", "Column1")
            .Columns.Add("Column2", "Column1")
            .AllowUserToAddRows = False
            .Name = "DGV1"
            For i As Integer = 0 To 10
                '.Items.Add(New ListViewItem(i.ToString)).SubItems.Add("Item" & i.ToString)
                .Rows.Add(New Object() {i.ToString, "Item" & i.ToString})
            Next
        End With

        'Buttons
        Dim btnUp As New Button
        Dim btnDown As New Button
        Me.Controls.Add(btnUp)
        Me.Controls.Add(btnDown)
        With btnUp
            .Left = DGV1.Left + DGV1.Width + 4
            .Top = DGV1.Top
            .Width = 48
            .Text = "Up"
            .Name = "btnUp"
        End With
        AddHandler btnUp.Click, AddressOf btnMove_Click
        AddHandler btnDown.Click, AddressOf btnMove_Click
        With btnDown
            .Left = DGV1.Left + DGV1.Width + 4
            .Top = DGV1.Top + btnUp.Height + 4
            .Width = 48
            .Text = "Down"
            .Name = "btnDown"
        End With
    End Sub

    Private Sub btnMove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim btn As Button = DirectCast(sender, Button)
        Dim DGV As DataGridView = DirectCast(Me.Controls("DGV1"), DataGridView)
        If DGV.SelectedRows.Count > 0 Then
            Dim CurrentPosition As Integer = DGV.SelectedRows(0).Index
            Dim PreviousPosition As Integer = IIf(CurrentPosition > 0, CurrentPosition - 1, CurrentPosition)
            Dim NextPosition As Integer = IIf(CurrentPosition < DGV.Rows.Count - 1, CurrentPosition + 2, CurrentPosition)
            Dim CurrentRow As DataGridViewRow = DGV.SelectedRows(0).Clone[b][red]
            For i As Integer = 0 To CurrentRow.Cells.Count - 1
                CurrentRow.Cells(i).Value = DGV.SelectedRows(0).Cells(i).Value
            Next[/red][/b]
            Dim SelectedRow As DataGridViewRow = DGV.SelectedRows(0)
            Select Case btn.Name
                Case "btnUp"
                    DGV.Rows.Insert(PreviousPosition, CurrentRow)
                Case "btnDown"
                    DGV.Rows.Insert(NextPosition, CurrentRow)
            End Select
            DGV.Rows.Remove(SelectedRow)
            CurrentRow.Selected = True
        End If
    End Sub
 
Once again, you've saved my sanity, RiverGuy. Thank you for the assist. The only thing I had to adjust was the unselecting the original row and moving the Active Row Indicator to the new location of the item.
Code:
    Private Sub btnMove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim btn As Button = DirectCast(sender, Button)
        Dim DGV As DataGridView = DirectCast(Me.Controls("DGV1"), DataGridView)
        If DGV.SelectedRows.Count > 0 Then
            Dim CurrentPosition As Integer = DGV.SelectedRows(0).Index
            Dim PreviousPosition As Integer = IIf(CurrentPosition > 0, CurrentPosition - 1, CurrentPosition)
            Dim NextPosition As Integer = IIf(CurrentPosition < DGV.Rows.Count - 1, CurrentPosition + 2, CurrentPosition)
            Dim CurrentRow As DataGridViewRow = DGV.SelectedRows(0).Clone
            For i As Integer = 0 To CurrentRow.Cells.Count - 1
                CurrentRow.Cells(i).Value = DGV.SelectedRows(0).Cells(i).Value
            Next
            Dim SelectedRow As DataGridViewRow = DGV.SelectedRows(0)
            Select Case btn.Name
                Case "btnUp"
                    DGV.Rows.Insert(PreviousPosition, CurrentRow)
                Case "btnDown"
                    DGV.Rows.Insert(NextPosition, CurrentRow)
            End Select
            DGV.Rows.Remove(SelectedRow)
            CurrentRow.Selected = True
           [COLOR=red] DGV.Rows(CurrentPosition).Selected = False
            DGV.CurrentCell = DGV.Item(0, CurrentRow.Index)
   [/color]     End If
    End Sub

--------------------------------------------------
Bluto: What? Over? Did you say "over"? Nothing is over until we decide it is! Was it over when the Germans bombed Pearl Harbor? No!
Otter: Germans?
Boon: Forget it, he's rolling.
--------------------------------------------------
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top