I'm having issues with the updated value in the CheckBox not updating the DataTable. When I use DataTable.GetChanges I get Nothing even though I changed the check box value by clicking on it.
This CheckBox is bound to a DataTable and it does load the values from the BindingSource. The code runs fine but it is just not noticing the changes in the property... at least I have not been able to get the changes by using the DataTable.GetChanges method.
Here is some of my code:
Public Class ManageSchedule
Private _bsSchedule as BindingSource
Private _dtSchedule As DataTable
Private Sub ManageSchedule_Load(sender As Object, e As EventArgs) Handles Me.Load
Try
Dim bsCleaningPersons As BindingSource = DataAccess.GetActiveCleaningPersonsRecords()
Dim dtCleaningPersons As DataTable = bsCleaningPersons.DataSource
With CleaningPersonComboBox
.DataSource = dtCleaningPersons
.DisplayMember = dtCleaningPersons.Columns("CleaningPersonName").ToString()
.ValueMember = dtCleaningPersons.Columns("CleaningPersonId").ToString()
End With
Dim bsAssignedTo As BindingSource = DataAccess.GetActiveAssignedToRecords()
Dim dtAssignedTo As DataTable = bsAssignedTo.DataSource
With AssignedToComboBox
.DataSource = dtAssignedTo
.DisplayMember = dtAssignedTo.Columns("AssignedToName").ToString()
.ValueMember = dtAssignedTo.Columns("AssignedToId").ToString()
End With
_bsSchedule = DataAccess.GetSchedulesByScheduleIdRecord(ScheduleIdLookup)
_dtSchedule = _bsSchedule.DataSource
If Not IsNothing(_dtSchedule) Then
With _dtSchedule
ScheduleIdTextBox.DataBindings.Add(New Binding("Text", _dtSchedule, .Columns("ScheduleId").ToString(), False, DataSourceUpdateMode.OnPropertyChanged))
AppointmentDateTimePicker.DataBindings.Add(New Binding("Value", _dtSchedule, .Columns("AppointmentDate").ToString()))
CleaningPersonComboBox.DataBindings.Add(New Binding("SelectedValue", _dtSchedule, .Columns("CleaningPersonsId").ToString()))
AssignedToComboBox.DataBindings.Add(New Binding("SelectedValue", _dtSchedule, .Columns("AssignedToId").ToString()))
ArrivedLateCheckBox.DataBindings.Add(New Binding("Checked", _dtSchedule, .Columns("ArrivedLate").ToString(), False, DataSourceUpdateMode.OnPropertyChanged))
End With
If _dtSchedule.Rows.Count = 0 Then
AppointmentDateTimePicker.Value = Today
CleaningPersonComboBox.SelectedValue = -1
AssignedToComboBox.SelectedValue = -1
End If
End If
NewAppointmentLabel.Visible = (Trim(ScheduleIdTextBox.Text) = "")
Catch ex As Exception
MessageBox.Show(ex.Message & vbCrLf & vbCrLf & $"Stack Trace:" & vbCrLf & ex.StackTrace)
End Try
End Sub
Private Sub ManageSchedule_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
Dim dChanges As DataTable = _dtSchedule.GetChanges()
If Not IsNothing(dChanges) Then
Select Case MessageBox.Show($"Do you want to save your changes before closing this form?",caption := $"Save Changes?",buttons := MessageBoxButtons.YesNoCancel )
Case DialogResult.Yes
If SaveChanges() = Failed Then
e.Cancel = True
End If
Case DialogResult.No
'Don't do anything, just continue closing.
Case DialogResult.Cancel
'Cancel closing
e.Cancel = True
End Select
End If
End Sub
End Class
CodePudding user response:
Bind your DataTable
to a BindingSource
and bind that to your control(s). Call EndEdit
on the BindingSource
before trying to use the data in the DataTable
.
CodePudding user response:
I got it to work by adding these two lines of code at the beginning of the FormClosing event.
Validate()
_bsSchedule.EndEdit()
Thanks in part to jmcilhinney in a comment on this page... DataTable.GetChanges() not working as expected
I had searched Google and StackOverflow but must not have searched for the proper terms to find that answer.