Click here to Skip to main content
15,919,358 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Iam using vs2010. While I wish to select multiple columns from datatable and wish to convert it as DataTable, Iam not able to use the CopyToDataTable() statement.

Does anybody can give the right directions pls...

C#
var MyVehListQry = (from DR1 in MyDataTable.AsEnumerable() where DR1.Field<string>("Field1") == MyValue select new { Field1 = DR1.Field<string>("Field1"), Field2 = DR1.Field<string>("Field2") }).ToList();

The below is not working....?

C#
DataTable MyDtb2 = MyVehListQry.CopyToDataTable(); ??????????????

Thanks for the guidences...
Thanks & Regards
Paramu
Posted

Your collection is not a DataRow IEnumerable collection, so CopyToDataTable is not appearing
Solution :
http://msdn.microsoft.com/en-us/library/bb669096.aspx[^]
 
Share this answer
 
Why so complicated ?)

Try like this,

DataTable newTable = oldTable.Copy();
newTable.Columns.Remove("ColumnName");
You remove the unwanted columns here.

Here is the best Solution for your Question:

DataTable dt = new DataTable();
string [] column = {"Column1", "Column2"};
dt = DTItem.DefaultView.ToTable("dd", false, column);
//DTItem is the Existing Table and "dd" is the temporary tablename, u give whatever U want
 
Share this answer
 
vmhjyhjtfyjhkjhkyuyrjrteyvyfe
C#
DataTable dt = new DataTable();
string [] column = {"Column1", "Column2"};
dt = DTItem.DefaultView.ToTable("dd", false, column);
//DTItem is the Existing Table and "dd" is the temporary tablename, u give whatever U want
 
Share this answer
 
C#
DataTable dt = new DataTable();
string [] column = {"Column1", "Column2"};
dt = DTItem.DefaultView.ToTable("dd", false, column);
//DTItem is the Existing Table and "dd" is the temporary tablename, u give whatever U want
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900