将一个数据表特定列复制到另一个数据表

编程入门 行业动态 更新时间:2024-10-28 02:34:53
本文介绍了将一个数据表特定列复制到另一个数据表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

您好, 我有一个DataTable,其列为SlNo,FirstName,LastName,MobileNO和Salary, i想要只将SlNo,FirstName,LastName和Salary复制到anothere表..... 使用循环我要添加数据 我尝试过: newTable = dt.DefaultView.ToTable(false, ColumnName); i希望通过数据表副本循环添加到另一个表

Hello , I Have one DataTable with Columns as SlNo,FirstName,LastName,MobileNO and Salary, i want to Copy only SlNo,FirstName,LastName and Salary to anothere table ..... Using Loops i want to add Data What I have tried: newTable = dt.DefaultView.ToTable(false, ColumnName); i want add through loop of data table copy to another table

推荐答案

使用复制方法并删除所需的列 Use Copy method and remove desired columns DataTable newTable ; newTable = dt.Copy();

或者您可以克隆数据表结构并循环添加行到新表

or you can clone datatable structure and by looping add rows to new table

DataTable newTable = dt.Clone(); foreach (DataRow item in dt.Rows) { newTable.ImportRow(item); }

用于删除列的使用以下其中一项

for column removal use one of below

dt.Columns.Remove("columnName"); dt.Columns.RemoveAt(columnIndex);

更新的解决方案:

Updated solution:

DataTable dt = new DataTable(); dt.Columns.Add("[Serial Number]", typeof(int)); dt.Columns.Add("[Last Name]", typeof(string)); dt.Columns.Add("[First Name]", typeof(string)); dt.Columns.Add("Salary", typeof(decimal)); dt.Columns.Add("[Mobile No]", typeof(string)); dt.Rows.Add(1, "Kulkarni", "Ramesh", 1500.00, "9485234567"); dt.Rows.Add(2, "rani", "suresh", 1500.00, "9485234567"); DataTable newTable; newTable = dt.Copy(); newTable.Columns.Remove("[Mobile No]"); newTable.AcceptChanges();

更多推荐

将一个数据表特定列复制到另一个数据表

本文发布于:2023-11-11 23:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579907.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据表

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!