I have a DataTable Dt1 and another DataTable Dt2.
Dt1 contains many columns and rows and Dt2 is basically empty
I know I should use Dt1.Select to select the specific DataTaRow [] but how can I copy them to Dt2
DataRow [] row = Dt1.Select("ID,MIN_VALUE,MAX_VALUE");
how can i copy them to Dt2?
For .Net Framework 3.5+
You can use CopyToDataTable method.
Returns a DataTable that contains copies of the DataRow objects, given
an input IEnumerable object.
DataTable dt1 = new DataTable();
DataRow[] dr = dt.Select("Your string");
DataTable dt2 = dr.CopyToDataTable();
Related
I'm trying to copy the data from source dt to destination datatable.
source datatble types are sting and destination datatble types contains datetime along with strings.
datatable dt2=new datatable();
foreach (DataRow row in dt1.Rows)
{
dt2.ImportRow(row); //String was not recognized as a valid DateTime.
}
I get String was not recognized as a valid DateTime as destination column type is datetime and is not able to import that row.
Use DataTable.Clone() to setup a new DataTable object with the existing schema. Then add any additional columns you might need.
DataTable dt1 = MyData();
DataTable dt2 = dt1.Clone();
foreach(DataRow row in dt1.Rows)
{
dt2.ImportRow(row);
}
I want to copy first row of one DataTable to first row of another DataTable. I did as shown below but it throws error like
Property or indexer 'System.Data.DataRowCollection.this[int]' cannot be assigned to -- it is read only
DataTable dt = GetTable();
DataTable dt1 = GetTable1();
dt.Rows[0] = dt1.Rows[0];
You have to copy it field by field.
The data row within the datatable is immutable, which means that you can change the values of its fields, but not the object itself, being said that you should copy the values from you source row to the destination row.
Assuming your datatables have the same columns:
DataTable dt = GetTable();
DataTable dt1 = GetTable1();
foreach(DataColumn column in dt.Columns)
{
dt.Rows[0][column] = dt1.Rows[0][column];
}
Hope this helps.
i have a DataSet which reads xml data and it has almost 20 columns,
i need 5 columns from the DataSet .
i have tried at my level but not able to get the DataTable with the specific columns i need.
some codes which i tried are :
DataTable dt = new DataTable(); //dt is blank DataTable
DataSet ds = new DataSet(); //ds is existing DataSet which has single table in it
dt.Columns[0]=dst4.Tables[0].Columns[0];
dt.Columns.Add(dst4.Tables[0].Columns[0]);
ds.Tables.Add(dt)
dst4.Tables.Columns.Add(dt);
You could clone the table which creates an empty table with the same columns, then you can remove the redundant columns.Here is an example with named columns, you could also use the indices:
DataTable dt = ds.Tables[0].Clone();
var colToTake = new[] { "Col1", "Col3", "Col7", "Col15", "Col19" };
var colsToRemove = dt.Columns.Cast<DataColumn>()
.Where(c => !colToTake.Contains(c.ColumnName));
foreach (DataColumn colToRemove in colsToRemove)
dt.Columns.Remove(colToRemove);
say you have to copy columns "ColumnName1", "ColumnName2", "ColumnName3", "ColumnName4", "ColumnName5" to new DataTable
DataTable source = dst4.Tables[0];
DataTable dt = source.DefaultView.ToTable(false, "ColumnName1", "ColumnName2", "ColumnName3", "ColumnName4", "ColumnName5");
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How To Convert The DataTable Column type?
How can we change the datatable column type if table already have value?
Changing data type of datatable after get filled is not possible but one of the work abound for this is clone datatable and change type
First way
//clone datatable
DataTable dtCloned = dt.Clone();
//change data type of column
dtCloned.Columns[0].DataType = typeof(Int32);
//import row to cloned datatable
foreach (DataRow row in dt.Rows)
{
dtCloned.ImportRow(row);
}
second way
and other way to do it is like this , fill schema first - than change column datatype and than fill datable
adapter.FillSchema(table, SchemaType.Source);
table.Columns[0].DataType = typeof (Int32);
adapter.Fill(table);
third way
one more way to do it is
System.Data.DataTable dt = new System.Data.DataTable();
dt.Columns.Add("Col1", typeof(int));
dt.Rows.Add(1);
System.Data.DataTable dt2 = new System.Data.DataTable();
dt2.Columns.Add("Col1", typeof(string));
dt2.Load(dt.CreateDataReader(), System.Data.LoadOption.OverwriteChanges);
I have facing Problem..
DataTable Dt1 = table1;
DataTable Dt2 = Dt1.Copy();
DataRow[] row = Dt1.Select("Name = 'Test'");
foreach (DataRow row in Dt2)
{
Dt2.Rows.Remove(row); // Here The given DataRow is not in the current DataRowCollection
}
Its giving the exception because I Filter the date from Different Row and Removing It from Different Row.
Thanks, Shivam
Your current code removes all rows from the Dt2 DataTable because you overwrite the selected row from Dt1 when you loop through your foreach statement.