我正在尝试定义一个数据行以保存值,然后将其添加到数据集中 indgv:datagridview,其中包含值 dsdetails:包含名为details
的表的数据集If indgv.Rows.Count > 0 Then
Dim dr As DataRow
dr = dsdetails.Tables("details").NewRow
For Each row As DataGridViewRow In indgv.Rows
dr("mat") = row.Cells("icode").Value
dr("dateoftrans") = Me.DateTimePicker1.Value
dr("numoftrans") = transnum.Text
dr("type") = 1
dr("doc") = doctyp.SelectedValue
dr("amount") = row.Cells("iamo").Value
dsdetails.Tables("details").Rows.Add(dr)
Next
adpdetails.Update(dsdetails, "details")
End If
运行此操作会导致以下错误
对象引用未设置为对象的实例。
如何使用'New'重新声明声明以避免问题 BTW:使用new时如下
Dim dr As New DataRow = dsdetails.Tables("details").NewRow
显示设计时错误
未定义类型'dsdetails.Tables'。
答案 0 :(得分:0)
试试这段代码:
If indgv.Rows.Count > 0 Then
Dim tbl As DataTable = dsdetails.Tables("details")
Dim dr As DataRow
For Each row As DataGridViewRow In indgv.Rows
dr = tbl.NewRow 'Create a new row inside the loop!
dr("mat") = row.Cells("icode").Value
dr("dateoftrans") = Me.DateTimePicker1.Value
dr("numoftrans") = transnum.Text
dr("type") = 1
dr("doc") = doctyp.SelectedValue
dr("amount") = row.Cells("iamo").Value
tbl.Rows.Add(dr)
Next
adpdetails.Update(tbl)
End If
答案 1 :(得分:0)