Solved

Move specific row from one datatable to another

Posted on 2014-04-04
4
16 Views
Last Modified: 2016-07-11
I have a simple .aspx webpage  with two listboxes.  I start on the initial page load by binding Listbox1 to a datatable and Listbox2 to another datatable :

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Label1.Text = ADS1.ConnectionString
        If Not Page.IsPostBack Then
            ListLoad()
        End If
End Sub

Private Sub ListLoad()
        Dim dv As System.Data.DataView = CType(ADS1.Select(DataSourceSelectArguments.Empty), System.Data.DataView)
        dt = dv.ToTable
        cdt = dt.Clone
        
        ListBox1.DataSource = dt
        ListBox1.DataValueField = "ID"
        ListBox1.DataTextField = "FullName"
        ListBox1.DataBind()

        ListBox2.DataSource = cdt
        ListBox2.DataValueField = "ID"
        ListBox2.DataTextField = "FullName"
        ListBox2.DataBind()    
End Sub

Open in new window

So far everything is working fine and I have the two listboxes to play around with and can move stuff back and forth once the listboxes have been bound to a null.  My issue is trying to move the data in the datatables back and forth (which is why I set up two datatables and bound the listboxes in the first place).  Could someone please explain and possibly provide an example of how I could find the associated row in dt and move it to cdt if that corresponding row in listbox1 is selected?  Ideally I want to be able to handle multiple selected values in Listbox1 use  'For Each item As ListItem In ListBox1.Items...Next'  to handle this.  Thanks a ton!
0
Comment
Question by:Fishin4Walleye
4 Comments
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 250 total points
ID: 39979963
I would think that it would be easier if you converted the DataTable to a List(Of T), and then you can use RemoveAt and Insert methods to modify the list.  

There is the LINQ-to-DataSet AsEnumerable method to help you convert a DataTable to a generics list of business objects.

DataTableExtensions.AsEnumerable Method
http://msdn.microsoft.com/en-us/library/system.data.datatableextensions.asenumerable.aspx
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 250 total points
ID: 39982648
If you want to continue using datatables:

You can use the DataTable.Select method to find rows

http://msdn.microsoft.com/en-us/library/det4aw50.aspx

Then you can use the DataTable.Rows.Remove method to remove this row from current table

http://msdn.microsoft.com/en-us/library/system.data.datarowcollection.remove.aspx

and then use the DataTable.ImportRow method to add it to the 2nd datatable.

http://msdn.microsoft.com/en-us/library/system.data.datatable.importrow.aspx
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now