How to convert string into datetime
Using VB.Net
When i click the gridview column, that selected column items should appear in the datetime picker
GridView Column value is string, datetime picker datatype is datetime
Code
Private Sub gridview1_CellContentDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles gridview1.CellContentDoubleClick
Dim i As Integer
i = gridview1.CurrentRow.Index
Dim dt As String = gridview1.Item(1, i).Value
datetimepicker1.value = CD开发者_开发技巧ate(dt)
End Sub
When i clicked the gridview column, it is showing error as "Conversion from string "25/09/2011" to type 'Date' is not valid"
How to solve the above problem
Need VB.Net Code Help....
This problem is related to locale settings in that the CDATE() function expects date values to be provided in the following format: MM/DD/YYYY (MM = Month, DD = Day, YYYY = Year). This means the code fails in the UK which is DD/MM/YYYY. Passing 25/09/2011 is translated to:
- Month = 25
- Day = 09
- Year = 2011
If the value you're using is a string you'll need to parse the string value to extract the correct values before passing it to the CDate() function:
Private Sub gridview1_CellContentDoubleClick(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) _
Handles holidaygrid.CellContentDoubleClick
textbox1.Text = DateTime.ParseExact( _
gridview1.Item(1, gridview1.CurrentRow.Index).Value, _
"dd/MM/yyyy", CultureInfo.InvariantCulture).ToString()
End Sub
If you know which date format to expect in advance, use DateTime.ParseExact, e.g.:
datetimepicker1.Value = DateTime.ParseExact(dt, "dd/MM/yyyy", CultureInfo.InvariantCulture)
精彩评论