简体   繁体   English

将行从一个数据表复制到另一个数据表?

[英]Copy rows from one Datatable to another DataTable?

How can I copy specific rows from DataTable to another Datable in c#?如何将特定行从 DataTable 复制到 c# 中的另一个 Datable? There will be more than one row.会有不止一排。

foreach (DataRow dr in dataTable1.Rows) {
    if (/* some condition */)
        dataTable2.Rows.Add(dr.ItemArray);
}

The above example assumes that dataTable1 and dataTable2 have the same number, type and order of columns.上面的示例假设dataTable1dataTable2具有相同的列数、类型和顺序。

Copy Specified Rows from Table to another将指定的行从表复制到另一个

// here dttablenew is a new Table  and dttableOld is table Which having the data 

dttableNew  = dttableOld.Clone();  

foreach (DataRow drtableOld in dttableOld.Rows)
{
   if (/*put some Condition */)
   {
      dtTableNew.ImportRow(drtableOld);
   }
}

Try This试试这个

    String matchString="ID0001"//assuming we have to find rows having key=ID0001
    DataTable dtTarget = new DataTable();
    dtTarget = dtSource.Clone();
    DataRow[] rowsToCopy;
    rowsToCopy = dtSource.Select("key='" + matchString + "'");
    foreach (DataRow temp in rowsToCopy)
    {
        dtTarget.ImportRow(temp);
    }

Check this out, you may like it (previously, please, clone table1 to table2):看看这个,你可能会喜欢它(以前,请把 table1 克隆到 table2):

table1.AsEnumerable().Take(recodCount).CopyToDataTable(table2,LoadOption.OverwriteChanges);

Or:或者:

table1.AsEnumerable().Where ( yourcondition  ) .CopyToDataTable(table2,LoadOption.OverwriteChanges);

受以下版本支持:4、3.5 SP1,您现在可以只调用对象上的方法。

DataTable dataTable2 = dataTable1.Copy()

As a result of the other posts, this is the shortest I could get:由于其他帖子,这是我能得到的最短的:

DataTable destTable = sourceTable.Clone();
sourceTable.AsEnumerable().Where(row => /* condition */ ).ToList().ForEach(row => destTable.ImportRow(row));

below sample would be the fastest way to copy one row.下面的示例将是复制一行的最快方法。 each cell is being copied based on the column name.每个单元格都根据列名进行复制。 in case you dont need a specific cell to copy then have a try catch or add if.如果您不需要特定的单元格进行复制,请尝试捕获或添加 if。 if your going to copy more than 1 row then loop the code below.如果您要复制超过 1 行,则循环下面的代码。

DataRow dr = dataset1.Tables[0].NewRow();
for (int i = 0; i < dataset1.Tables[1].Columns.Count; i++)
{
    dr[dataset1.Tables[1].Columns[i].ColumnName] = dataset1.Tables[1].Rows[0][i];
}

datasetReport.Tables[0].Rows.Add(dr);

dataset1.Tables[1].Rows[ 0 ][i]; dataset1.Tables[1].Rows[ 0 ][i]; change the index 0 to your specified row index or you can use a variable if your going to loop or if its going to be logical将索引 0 更改为您指定的行索引,或者如果您要循环或者它是合乎逻辑的,则可以使用变量

I've created an easy way to do this issue我创建了一个简单的方法来解决这个问题

 DataTable newTable = oldtable.Clone();    
 for (int i = 0; i < oldtable.Rows.Count; i++)
 {
   DataRow drNew = newTable.NewRow();    
   drNew.ItemArray = oldtable.Rows[i].ItemArray;    
   newTable.Rows.Add(drNew);   
 } 
 private void CopyDataTable(DataTable table){
     // Create an object variable for the copy.
     DataTable copyDataTable;
     copyDataTable = table.Copy();
     // Insert code to work with the copy.
 }

For those who want single command SQL query for that:对于那些想要单个命令 SQL 查询的人:

INSERT INTO TABLE002 
(COL001_MEM_ID, COL002_MEM_NAME, COL002_MEM_ADD, COL002_CREATE_USER_C, COL002_CREATE_S)
SELECT COL001_MEM_ID, COL001_MEM_NAME, COL001_MEM_ADD, COL001_CREATE_USER_C, COL001_CREATE_S
FROM TABLE001;

This query will copy data from TABLE001 to TABLE002 and we assume that both columns had different column names.此查询会将数据从TABLE001复制到TABLE002 ,我们假设两列具有不同的列名。

Column names are mapped one-to-one like:列名是一对一映射的,例如:

COL001_MEM_ID -> COL001_MEM_ID COL001_MEM_ID -> COL001_MEM_ID

COL001_MEM_NAME -> COL002_MEM_NAME COL001_MEM_NAME -> COL002_MEM_NAME

COL001_MEM_ADD -> COL002_MEM_ADD COL001_MEM_ADD -> COL002_MEM_ADD

COL001_CREATE_USER_C -> COL002_CREATE_USER_C COL001_CREATE_USER_C -> COL002_CREATE_USER_C

COL002_CREATE_S -> COL002_CREATE_S COL002_CREATE_S -> COL002_CREATE_S

You can also specify where clause, if you need some condition.如果需要某些条件,您还可以指定 where 子句。

To copy whole datatable just do this:要复制整个数据表,只需执行以下操作:

DataGridView sourceGrid = this.dataGridView1;
DataGridView targetGrid = this.dataGridView2;
targetGrid.DataSource = sourceGrid.DataSource;

There is better way to do this.有更好的方法来做到这一点。

DataTable targetDataTable = new DataTable(); DataTable targetDataTable = new DataTable(); targetDataTable = changedColumnMetadata.AsEnumerable().Where(dataRow => entityName.Equals(dataRow["EntityName"])).CopyToDataTable(); targetDataTable = changedColumnMetadata.AsEnumerable().Where(dataRow => entityName.Equals(dataRow["EntityName"])).CopyToDataTable();

Please try this and let me know in case of any issues.请试试这个,如果有任何问题,请告诉我。

You can do it calling the DataTable.Copy() method, for example:您可以通过调用DataTable.Copy()方法来实现,例如:

   DataSet ds = new DataSet();
   System.Data.DataTable dt = new System.Data.DataTable();
   dt = _BOSearchView.DS.Tables[BusLib.TPV.TableName.SearchView].Copy();
   ds.Tables.Add(dt);
   UltGrdSaleExcel.SetDataBinding(ds, dt.TableName, true);

Not that anyone is still reading this thread by now but...并不是说现在还有人在阅读这个帖子,但是......

I needed to copy rows from multiple tables with the same structure into a new table to be used as a datasource for datagridview:我需要将具有相同结构的多个表中的行复制到一个新表中,以用作 datagridview 的数据源:

// Generate DataTable[] alltables from multiple datatables

DataTable newTable = alltables[0].Clone();

  foreach (DataTable dt in alltables)
  {
      for (int i = 0; i < dt.Rows.Count; i++)
           newTable.Rows.Add(dt.Rows[i].ItemArray);
  }
//use Merge
//this will append all rows of newDataTable with oldDateTale
oldDateTale.Merge(newDataTable);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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