索引超出范围。必须 non-negative 且小于 collection 的大小。日期时间选择器值
Index was out of range. Must be non-negative and less than the size of the collection. Date time picker value
Private Sub IssueDGV_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles IssueDGV.CellMouseClick
Dim row As DataGridViewRow = IssueDGV.Rows(e.RowIndex)
Bidlbl.Text = row.Cells(2).Value.ToString
Booktitlelbl.Text = row.Cells(3).Value.ToString
DateTimePicker1.Value = row.Cells(6).Value.ToString
If MainStudent.stdidTB.Text = "" Then
key = 0
Else
key = Convert.ToInt32(row.Cells(0).Value.ToString)
End If
End Sub
我想要代码 DateTimePicker1.Value = row.Cells(6).Value.ToString
到 return 日期时间选择器的日期,但它 return 是标题中的错误。任何人都可以协助任何相关代码或替代方法来解决问题。
如果可能,您应该使用数据绑定。从 class 开始保存您的数据(也许您已经有一个,但这是一个基本模型),
Private Class DataClass
Public Property Bid As Integer
Public Property BookTitle As String
Public Property [Date] As DateTime
End Class
并填充该模型的列表,并绑定您的 DataGridView,
Dim data As New List(Of DataClass)
data.Add(New DataClass With {.Bid = 1, .BookTitle = "Title 1", .[Date] = DateTime.Now.AddDays(-1)})
data.Add(New DataClass With {.Bid = 2, .BookTitle = "Title 2", .[Date] = DateTime.Now})
IssueDGV.DataSource = data
现在您可以获得保存数据的对象,并使用强名称而不是索引。
Private Sub IssueDGV_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles IssueDGV.CellMouseClick
Dim row As DataGridViewRow = IssueDGV.Rows(e.RowIndex)
Dim item = DirectCast(row.DataBoundItem, DataClass)
Bidlbl.Text = item.Bid.ToString()
Booktitlelbl.Text = item.BookTitle
DateTimePicker1.Value = item.Date
End Sub
您的方法存在一个问题,即列索引与 DataGridView 的填充方式相关联。如果您添加另一列,那么您可能需要更改索引,并且很难跟踪每个列的含义,甚至要收到通知您需要更改索引。
我不知道您的 DataGridView 当前是如何填充的,但如果您没有后备对象并将状态存储在 UI 中,那绝不是一个好主意。 UI 应该只用于信息流,而不是状态。
我想在将字符串转换为 DateTimePicker.Value 的过程中发生了一些事情。
在将其分配给 " DateTimePicker1.Value = "
之前尝试转换您的字符串
*编辑尝试先删除值旁边的 .ToString,然后检查空值
Private Sub IssueDGV_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles IssueDGV.CellMouseClick
Dim row As DataGridViewRow = IssueDGV.Rows(e.RowIndex)
Bidlbl.Text = row.Cells(2).Value.ToString
Booktitlelbl.Text = row.Cells(3).Value.ToString
DateTimePicker1.Value = row.Cells(6).Value.ToString
If MainStudent.stdidTB.Text = "" Then
key = 0
Else
key = Convert.ToInt32(row.Cells(0).Value.ToString)
End If
End Sub
我想要代码 DateTimePicker1.Value = row.Cells(6).Value.ToString
到 return 日期时间选择器的日期,但它 return 是标题中的错误。任何人都可以协助任何相关代码或替代方法来解决问题。
如果可能,您应该使用数据绑定。从 class 开始保存您的数据(也许您已经有一个,但这是一个基本模型),
Private Class DataClass
Public Property Bid As Integer
Public Property BookTitle As String
Public Property [Date] As DateTime
End Class
并填充该模型的列表,并绑定您的 DataGridView,
Dim data As New List(Of DataClass)
data.Add(New DataClass With {.Bid = 1, .BookTitle = "Title 1", .[Date] = DateTime.Now.AddDays(-1)})
data.Add(New DataClass With {.Bid = 2, .BookTitle = "Title 2", .[Date] = DateTime.Now})
IssueDGV.DataSource = data
现在您可以获得保存数据的对象,并使用强名称而不是索引。
Private Sub IssueDGV_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles IssueDGV.CellMouseClick
Dim row As DataGridViewRow = IssueDGV.Rows(e.RowIndex)
Dim item = DirectCast(row.DataBoundItem, DataClass)
Bidlbl.Text = item.Bid.ToString()
Booktitlelbl.Text = item.BookTitle
DateTimePicker1.Value = item.Date
End Sub
您的方法存在一个问题,即列索引与 DataGridView 的填充方式相关联。如果您添加另一列,那么您可能需要更改索引,并且很难跟踪每个列的含义,甚至要收到通知您需要更改索引。
我不知道您的 DataGridView 当前是如何填充的,但如果您没有后备对象并将状态存储在 UI 中,那绝不是一个好主意。 UI 应该只用于信息流,而不是状态。
我想在将字符串转换为 DateTimePicker.Value 的过程中发生了一些事情。 在将其分配给 " DateTimePicker1.Value = "
之前尝试转换您的字符串*编辑尝试先删除值旁边的 .ToString,然后检查空值