?
Solved

Move specific row from one datatable to another

Posted on 2014-04-04
4
Medium Priority
?
25 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
2 Comments
 
LVL 96

Assisted Solution

by:Bob Learned
Bob Learned earned 1000 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 1000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Integration Management Part 2
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

864 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