2016-08-21 111 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

你應該像你這樣在第一次設置DateTimePickerValue財產,請不要輸入值作爲一個字符串...相反,它解析爲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:在'DateTime.TryParse'行上放置一個斷點並告訴我'ExpiryDate'的值是多少。 –

相關問題