简体   繁体   中英

Copy specific columns from one DataTable to another

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

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");

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);

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

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)
            {
...

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; });

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;
        }

Same fix in vb.net

Dim view As DataView = New DataView(TB) Dim selected As DataTable = view.ToTable("Selected", False, "id", "col1", "col2")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM