如何将多个表合并成一个,具有相同的模式和列名称(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);如何将多个表合并成一个,具有相同的模式和列名称(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);