我在VB中使用DataGridView时遇到错误。我在Visual Studio 2015中处理这个问题,与AccessDB相关联。错误是"Index was out of range. Must be non-negative and less than the size of the collection."
我有一个搜索按钮,当单击它时,它会使用来自数据库的信息更新DataGrid。现在,我希望能够选中一个框并执行一个操作,以便在单击RENEW按钮时更新日期。当我选中复选框并单击续订时,我收到错误时即可。我试过寻找解决方案,但我没有得到一个好的答案。这是我的代码(这些行标有****,代码中出现了错误。一切正常,直到这一点为止):
Public Class MyAccount
Dim RowData As Object
Dim CheckoutPeriod As Long
Dim NumberOfRows As Integer
Dim CheckedOutRecord As Integer
Dim bookinfo As Object
Dim resourceid As String
Dim Resourcestatus As String
Dim DueDate As Date
Dim checkoutdate As Date
Dim memberid As String = GlobalVariables.LogInMemberID
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
dgvMyAccount.Rows.Clear()
'determine the number of books the current member has checked out.
NumberOfRows = CheckoutTableAdapter.FillByMemberCheckout(Library_DBDataSet.Checkout, memberid)
'if the member has at least one book checked out then…
If NumberOfRows > 0 Then
Dim x As Integer = 0
'begin a loop that will add information for each of the currently checked out books one at a time, to the DataGridView
'you can reorder these columns, remove some of them or add new ones as you wish
For x = 0 To NumberOfRows - 1 Step 1
bookinfo = CheckOutTableAdapter.GetDataByMemberCheckout(memberid)(x)
resourceid = bookinfo.resourceID
RowData = ResourceTableAdapter.GetDataByResourceID(resourceid)(0)
'calculate the due date for a book based on the check out date and the book’s checkout period.
checkoutdate = bookinfo.checkoutdate
CheckoutPeriod = RowData.checkoutperiod
DueDate = DateAdd(DateInterval.Day, CheckoutPeriod, checkoutdate)
'at “due” to the due date so that the member knows when the book is due
Resourcestatus = "Due " & DueDate.Date
'add information for the checked out resource to the DataGridView
Dim dgvRow As New DataGridViewRow
Dim dgvCell As DataGridViewCell
'add title to the first column of the DataGridView
dgvCell = New DataGridViewTextBoxCell()
dgvCell.Value = RowData.title
dgvRow.Cells.Add(dgvCell)
'add author’s last name to the 2nd column of the DataGridView
dgvCell = New DataGridViewTextBoxCell()
dgvCell.Value = RowData.authorlast
dgvRow.Cells.Add(dgvCell)
'add publication date to the 3rd column of the DataGridView
dgvCell = New DataGridViewTextBoxCell()
dgvCell.Value = RowData.publicationdate
dgvRow.Cells.Add(dgvCell)
'add series to the 4th column of the DataGridView
dgvCell = New DataGridViewTextBoxCell()
dgvCell.Value = RowData.Series
dgvRow.Cells.Add(dgvCell)
'add checkout data to the 5th column of the DataGridView
dgvCell = New DataGridViewTextBoxCell()
dgvCell.Value = checkoutdate.Date
dgvRow.Cells.Add(dgvCell)
'add resourcestatus, created above, to the 6th column of the DataGridView
dgvCell = New DataGridViewTextBoxCell()
dgvCell.Value = Resourcestatus
dgvRow.Cells.Add(dgvCell)
'add checkout period to the 7th column of the DataGridView
dgvCell = New DataGridViewTextBoxCell()
dgvCell.Value = RowData.CheckOutPeriod
dgvRow.Cells.Add(dgvCell)
'add checkout period to the 8th column of the DataGridView
dgvCell = New DataGridViewTextBoxCell()
dgvCell.Value = RowData.ResourceID
dgvRow.Cells.Add(dgvCell)
dgvMyAccount.Rows.Add(dgvRow)
'Results is the name of the DataGridView Control added to the Form
Next
ElseIf NumberOfRows = 0 Then
MessageBox.Show("You have no items checked out at this time.")
End If
End Sub
Private Sub CheckoutBindingNavigatorSaveItem_Click(sender As Object, e As EventArgs)
Me.Validate()
Me.CheckoutBindingSource.EndEdit()
Me.TableAdapterManager.UpdateAll(Me.Library_DBDataSet)
End Sub
Private Sub MyAccount_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'Library_DBDataSet.Resource' table. You can move, or remove it, as needed.
Me.ResourceTableAdapter.Fill(Me.Library_DBDataSet.Resource)
'TODO: This line of code loads data into the 'Library_DBDataSet.Checkout' table. You can move, or remove it, as needed.
Me.CheckoutTableAdapter.Fill(Me.Library_DBDataSet.Checkout)
End Sub
Private Sub btnRenew_Click(sender As Object, e As EventArgs) Handles btnRenew.Click
Dim currentdate As Date = Date.Today.Date
NumberOfRows = CheckoutTableAdapter.FillByMemberCheckout(Library_DBDataSet.Checkout, memberid)
If NumberOfRows = 0 Then
MessageBox.Show("You have no items checked out at this time.")
Else
If dgvMyAccount.SelectedCells.Count = 0 Then
MessageBox.Show("Please select the book you wish to renew")
Else
'renew book by altering the due date and updating the record selected in ‘the “results” data grid
'the numbers in parentheses are the index of the 'data grid column holding the piece of
'Information needed.
'***********************************************
resourceid = dgvMyAccount.SelectedCells(7).Value
'***********************************************
CheckoutTableAdapter.RenewBookUpdateQuery(currentdate, resourceid)
CheckoutPeriod = dgvMyAccount.SelectedCells(6).Value
DueDate = DateAdd(DateInterval.Day, CheckoutPeriod, currentdate)
Resourcestatus = "Due " & DueDate.Date
dgvMyAccount.SelectedCells(5).Value = Resourcestatus
dgvMyAccount.SelectedCells(4).Value = currentdate
MessageBox.Show("you have successfully renewed your book.", " Book Renewal Successful", MessageBoxButtons.OK)
End If
End If
End Sub
End Class