我一直收到以下错误,并且不确定如何修复它。作为VB.NET的一个相当新的用户,我认为它说那个位置没有行?为了弥补这一点,我包含一个If语句来检查行数,但它仍然产生这个错误。实际上,消息框根本没有触发。有人可以告诉我如何纠正这个错误。感谢
获取代码的链接:http://support.microsoft.com/kb/305271/en-us
位置1没有行。
Private Sub loadpages()
Dim i As Integer
Dim startRec As Integer
Dim endRec As Integer
Dim dtTemp As DataTable
'Dim dr As DataRow
'Duplicate or clone the source table to create the temporary table.
dtTemp = dtSource.Clone
If currentPage = PageCount Then
endRec = maxRec
Else
endRec = pageSize * currentPage
End If
startRec = recNo
'Copy the rows from the source table to fill the temporary table.
If dtSource.Rows.Count <> 0 Then
For i = startRec To endRec - 1
dtTemp.ImportRow(dtSource.Rows(i)) <--- ERROR HERE
recNo = recNo + 1
Next
Else
MessageBox.Show(dtSource.Rows.Count.ToString())
End If
frmMain.DGV.DataSource = dtTemp
DisplayPageInfo()
'fillPostings()
End Sub
组合框子更改页面大小
Sub cmbpage()
'Set the start and max records.
pageSize = CInt(frmMain.cmbPageSize.Text)
maxRec = dtSource.Rows.Count
PageCount = maxRec \ pageSize
MessageBox.Show(CStr(maxRec))
' Adjust the page number if the last page contains a partial page.
If (maxRec Mod pageSize) > 0 Then
PageCount = PageCount + 1
End If
'Initial seeings
currentPage = 1
recNo = 0
' Display the content of the current page.
UDGfillPostings()
loadpages()
End Sub
答案 0 :(得分:1)
你可能想要这种循环。
For i = 0 To dtSource.Rows.Count-1
如果您只想将一个DataTable复制到另一个DataTable,那么您可以使用DataTable.Copy方法。
Dim dtTemp As DataTable
dtTemp = dtSource.Copy()
答案 1 :(得分:1)
您已将startRec
指定为1,因此在dtSource.Rows(1)
时抛出错误,因为数组中只有一个元素
您可以使用dtSource.Rows(i-1)