vb.net中如何将两个结构不同的DATATABLE合并成一个DATATABLE vb.net中如何将两个结构不同的DATATABLE合并成一个DATATABLE __________________________________________________________________________ Public Class Form1 Private Sub Form1_Load(ByVal sender As Object,ByVal e As System.EventArgs) Handles Me.Load 合并2个DT Dim dt1 As New DataTable Dim dt2 As New DataTable Dim dt3 As DataTable = dt1.Clone() For i As Int32 = 0 To dt2.Columns.Count - 1 dt3.Columns.Add(dt2.Columns(i).ColumnName) Next Dim obj As New Object obj = dt3.Columns.Count For i As Int32 = 0 To dt1.Rows.Count - 1 dt1.Rows(i).ItemArray.CopyTo(obj,0) dt3.Rows.Add(obj) Next If (dt1.Rows.Count > = dt2.Rows.Count) Then For i As Int32 = 0 To dt2.Rows.Count - 1 For j As Int32 = 0 To dt2.Columns.Count - 1 dt3.Rows(i)(j + dt1.Columns.Count) = dt2.Rows(i)(j).ToString() Next Next Else Dim dr3 As DataRow For i As Int32 = 0 To dt2.Rows.Count - dt1.Rows.Count - 1 dr3 = dt3.NewRow() dt3.Rows.Add(dr3) Next For i As Int32 = 0 To dt2.Rows.Count - 1 For j As Int32 = 0 To dt2.Columns.Count - 1 dt3.Rows(i)(j + dt1.Columns.Count) = dt2.Rows(i)(j).ToString() Next Next End If End Sub End Class __________________________________________________________________________ 由wf5360308(王峰)的C#程序
修改得来。 __________________________________________________________________________