0
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

I want the code DateTimePicker1.Value = row.Cells(6).Value.ToString to return the date to the datetimepicker but it returns the error in the title. May anyone assist with any relevant code or alternate way to approach the problem.

Charlieface
  • 52,284
  • 6
  • 19
  • 43
  • Apparently there is no `Cells(6)`, as that's the only spot in that line of code that would cause that error. – Ken White Oct 18 '21 at 18:47
  • You should really bind a DataSource to your DataGridView so you can just get the date from a property of the DataSource instead of using arbitrary indices. – djv Oct 18 '21 at 19:56
  • Does this answer your question? [What is an IndexOutOfRangeException / ArgumentOutOfRangeException and how do I fix it?](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f) – Charlieface Oct 18 '21 at 20:20
  • Start counting the index of your fields at 0. The first field is index 0. When you get your indexes straightened out be careful with `DateTimePicker1.Value = row.Cells(6).Value.ToString` The `Value` property of a `DateTimePicker` is type `DateTime` and you are providing a `String`. – Mary Oct 19 '21 at 04:12

2 Answers2

0

You should use databindings if possible. Start with a class to hold your data (maybe you already have one but this is a basic model),

Private Class DataClass
    Public Property Bid As Integer
    Public Property BookTitle As String
    Public Property [Date] As DateTime
End Class

and populate a list of that model, and bind your 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

and now you can get the object which holds your data, and use strong names instead of indices.

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

One problem with your approach is that the column indices are tied to how your DataGridView is populated. If you add another column, then you would need to potentially change the indices, and it is difficult to keep track of what each means, and to even be notified you need to change the indices.

I don't know how your DataGridView is currently populated but if you don't have a backing object and are storing state in the UI, that is never a good idea. The UI should only be used for information flow, not state.

djv
  • 15,168
  • 7
  • 48
  • 72
0

I imagine there's something going on during the string conversion to DateTimePicker.Value. Try Converting your string before assigning it to " DateTimePicker1.Value = "

*edit try removing the .ToString next to value first, and check for null values

these links may be helpful. https://learn.microsoft.com/en-us/dotnet/api/system.windows.forms.datetimepicker.value?view=windowsdesktop-5.0

https://www.educba.com/string-to-date-c-sharp/

drpepper1324
  • 113
  • 9