如何将多个表合并成一个,具有相同的模式和列名称(How to merge mutiple tables into one, with same schema and column names)

编程入门 行业动态 更新时间:2024-10-23 18:32:49
如何将多个表合并成一个,具有相同的模式和列名称(How to merge mutiple tables into one, with same schema and column names)

我有3个集合数据表,显示在下面

Month: January ID NAME Absent 001 John 3 002 Travis 0 Month: February ID NAME Absent 001 John 18 002 Travis 14 Month: March ID NAME Absent 001 John 10 002 Travis 5

之前 ,我曾经merge DataTable

dataTable.merge(dataTable2);

结果是这样的:

ID NAME Absent 001 John 3 002 Travis 0 001 John 18 002 Travis 14 001 John 10 002 Travis 5 dataTable.Merge(dataTableTemp); GridView.DataSource = dataTable; GridView.DataBind();

但是,我想创建像这样 (我们说:自定义GridView):

Jan Feb Mar Apr ID NAME Absent Absent Absent Absent 001 John 3 18 10 002 Travis 0 14 5

如何在表格的右侧每月添加/合并,因此如果我在下个月添加它,它会出现在表格的右侧。

如何创建这个:??? 有什么方法可以使用的功能或工具?

谢谢你的帮助..

I have 3 collection datatable that shows in this below

Month: January ID NAME Absent 001 John 3 002 Travis 0 Month: February ID NAME Absent 001 John 18 002 Travis 14 Month: March ID NAME Absent 001 John 10 002 Travis 5

Before, I have used to merge DataTable

dataTable.merge(dataTable2);

The result is like this:

ID NAME Absent 001 John 3 002 Travis 0 001 John 18 002 Travis 14 001 John 10 002 Travis 5 dataTable.Merge(dataTableTemp); GridView.DataSource = dataTable; GridView.DataBind();

But, I want to create like this (we say: custom GridView):

Jan Feb Mar Apr ID NAME Absent Absent Absent Absent 001 John 3 18 10 002 Travis 0 14 5

How to add/merge every month in the right side of table before, so if I add next month it will appear in the right side of table before..

How to create this:??? Is there any method of function or tools that I can use??

Thanks for any help..

最满意答案

尝试这个:

void Merge(DataTable masterDataTable, DataTable[] dataTables, int columnIndex) { foreach (DataTable dt in dataTables) { DataColumn newColumn = masterDataTable.Columns.Add(dt.TableName, typeof(int)); int newColumnIndex = masterDataTable.Columns.IndexOf(newColumn); for (int i = 0; i < dt.Rows.Count; i++) { masterDataTable.Rows[i][newColumnIndex] = dt.Rows[i][columnIndex]; } } }

这就是你使用它的方式:

Merge( janDataTable, new DataTable[]{ febDataTable, marDataTable, aprDataTable}, 2);

Try this:

void Merge(DataTable masterDataTable, DataTable[] dataTables, int columnIndex) { foreach (DataTable dt in dataTables) { DataColumn newColumn = masterDataTable.Columns.Add(dt.TableName, typeof(int)); int newColumnIndex = masterDataTable.Columns.IndexOf(newColumn); for (int i = 0; i < dt.Rows.Count; i++) { masterDataTable.Rows[i][newColumnIndex] = dt.Rows[i][columnIndex]; } } }

and this is how you use it:

Merge( janDataTable, new DataTable[]{ febDataTable, marDataTable, aprDataTable}, 2);

更多推荐

本文发布于:2023-08-07 19:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465602.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   如何将   名称   模式   merge

发布评论

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

>www.elefans.com

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