您好, 我有一个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();
更多推荐
将一个数据表特定列复制到另一个数据表
发布评论