|
-
November 24th, 2006, 09:26 AM
#1
problem copying columns
I've got a couple of listboxes which the user can move items from one to the other. When they're done moving the items, which are column names from a datatable, I want to show only the columns which have names in the first list box.
the list box items look like this:
01 surname
02 firstname
03 initials
So I cut off the first part of the listbox item to get the column index
Early on in the code I populate a datatable called dt and use the column names to fill the listbox. The part I've described above all seems to work fine.
This bit doesn't:
Code:
Dim newdt As New DataTable()
For i = 0 To ListBox1.Items.Count() - 1
Dim mystr As String = ListBox1.Items(i).ToString().Substring(0, 2) 'here I'm cutting the index off the front of the string
Dim myint As Short = Val(mystr) 'turning the index into a value
newdt.Columns.Add(dt.Columns(myint))
Next
I get this error:
An unhandled exception of type 'System.ArgumentException' occurred in system.data.dll
Additional information: Column 'Surname' already belongs to another DataTable.
I'm using .NET Framework 3.5
I'm planning to be spontaneous tomorrow
-
November 24th, 2006, 11:25 AM
#2
Re: problem copying columns
No, you can't add a column in another datatable to a new table. But you could use method "Clone" of DataTable class to do that.
-
November 24th, 2006, 01:31 PM
#3
Re: problem copying columns
use the code to add a column to the new dataTable
Code:
newdt.Columns.Add(dt.Columns(myint).ColumnName, dt.Columns(myint).DataType)
-
November 25th, 2006, 09:05 AM
#4
Re: problem copying columns
Thank you aniskhan, your code creates the correct column headings for me, but no data. Do I need to add the data row by row too?
I'm using .NET Framework 3.5
I'm planning to be spontaneous tomorrow
-
November 26th, 2006, 05:44 AM
#5
Re: problem copying columns
Yes, you need to cpoy the data row by row.
-
June 15th, 2011, 04:24 AM
#6
Re: problem copying columns
Superb!!!Nice work......correct solution 100%
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|