通常我们都知道可以使用OleDbDataAdapter对象的Fill方法从数据源检索数据并填充DataSet 中的DataTable,其实我们也可以人工为DataTable添加DataRow。
Add和InsertAt方法都可以把新行添加到DataTable。
新一个Windows应用程序,在Form1窗体拉入一个按钮和DataGridView。
在Form1代码界面添加如下代码:
Public Class Form1
'声明DataTable
Dim dt1 As New DataTable
Dim dt2 As New DataTable
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'为dt1创建结构
dt1.Columns.Add("ID", Type.GetType("System.Int32"))
dt1.Columns.Add("Name", Type.GetType("System.String"))
'为dt1添加测试用数据
Dim row As DataRow
For i As Integer = 1 To 10
row = dt1.NewRow
row("ID") = i
row("Name") = "Name" & i.ToString
'添加新行
dt1.Rows.Add(row)
'插入新行
'dt1.Rows.InsertAt(row, 0)
Next
End Sub
End Class
测试代码1:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'复制dt1的结构和数据
dt2 = dt1.Copy()
'UI界面显示dt2数据
DataGridView1.DataSource = Nothing
DataGridView1.DataSource = dt2
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'克隆dt1的结构,包括所有DataTable架构和约束。
dt2 = dt1.Clone()
'UI界面显示dt2数据
DataGridView1.DataSource = Nothing
DataGridView1.DataSource = dt2
End Sub
使用DataTable.Copy方法可以复制DataTable的结构和所有数据行,而有时仅需要复制其中的一行,DataRow没有Copy方法,要如何实现呢?
测试代码1:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'克隆dt1的结构,包括所有DataTable架构和约束。
dt2 = dt1.Clone()
For Each row As DataRow In dt1.Rows
'报错
dt2.Rows.Add(row)
'dt2.Rows.InsertAt(row, 0)
Next
'UI界面显示dt2数据
DataGridView1.DataSource = Nothing
DataGridView1.DataSource = dt2
End Sub
测试结果:
报错:System.ArgumentException:“该行已经属于另一个表。”,如图,
测试代码2:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'克隆dt1的结构,包括所有DataTable架构和约束。
dt2 = dt1.Clone()
For Each row As DataRow In dt1.Rows
dt2.Rows.Add(row.ItemArray)
Next
'UI界面显示dt2数据
DataGridView1.DataSource = Nothing
DataGridView1.DataSource = dt2
End Sub
测试结果:正常
测试代码:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'克隆dt1的结构,包括所有DataTable架构和约束。
dt2 = dt1.Clone()
For Each row As DataRow In dt1.Rows
dt2.ImportRow(row)
Next
'UI界面显示dt2数据
DataGridView1.DataSource = Nothing
DataGridView1.DataSource = dt2
End Sub
测试结果:正常
测试代码1:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'为dt2创建结构
dt2.Columns.Add("dt2ID", Type.GetType("System.Int32"))
dt2.Columns.Add("dt2Name", Type.GetType("System.String"))
For Each row As DataRow In dt1.Rows
dt2.Rows.Add(row.ItemArray)
Next
'UI界面显示dt2数据
DataGridView1.DataSource = Nothing
DataGridView1.DataSource = dt2
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'为dt2创建结构
dt2.Columns.Add("dt2ID", Type.GetType("System.Int32"))
dt2.Columns.Add("dt2Name", Type.GetType("System.String"))
For Each row As DataRow In dt1.Rows
dt2.ImportRow(row)
Next
'UI界面显示dt2数据
DataGridView1.DataSource = Nothing
DataGridView1.DataSource = dt2
End Sub
测试结果:行是复制过来了,但数据为null,说明该方法要求两表架构一致。如图
测试代码:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'为dt2创建结构
dt2.Columns.Add("dt2ID", Type.GetType("System.String"))
dt2.Columns.Add("dt2Name", Type.GetType("System.String"))
For Each row As DataRow In dt1.Rows
dt2.Rows.Add(row.ItemArray)
Next
'UI界面显示dt2数据
DataGridView1.DataSource = Nothing
DataGridView1.DataSource = dt2
End Sub
测试结果:成功复制,可见ItemArray属性的兼容很强。
测试代码1:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'为dt2创建结构
dt2.Columns.Add("dt2ID", Type.GetType("System.String"))
For Each row As DataRow In dt1.Rows
dt2.Rows.Add(row.ItemArray)
Next
'UI界面显示dt2数据
DataGridView1.DataSource = Nothing
DataGridView1.DataSource = dt2
End Sub
测试结果:报错——System.ArgumentException:“输入数组长度大于此表中的列数。如图
测试代码2:真正的自由自在的自定义复制
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'为dt2创建结构
dt2.Columns.Add("dt2ID", Type.GetType("System.String"))
dt2.Columns.Add("myColumn", Type.GetType("System.String"))
dt2.Columns.Add("dt2Name", Type.GetType("System.String"))
Dim newrow As DataRow
For Each row As DataRow In dt1.Rows
newrow = dt2.NewRow
newrow("dt2ID") = row("ID")
newrow("dt2Name") = row("Name")
dt2.Rows.Add(newrow)
Next
'UI界面显示dt2数据
DataGridView1.DataSource = Nothing
DataGridView1.DataSource = dt2
End Sub