.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> Windows Application >> Post New QuestionBookmark and Share Subscribe to Forum

How to make datatable from another

Posted By: Ruchi     Posted Date: April 06, 2010    Points:2   Category :Windows Application
hi.

Can anyone guide me how can i create a new datatable containing only 2 columns from another datatable ?

Responses
Author: Venkat             
Posted Date: April 06, 2010     Points: 5   

Hi,

If you want to copy all the data from DataTable1 to DataTable2 then you can use Copy() method of DataTable.

DataTable.Clone()
The schema and all other constrains belonging DataTable
DataTable.Copy()
The schema and the table data

Mark As Answer if this solve your problem
Warm Regards,
Venkat
Author: Ruchi             
Posted Date: April 06, 2010     Points: 5   

hi.
Thanks for the reply.
but i only want 2 columns.
Bcoz if i put a separate query then it becomes time consuming so i want to create a data table which can substitute as a part of main table.

Author: Dilip             
Posted Date: April 06, 2010     Points: 5   

What about this
-----------------------------------
Datatable dt2 = new DataTable();
dt2 = dt1.Copy();
dt2.Columns.Remove("ColumnToRemove");

I am not sure of the performance...Basically let us know what is that you are trying to achive

Regards
Dilip
Author: Sagar Pardeshi             
Posted Date: April 06, 2010     Points: 5   

sqlCommand = new SqlCommand("SELECT ID, A, B FROM Table1", sqlConnection, sqlTransaction);
sqlAdapter = new SqlDataAdapter(sqlCommand);
DataTable dataTable1 = new DataTable();
sqlAdapter.Fill(dataTable1);

sqlCommand = new SqlCommand("SELECT ID, C, D FROM Table2", sqlConnection, sqlTransaction);
sqlAdapter = new SqlDataAdapter(sqlCommand);
DataTable dataTable2 = new DataTable();
sqlAdapter.Fill(dataTable2);

sqlCommand = new SqlCommand("SELECT Table1.ID, A, B, Table2.ID, C, D FROM Table1 INNER JOIN Table2 ON Table1.ID = Table2.ID", sqlConnection, sqlTransaction);
sqlAdapter = new SqlDataAdapter(sqlCommand);
DataTable dataTableSqlJoined = new DataTable();
sqlAdapter.Fill(dataTableSqlJoined);

var dataRows =
from
dataRows1 in dataTable1.AsEnumerable()
join
dataRows2 in dataTable2.AsEnumerable()
on
dataRows1.Field<int>("ID") equals dataRows2.Field<int>("ID")
select
dataRows1; // + dataRows2;

DataTable dataTableLinqJoined = dataRows.CopyToDataTable();




Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend