2016-08-21 2 views
0

Каждый раз, когда я хочу редактировать данные, DateTimePicker игнорирует или не принимает значение из DataGridView. Я попытался отладить его, но он не работает, DateTimePicker остается тем же, он не меняет базу значений из DataGridView.DateTimePicker не принимает значение даты из DataGridView в форму редактирования

image

Вот код:
код, чтобы открыть форму редактирования:

Dim editData As New AddData 

Private Sub editForm() 
     editData.Options = "EditData" 
     editData.load_leasee(leasee_id) 
     editData.ShowDialog() 
End Sub 

Private Sub EditToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles EditToolStripMenuItem.Click 
     editForm() 
End Sub 

код для функции CellClickValue и DataGridView1_CellClick:

Private Sub CellClickValue(ByVal selectedRow As DataGridViewRow) 
     selectedRow = DataGridView1.Rows(index) 

     editData.ExpiryDate = Format(selectedRow.Cells(15).Value, "yyyy-MM-dd").ToString() 'Expiry Date 

End Sub 

Dim selectedRow As DataGridViewRow 

Private Sub DataGridView1_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellClick 
     'Dim index As Integer 
     index = e.RowIndex 

     Console.WriteLine("index: " & index) 

     If index > -1 Then 

      CellClickValue(selectedRow) 
     Else 
      'Button2_EditForm.Enabled = False 
      disableButton_InHome() 
     End If 

    End Sub 

код для формы редактирования данных , Ошибка здесь:

Public Property Options As String 

    Public Property ExpiryDate As String 


    Public Sub showInputs() 
     Try 
      ' 
      DateTimePicker2_ExpDt.Value = ExpiryDate.ToString() 'Ignores the value 
      DateTimePicker2_ExpDt.Text = ExpiryDate.ToString() 'Ignores the value 
      ' 
      TextBox4_ExpDt.Text = ExpiryDate 'Accepts the value 
     Catch ex As Exception 
      TextBox4_ExpDt.Text = "" 
      DateTimePicker2_ExpDt.Text = "" 
     End Try 
    End Sub 


    Private Sub loadOptions() 
     Console.WriteLine("Options: " & Options) 

     If Options = "EditData" Then 
      Me.Text = "Edit Data" 
      showInputs() 
     End If 
    End Sub 

ответ

0

Вы должны установить DateTimePicker «s Value свойства, как вы делали в первом, но вы не должны ввести значение в виде строки ... Вместо того, чтобы разобрать его к DateTime structure:

Dim ResultDate As DateTime 
If DateTime.TryParse(ExpiryDate, ResultDate) = True Then 
    DateTimePicker2_ExpDt.Value = ResultDate 
Else 
    'Do what you want when 'ExpiryDate' could not be parsed into a DateTime object. 
End If 
+0

Это сработало, но когда я снова отредактировал, DateTimePicker снова сбрасывается. –

+0

Я обнаружил, что каждый раз, когда я очищаю или обновляю DateTimePicker после закрытия формы, он больше не работает. –

+0

@GuideMe: [Разместить точку останова] (http://www.visual-basic-tutorials.com/beginner/Breakpoints.html) в строке 'DateTime.TryParse' и сообщить мне, что такое значение' ExpiryDate'. –

Смежные вопросы