Answered by:
move selected data to another datagridview vb.net

Question
-
hi
i am trying to movie selected row in Book(datagridview1) to Borrow(datagridview2) i am able to move data from Book to Borrow but the problem is when i move data it's not saving it in other table
i'm using access 2010
Here is my code
Public Class Form1
Dim bookSource As New BindingSource
Dim borrowSource As New BindingSource
Dim booksView As New DataView
Dim borrowView As New DataView
Dim ds As New DataSet
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'BorrowDataSet.borrow' table. You can move, or remove it, as needed.
Me.BorrowTableAdapter.Fill(Me.BorrowDataSet.borrow)
'TODO: This line of code loads data into the 'BookDataSet.Book' table. You can move, or remove it, as needed.
Me.BookTableAdapter.Fill(Me.BookDataSet.Book)
bookSource = Book.DataSource
booksView = CType(bookSource.List, DataView)
ds = booksView.DataViewManager.DataSet.Clone
borrowSource.DataSource = ds
borrowSource.DataMember = "Book"
borrowView = CType(borrowSource.List, DataView)
Borrow.DataSource = borrowSource
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
MoveBook(Book, borrowView)
'sort the items in the target datagrid
Borrow.Sort(Borrow.Columns(0), System.ComponentModel.ListSortDirection.Ascending)
borrowSource.MoveLast()
borrowSource.MoveFirst()
End Sub
Private Sub MoveBook(ByRef source As DataGridView, ByRef target As DataView)
For i = 0 To source.SelectedRows.Count - 1
Dim row As DataRowView
row = target.AddNew()
Dim col As Int16 = 0
For Each cell As DataGridViewCell In source.SelectedRows(i).Cells
row.Item(col) = cell.Value
col = col + 1
Next
Next
'delete the copied items from the source table
Dim count As Int16 = source.SelectedRows.Count
For i = 0 To count - 1
source.Rows.RemoveAt(source.SelectedRows(0).Index)
Next
End Sub
End Classwith regards
- Edited by Redar123 Thursday, March 20, 2014 12:50 AM
Thursday, March 20, 2014 12:47 AM
Answers
-
Hello,
If you are open to an alternate idea try this solution on OneDrive. I use a CheckBox column to mark rows to move instead of selecting rows via the row header of the DataGridView. The move is immediate meaning I move selected rows to another table in the MS-Access database. All operations are done against the data not the DataGridView.
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.
- Marked as answer by Redar123 Thursday, March 20, 2014 11:11 PM
Thursday, March 20, 2014 1:34 PM
All replies
-
why not just do an ImportRow on the borrow datatable then delete the row from the book datasourceThursday, March 20, 2014 3:46 AM
-
hmmm how to do that i'm still new in vb.netThursday, March 20, 2014 11:15 AM
-
Hello,
If you are open to an alternate idea try this solution on OneDrive. I use a CheckBox column to mark rows to move instead of selecting rows via the row header of the DataGridView. The move is immediate meaning I move selected rows to another table in the MS-Access database. All operations are done against the data not the DataGridView.
Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem.
- Marked as answer by Redar123 Thursday, March 20, 2014 11:11 PM
Thursday, March 20, 2014 1:34 PM