Reputation: 5639
Have some read in data (from excel file) in a DataTable and now I want to filter this and copy only specific columns to the other one!
dataTable format:
some data
ColA|ColB|ColC
xxxx|xxxx|xxxx
some data
some data
represents other table data not related to ColA-ColC
How can I copy ColA-ColC with xxxx to the new DataTable?
Thx
Upvotes: 21
Views: 124291
Reputation: 248
Same fix in vb.net
Dim view As DataView = New DataView(TB) Dim selected As DataTable = view.ToTable("Selected", False, "id", "col1", "col2")
Upvotes: 0
Reputation: 301
It can be achieved using LINQ
Assume we have two DataTable
. 1. dtSource
and 2. dtDestination
.
if dtDestination
has no rows then use below code to generate blank rows.
dtSource.AsEnumerable().All(row => { dtDestination.Rows.Add(); return true; });
Below code will copy particular DataColumn
data to DataColumn
of another DataTable
. Assume both Tables have same number of rows.
int rowIdx = 0;
dtDestination.AsEnumerable().All(row => { row["colName"] = dtSource.Rows[rowIdx++]["colName"]; return true; });
Upvotes: 1
Reputation: 470
This method receives a data table (TextFileTable) as a parameter and proceeds to copy the selected contents of TextFileTable into tblFormat table.In the add row statement the number of columns, must match the number of columns in the target ted table,even though the the two table can be of different sizes.
public DataTable CopyTable (DataTable TextFileTable)
{
DataTable tblFormat = new DataTable();
tblFormat.Columns.Add("ColumnA");
tblFormat.Columns.Add("ColumnB");
tblFormat.Columns.Add("ColumnC");
tblFormat.Columns.Add("ColumnD");
tblFormat.Columns.Add("ColumnE");
for (int i = 0; i < TextFileTable.Rows.Count; i++)
{
tblFormat.Rows.Add(new string[] { TextFileTable.Rows[i][0].ToString(), TextFileTable.Rows[i][1].ToString(),
TextFileTable.Rows[i][2].ToString(), TextFileTable.Rows[i][8].ToString(), TextFileTable.Rows[i][9].ToString() });
}
return tblFormat;
}
Upvotes: 0
Reputation: 2016
Define your copy DataTable with only the interest columns. You can loop on columns of source row and set the value to the target row with this sample code :
public void IntegrateRow(DataRow p_RowCible, DataRow p_RowSource)
{
try
{
foreach (DataColumn v_Column in p_RowCible.Table.Columns)
{
string ColumnName = v_Column.ColumnName;
if (p_RowSource.Table.Columns.Contains(ColumnName))
{
p_RowCible[ColumnName] = p_RowSource[ColumnName];
}
}
}
catch (Exception e)
{
...
Upvotes: 2
Reputation: 18411
Copy the whole table and remove the columns you don't want.
DataTable copyDataTable;
copyDataTable = table.Copy();
copyDataTable.Columns.Remove("ColB");
or
int columnIndex = 1;//this will remove the second column
DataTable copyDataTable;
copyDataTable = table.Copy();
copyDataTable.Columns.RemoveAt(columnIndex);
Upvotes: 31
Reputation: 1090
please check this
foreach (DataRow dr in dataTable1.Rows) {
if (/* some condition */)
dataTable2.Rows.Add(dr.ItemArray);
}
The above example assumes that both table have the same number, type and order of columns.
here is the actual link
Upvotes: 3
Reputation: 9862
You can simply do it by using DataView.ToTable() :
System.Data.DataView view = new System.Data.DataView(yourOriginalTable);
System.Data.DataTable selected =
view.ToTable("Selected", false, "col1", "col2", "col6", "col7", "col3");
Upvotes: 52