Databinding, Textboxes, and Table Rowstate

DVader

Member
Joined
Aug 21, 2006
Messages
5
Im developing a form that has five textboxes that I bind to a DataTable in a DataSet.

Code:
Private Sub BindEvent()

        Try
            Me.txtEventNbr.DataBindings.Add(New Windows.Forms.Binding("text", dsSafety.Tables("Event"), "EventID"))
            Me.txtAccidentLocation.DataBindings.Add(New Windows.Forms.Binding("text", dsSafety.Tables("Event"), "LocationDesc"))
            Me.txtAccidentDate.DataBindings.Add(New Windows.Forms.Binding("text", dsSafety.Tables("Event"), "EventDate"))
            Me.txtAccidentTime.DataBindings.Add(New Windows.Forms.Binding("text", dsSafety.Tables("Event"), "EventTime"))
            Me.txtAccidentDesc.DataBindings.Add(New Windows.Forms.Binding("text", dsSafety.Tables("Event"), "EventDesc"))
        Catch ex As Exception
            MessageBox.Show(ex.Message & vbCrLf & vbCrLf & ex.StackTrace, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

If I change a value of one of the textboxes on the form I expect the Rowstate property for the DataTable the textbox is bound to tobe change to Modified, but that isnt happening. It still indicates that the RowState is Unmodified. Though if I check the value of the column in my DataTable it has the value that I keyed into the textbox (using a messagebox behind a button).

I tried adding some additional logic to force an EndCurrentEdit when I change the value of the textbox, but the RowState still says Unmodified.

Code:
Private Sub txtAccidentLocation_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtAccidentLocation.LostFocus

        Try
            Me.BindingContext(dsSafety).EndCurrentEdit()
        Catch ex As Exception
            MessageBox.Show(ex.Message & vbCrLf & vbCrLf & ex.StackTrace, Me.Text, MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try

    End Sub

Any ideas on why the Rowstate of the DataTable doesnt change to Modified when changing the value in the textbox?
 
DVader said:
...
Any ideas on why the Rowstate of the DataTable doesnt change to Modified when changing the value in the textbox?
The reason is because BeginEdit has been implicitly called when you edit the textbox and EndEdit hasnt been called, so RowState remains Unchanged. If you call EndEdit, RowState will change to Modified.

While in edit mode, to find out if a value has been changed, you can check for a proposed version in the DataRow, e.g., .Row.HasVersion(DataRowVersion.Proposed).

Hope this helps!
 
Back
Top