0

I have 2 datagridview (dgvFrom and dgvTo) and 2 buttons with right and left arrows. dgvfrom is populated from dataset. Now when I select rows in dgvFrom and click right arrow button, selected rows should move to dgvTo grid and those selected rows should get deleted from dgvFrom grid.and vice versa for left arrow button.Can any1 help me with this.

2
Contributors
1
Reply
15
Views
4 Years
Discussion Span
Last Post by TnTinMN
0

Here is one way to handle it. Add a "Moved" (type Boolean) to the underlying datatable and create to dataviews to act as the datasource for the respective datagridviews.

Public Class Form1
   Private ds As New DataSet

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      ' Make Some Data
      Dim dt As New DataTable("flintstones")
      ds.Tables.Add(dt) ' you indicated that you are using a dataset, so I will also use one
      Dim r As DataRow
      With dt
         ' Add a column for some data:  You don't need to do this as you already have a table of data
         .Columns.Add("data", GetType(String))
         r = .NewRow : r(0) = "fred" : .Rows.Add(r)
         r = .NewRow : r(0) = "wilma" : .Rows.Add(r)
         r = .NewRow : r(0) = "dino" : .Rows.Add(r)
         r = .NewRow : r(0) = "barney" : .Rows.Add(r)
         r = .NewRow : r(0) = "betty" : .Rows.Add(r)
         r = .NewRow : r(0) = "hoppy" : .Rows.Add(r)
      End With

      ' you can add a column to an existing table
      ds.Tables(0).Columns.Add("Moved", GetType(Boolean))
      ' Initially, the added column cells will have a Null value.
      ' We will treat this as false

      ' Create a DataView to act as the datasource for each datgridview

      Dim fromview As New DataView(ds.Tables(0))
      ' Select only False "Moved" rows
      fromview.RowFilter = "[Moved]=False or [Moved] is Null"
      fromview.Sort = "[Data] Asc"
      dgvFrom.DataSource = fromview
      dgvFrom.Columns("Moved").Visible = False
      dgvFrom.SelectionMode = DataGridViewSelectionMode.FullRowSelect
      dgvFrom.ReadOnly = True

      Dim ToView As New DataView(ds.Tables(0))
      ' Select only True "Moved" rows
      ToView.RowFilter = "[Moved]=True"
      ToView.Sort = "[Data] Asc"
      dgvTo.DataSource = ToView
      dgvTo.Columns("Moved").Visible = False
      dgvTo.SelectionMode = DataGridViewSelectionMode.FullRowSelect
      dgvTo.ReadOnly = True
   End Sub

   Private Sub btnRight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRight.Click
      For Each r As DataGridViewRow In dgvFrom.SelectedRows
         ' Modify the underlying datarow
         CType(r.DataBoundItem, DataRowView).Row.Item("Moved") = True
      Next
   End Sub

   Private Sub btnLeft_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLeft.Click
      For Each r As DataGridViewRow In dgvTo.SelectedRows
         ' Modify the underlying datarow
         CType(r.DataBoundItem, DataRowView).Row.Item("Moved") = False
      Next
   End Sub
End Class
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.