Solved

Move specific row from one datatable to another

Posted on 2014-04-04
4
21 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 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

Enroll in June's Course of the Month

June’s Course of the Month is now available! Experts Exchange’s Premium Members, Team Accounts, and Qualified Experts have access to a complimentary course each month as part of their membership—an extra way to sharpen your skills and increase training.

Question has a verified solution.

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

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
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 …

705 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