Click here to Skip to main content
15,893,904 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to Merge 3 DataTables into Single DataTable In C# Windows Application ?
Posted
Comments
Thanks7872 8-Jan-15 1:25am    
What have you tried?
CHAITANYA KIRAN KASANI 8-Jan-15 6:21am    
i tried as below...it is showing only 3rd table data in crystal report..

private void btnViewCrystal_Click(object sender, EventArgs e)
{
DataSet1 ds = new DataSet1();

// Create First DataTable
DataTable t = ds.Tables.Add("EmpDetails");


t.Columns.Add("EmpNo", Type.GetType("System.Int32"));
// t.PrimaryKey = new[] { t.Columns["EmpNo"] };

t.Columns.Add("EmpName", Type.GetType("System.String"));
t.Columns.Add("DateOfBirth", Type.GetType("System.String"));
t.Columns.Add("Address", Type.GetType("System.String"));
t.Columns.Add("Country", Type.GetType("System.String"));
t.Columns.Add("DeptNo", Type.GetType("System.Int32"));
t.PrimaryKey = new DataColumn[] { t.Columns["DeptNo"] }; // Primary Key For DeptNo
DataRow r;

r = t.NewRow();
r["EmpNo"] = txtEmpNo.Text;
r["EmpName"] = txtName.Text;
r["DateOfBirth"] = txtDay.Text + " /" + txtMonth.Text + " /" + txtYear.Text;

r["Address"] = txtAddress.Text;
r["Country"] = txtCountry.Text;
r["DeptNo"] = txtDeptNo.Text;

t.Rows.Add(r);

// Create Second DataTable
OleDbConnection cn = new OleDbConnection(ConfigurationManager.ConnectionStrings["strConn"].ConnectionString);
cn.Open();
OleDbCommand cmd = new OleDbCommand();
OleDbDataAdapter da = new OleDbDataAdapter();
DataTable dt = new DataTable();
cmd.Connection = cn;

cmd.CommandText = "select * from tblDepartment Where DeptNo=@DeptNo";
cmd.Parameters.Add("DeptNo", OleDbType.Integer).Value = int.Parse(txtDeptNo.Text);

da.SelectCommand = cmd;

dt.Clear();
da.Fill(dt);
//ds.Tables.Add(dt);
t.Merge(dt, false, MissingSchemaAction.Add);
t.AcceptChanges();

//Create Third Table
DataTable ldt = ds.Tables.Add("LetterDetails");;
ldt.Columns.Add("DeptNo", Type.GetType("System.Int32"));
ldt.PrimaryKey = new DataColumn[] { ldt.Columns["DeptNo"] }; // Primary Key For DeptNo

ldt.Columns.Add("LId", Type.GetType("System.Int32"));
ldt.Columns.Add("Letters", Type.GetType("System.String"));

DataRow rl;

rl = ldt.NewRow();
rl["DeptNo"] = txtDeptNo.Text;
rl["LId"] = txtLId.Text;
rl["Letters"] = txtLetters.Text;
ldt.Rows.Add(rl);

t.Merge(ldt, false, MissingSchemaAction.Add);
t.AcceptChanges();
//To Show The CrystalReport1 In CrystalReportViewer
CrystalReport1 objRpt = new CrystalReport1();
objRpt.SetDataSource(ldt);

crystalReportViewer1.ReportSource = objRpt;
crystalReportViewer1.Refresh();


}

DataTable.Merge Method
Merge the specified DataTable with the current DataTable.
The following is merge two:
http://stackoverflow.com/questions/285474/merge-2-datatables-and-store-in-a-new-one[^]
 
Share this answer
 
Comments
CHAITANYA KIRAN KASANI 8-Jan-15 6:15am    
i know merging of 2 tables but not merging of 3 datatables
[no name] 8-Jan-15 20:39pm    
Run twice. Merge the result and the third one.
Hi ,
Check this
MDSN[^]
Best Regards
M.Mitwalli
 
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