EFileTahi-A
Well-known member
Whats the best way of copying rows from a DataTable to another Datable?
Thanks.
Thanks.
// Copy Table 0 to a new table named "MyNewName"
DataTable newTable = ds.Tables[0].Copy();
newTable.TableName = "MyNewName";
ds.Tables.Add(newTable);
DataTable newTable = ds.Tables[0].Clone();
newTable.TableName = "MyNewName";
for(int i=0; i<3; i++)
{
newTable.ImportRow(ds.Tables[0].Rows[i]);
}
ds.Tables.Add(newTable);
DataTable newTable = ds.Tables[0].Copy();
// Same as
DataTable myTable = ds.Tables[0];
DataTable newTable = myTable.Copy();
//Query to execute in order to load the DataTable
sSQL = "SELECT * FROM InDocLin";
//Here I create a temporarely DataTable to store all Data from InDocLin table
OdbcDataAdapter daTemp = new OdbcDataAdapter(sSQL, MyConnData);
//Now I Fill the dtTemp with all the InDocLin data
System.Data.DataTable dtTemp = new System.Data.DataTable();
daTemp.Fill(dtTemp);
//I clone all the info from dtTemp to the Static DataTable (dtDocLinTemp)
dtDocLinTemp = dtTemp.Clone();
//Clearing any existing rows
dtDocLinTemp.Rows.Clear();
// here is dtInDocLin Table that will recieve the new rows from the static
Datatable DocLinTemp
//I did this to get the rows from the static table
DataRow[] rows = DB_Engine.dtDocLinTemp.Select();
//So now I supposely just need to put them back on dtInDocLin
dtInDocLin.Rows.Add(rows);
Couldnt store <System.data.DataRow> in "numero" column expecting type is int32
dtDocLinTemp = dtTemp.Clone();
// Dont need to do the following
// dtDocLinTemp.Rows.Clear();
Datatable DocLinTemp = DB_Engine.dtDocLinTemp.Clone();
//So now I supposely just need to put them back on dtInDocLin
foreach(DataRow row in DB_Engine.dtDocLinTemp.Rows)
{
DocLinTemp.Rows.ImportRow(row);
}
DataTable dtInDocLin = new DataTable();
DataRow newRow;
string sRecBefore = "";
string sRecNow = "";
sSQL = "SELECT * FROM InDocLin";
OdbcDataAdapter daInDocLin = new OdbcDataAdapter(sSQL, MyConnection);
daInDocLin.Fill(dtInDocLin);
OdbcCommandBuilder cbInDocLin = new OdbcCommandBuilder(daInDocLin);
//dtDocLinTemp is a declared as static datatable in the declaration forms section
dtDocLinTemp = dtInDocLin.Clone();
//adding a new row into the static datatable
newRow = dtDocLinTemp.NewRow();
newRow["numlin"] = 30;
newRow["numdoc"] = 10;
dtDocLinTemp.Rows.Add(newRow);
//I dont know if acceptchanges is really necessary
dtDocLinTemp.AcceptChanges();
//this actualy counts the correct rows inside dtDocLinTemp which is "1"
sRecBefore = dtDocLinTemp.Rows.Count.ToString();
foreach(DataRow row in dtDocLinTemp.Rows)
{
dtInDocLin.ImportRow(row);
}
daInDocLin.Update(dtInDocLin);
dtInDocLin.AcceptChanges();
//This also returns the correct number of rows which is "1"
sRecNow = dtInDocLin.Rows.Count.ToString();