?
Solved

Move specific row from one datatable to another

Posted on 2014-04-04
4
Medium Priority
?
22 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

771 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