Bindings appear lost after changes are cancelled

R

Ron L

All

I am working with a DataGrid and a form consisting of a number of text,
checkbox, combobox controls, all bound to the same datatable. When I click
on my "New" button, I create a new row, make it the current row, and allow
the user to make changes in the form. If the user desires to cancel, they
can click the "Cancel Changes" button. Here is where my problems start.
Once the Cancel Changes button is clicked, the bindings on the form appear
to be permanently lost until I close and re-open the form. I have attached
the relevant portions of the New and Cancel button Click Event routines as
well as a representative portion of the binding initializations. Note that
I only bind the controls at form startup.

Can anyone tell me what I am doing wrong here?

TIA
Ron L



Private Sub BtnNew_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles _btnNew.Click
....
' Create a new row in the data table and change the List Tab's
filter so that the new row is the selected item
Dim dt As DataTable = CType(_ppdctlr.ListGrid.DataSource, DataTable)
Dim dr As DataRow = dt.NewRow
dr.Item("PPDID") = "NewPPD"
dt.Rows.Add(dr)
CType(cm.List, DataView).RowFilter = "PPDID = 'NewPPD'"
....
End Sub

Private Sub CancelChanges(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles _btnCancel.Click
Dim cm As CurrencyManager
Dim drv, newRow As DataRowView
Dim permLevel As Integer

Dim dt As DataTable = CType(_ppdctlr.ListGrid.DataSource, DataTable)
dt.RejectChanges()


' reset the previously selected ID.
_listCtlr.SetCurrentRow(_listSelectedID)
drv = CType(cm.List,
DataView).Item(_ppdctlr.ListGrid.CurrentRowIndex)
permLevel = PermissionLevel(drv)
....
End Sub

Private Sub BindUserControls()
Dim dt As DataTable = _ppdctlr.ListGrid.DataSource

_txtPPDID.DataBindings.Add("text", dt, "PPDID")
_txtGlobalID.DataBindings.Add("text", dt, "GlobalID")
_txtDuplicateOf.DataBindings.Add("text", dt, "DuplicateOf")
_txtCPRRequestSubmitted.DataBindings.Add("text", dt,
"CPRRequestedDate")
_cbCPRRequired.DataBindings.Add("Checked", dt, "CPRRequired")
_cbDPRRequired.DataBindings.Add("Checked", dt, "DPRRequired")
_txtTitle.DataBindings.Add("text", dt, "Title")
_txtModDate.DataBindings.Add("text", dt, "ModifiedDate")
_cboType.DataBindings.Add("text", dt, "Type")
_cboProblemArea.DataBindings.Add("text", dt, "ProblemArea")
....
End Sub
 
C

Cor Ligthert [MVP]

Ron,

I don't see direct the error, however does this work.
Private Sub CancelChanges(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles _btnCancel.Click
Dim cm As CurrencyManager
Dim drv, newRow As DataRowView
Dim permLevel As Integer

Dim dt As DataTable = CType(_ppdctlr.ListGrid.DataSource,
DataTable)
dt.RejectChanges()


' reset the previously selected ID.
_listCtlr.SetCurrentRow(_listSelectedID)
drv = CType(cm.List,
DataView).Item(_ppdctlr.ListGrid.CurrentRowIndex)

That cm (currencymanager is just an address in my opinon without any
reference in it).

Cor
 
R

Ron L

Cor

Thanks for the response. My Bad, the assignment to cm was in the code I cut
for clarity. The following line is in my code just after the
dt.RejectChanges line:

cm =
CType(_ppdctlr.ListGrid.BindingContext(CType(_ppdctlr.ListGrid.DataSource,
DataTable)), CurrencyManager)

Basically, my problem appears to be that the bindings are lost when the
changes are cancelled.

Ron L
 
B

Bart Mermuys

Hi,

Ron L said:
All

I am working with a DataGrid and a form consisting of a number of text,
checkbox, combobox controls, all bound to the same datatable. When I
click on my "New" button, I create a new row, make it the current row, and
allow the user to make changes in the form. If the user desires to
cancel, they can click the "Cancel Changes" button. Here is where my
problems start. Once the Cancel Changes button is clicked, the bindings on
the form appear to be permanently lost until I close and re-open the form.
I have attached the relevant portions of the New and Cancel button Click
Event routines as well as a representative portion of the binding
initializations. Note that I only bind the controls at form startup.

Can anyone tell me what I am doing wrong here?

Did you already set default values for the fields that are bound to
CheckBox, CheckBox can't handle null values (common for new rows) and may
crash the binding, eg.:

dt.Columns( "DPRRequired").DefaultValue = false
dt.Columns( "CPRRequired").DefaultValue = false

Instead you could probely also set the default values after you create the
new row (dt.NewRow), *but* before you add it to the Rows collection.

I know you are not using CurrencyManager.AddNew but the problem with
CheckBox is described here:
http://support.microsoft.com/default.aspx?scid=kb;en-us;326440


hth,
greetings
 
C

Cor Ligthert [MVP]

Ron,

May I try to make it for me more clearer.

You add a datarow.
That is in the table the row
dt.rows(count-1)

I would in this remove or delete that datarow again when it is canceled,
what is in this action the same because it is an added row.

Cor
 
C

Cor Ligthert [MVP]

The same is that delete or remove in this case. A little bit unclear when I
have read it back.
 
R

Ron L

Cor

I have tried using both the .RejectChanges method and row.Delete method, and
both have the same effect - my form's fields are empty and won't bind when
another row is selected in the list.

Ron L
 
R

Ron L

Bart

Thanks for the thought, but that was apparently not the problem. I put in
the DefaultValues on each of those columns and still have the same results.

Ron L
 
M

Miha Markic [MVP C#]

Hi Ron,

What exactly do you mean by bindings are lost?
You can't create new rows?

Do you realize that calling dt.RejectChanges will remove all rows that
aren't yet stored in database (DataRowState.Added) and cancel all other
changes?
Is this the answer?
 
R

Ron L

Miha

Thanks for the response. Yes, I am aware that this will remove all rows
that aren't stored yet, that is what I am trying to do.

What is happening is that I create a new row which is made current in the
form. If I cancel the form (either deleting that row explicitly or doing a
RejectChanges), the previously selected record should now be displayed in
the form, but the form stays blank. If I go back to my form and select
another row my form remains blank until I reinitialize it.

Ron L


Miha Markic said:
Hi Ron,

What exactly do you mean by bindings are lost?
You can't create new rows?

Do you realize that calling dt.RejectChanges will remove all rows that
aren't yet stored in database (DataRowState.Added) and cancel all other
changes?
Is this the answer?

--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Ron L said:
All

I am working with a DataGrid and a form consisting of a number of text,
checkbox, combobox controls, all bound to the same datatable. When I
click on my "New" button, I create a new row, make it the current row,
and allow the user to make changes in the form. If the user desires to
cancel, they can click the "Cancel Changes" button. Here is where my
problems start. Once the Cancel Changes button is clicked, the bindings
on the form appear to be permanently lost until I close and re-open the
form. I have attached the relevant portions of the New and Cancel button
Click Event routines as well as a representative portion of the binding
initializations. Note that I only bind the controls at form startup.

Can anyone tell me what I am doing wrong here?

TIA
Ron L



Private Sub BtnNew_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles _btnNew.Click
...
' Create a new row in the data table and change the List Tab's
filter so that the new row is the selected item
Dim dt As DataTable = CType(_ppdctlr.ListGrid.DataSource,
DataTable)
Dim dr As DataRow = dt.NewRow
dr.Item("PPDID") = "NewPPD"
dt.Rows.Add(dr)
CType(cm.List, DataView).RowFilter = "PPDID = 'NewPPD'"
...
End Sub

Private Sub CancelChanges(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles _btnCancel.Click
Dim cm As CurrencyManager
Dim drv, newRow As DataRowView
Dim permLevel As Integer

Dim dt As DataTable = CType(_ppdctlr.ListGrid.DataSource,
DataTable)
dt.RejectChanges()


' reset the previously selected ID.
_listCtlr.SetCurrentRow(_listSelectedID)
drv = CType(cm.List,
DataView).Item(_ppdctlr.ListGrid.CurrentRowIndex)
permLevel = PermissionLevel(drv)
...
End Sub

Private Sub BindUserControls()
Dim dt As DataTable = _ppdctlr.ListGrid.DataSource

_txtPPDID.DataBindings.Add("text", dt, "PPDID")
_txtGlobalID.DataBindings.Add("text", dt, "GlobalID")
_txtDuplicateOf.DataBindings.Add("text", dt, "DuplicateOf")
_txtCPRRequestSubmitted.DataBindings.Add("text", dt,
"CPRRequestedDate")
_cbCPRRequired.DataBindings.Add("Checked", dt, "CPRRequired")
_cbDPRRequired.DataBindings.Add("Checked", dt, "DPRRequired")
_txtTitle.DataBindings.Add("text", dt, "Title")
_txtModDate.DataBindings.Add("text", dt, "ModifiedDate")
_cboType.DataBindings.Add("text", dt, "Type")
_cboProblemArea.DataBindings.Add("text", dt, "ProblemArea")
...
End Sub
 
B

Bart Mermuys

Hi,

Ron L said:
Bart

Thanks for the thought, but that was apparently not the problem. I put in
the DefaultValues on each of those columns and still have the same
results.

Even though it doesn't help with your problem you still need to do this,
because the CheckBox will cause problems without a default value.

Other reasons that may cause independent navigating:
- DataSource of DataGrid and the other Controls are no longer the same.
They need to be exactly the same not just producing the same data.
- You have set a new BindingContext to either the DataGrid or to the
UserControl the DataGrid is on.
- You called CurrencyManager.SuspendBinding.

Check whether your app hangs for a small time the first time you add or
cancel the new record, which would indicate some Exception being thrown.

Another thing you can do is check if the CurrencyManager _after_ the problem
is still the same for the DataGrid and one of the TextBoxs, eg.:

Dim tbCM As BindingManagerBase = _
_txtPPDID.DataBindings("Text").BindingManagerBase

Dim dgCM As BindingManagerBase = _
_ppdctlr.ListGrid.BindingContext( _
_ppdctlr.ListGrid.DataSource, _
_ppdctlr.ListGrid.DataMember )

' Should return True
Console.WriteLine( Equals(tbCM, dgCM) )

' Should return True
Console.WriteLine( _
_txtPPDID.DataBindings("Text").IsBinding )


HTH,
Greetings
 
R

Ron L

Bart

Thanks, I inserted that code and both show true. I have noticed that if I
move my RejectChanges to follow the code where I choose the previously
selected item, I now have the text fields being filled in, but now my
selection on the Grid doesn't always update the CurrentRowIndex. Whether or
not it changes CurrentRowIndex seems to have to do with one of my fields in
the Grid - one that determines whether the panel others are on is displayed
or hidden I guess I'll have to keep digging there.

Ron L
 
M

Miha Markic [MVP C#]

Some parts of your code are missing, for example, how do you get
CurrencyManager.
If you create a sample simple as possible that reporduces the problem (you
can send it to me if you wish) it will be easier to understand what exactly
you are doing.

--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Ron L said:
Miha

Thanks for the response. Yes, I am aware that this will remove all rows
that aren't stored yet, that is what I am trying to do.

What is happening is that I create a new row which is made current in the
form. If I cancel the form (either deleting that row explicitly or doing
a RejectChanges), the previously selected record should now be displayed
in the form, but the form stays blank. If I go back to my form and select
another row my form remains blank until I reinitialize it.

Ron L


Miha Markic said:
Hi Ron,

What exactly do you mean by bindings are lost?
You can't create new rows?

Do you realize that calling dt.RejectChanges will remove all rows that
aren't yet stored in database (DataRowState.Added) and cancel all other
changes?
Is this the answer?

--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Ron L said:
All

I am working with a DataGrid and a form consisting of a number of text,
checkbox, combobox controls, all bound to the same datatable. When I
click on my "New" button, I create a new row, make it the current row,
and allow the user to make changes in the form. If the user desires to
cancel, they can click the "Cancel Changes" button. Here is where my
problems start. Once the Cancel Changes button is clicked, the bindings
on the form appear to be permanently lost until I close and re-open the
form. I have attached the relevant portions of the New and Cancel button
Click Event routines as well as a representative portion of the binding
initializations. Note that I only bind the controls at form startup.

Can anyone tell me what I am doing wrong here?

TIA
Ron L



Private Sub BtnNew_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles _btnNew.Click
...
' Create a new row in the data table and change the List Tab's
filter so that the new row is the selected item
Dim dt As DataTable = CType(_ppdctlr.ListGrid.DataSource,
DataTable)
Dim dr As DataRow = dt.NewRow
dr.Item("PPDID") = "NewPPD"
dt.Rows.Add(dr)
CType(cm.List, DataView).RowFilter = "PPDID = 'NewPPD'"
...
End Sub

Private Sub CancelChanges(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles _btnCancel.Click
Dim cm As CurrencyManager
Dim drv, newRow As DataRowView
Dim permLevel As Integer

Dim dt As DataTable = CType(_ppdctlr.ListGrid.DataSource,
DataTable)
dt.RejectChanges()


' reset the previously selected ID.
_listCtlr.SetCurrentRow(_listSelectedID)
drv = CType(cm.List,
DataView).Item(_ppdctlr.ListGrid.CurrentRowIndex)
permLevel = PermissionLevel(drv)
...
End Sub

Private Sub BindUserControls()
Dim dt As DataTable = _ppdctlr.ListGrid.DataSource

_txtPPDID.DataBindings.Add("text", dt, "PPDID")
_txtGlobalID.DataBindings.Add("text", dt, "GlobalID")
_txtDuplicateOf.DataBindings.Add("text", dt, "DuplicateOf")
_txtCPRRequestSubmitted.DataBindings.Add("text", dt,
"CPRRequestedDate")
_cbCPRRequired.DataBindings.Add("Checked", dt, "CPRRequired")
_cbDPRRequired.DataBindings.Add("Checked", dt, "DPRRequired")
_txtTitle.DataBindings.Add("text", dt, "Title")
_txtModDate.DataBindings.Add("text", dt, "ModifiedDate")
_cboType.DataBindings.Add("text", dt, "Type")
_cboProblemArea.DataBindings.Add("text", dt, "ProblemArea")
...
End Sub
 
R

Ron L

Miha

After looking more into the probem, it appears that there is some
interrelationship with the data. I am not sure quite what the problem is
yet but it appears only with certain rows of my data. We are still trying
to figure it out, but it appears to be related to a pair of combo boxes who
may or may not have a value set, depending on other data selections in the
record.

Ron L


Miha Markic said:
Some parts of your code are missing, for example, how do you get
CurrencyManager.
If you create a sample simple as possible that reporduces the problem
(you can send it to me if you wish) it will be easier to understand what
exactly you are doing.

--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

Ron L said:
Miha

Thanks for the response. Yes, I am aware that this will remove all rows
that aren't stored yet, that is what I am trying to do.

What is happening is that I create a new row which is made current in the
form. If I cancel the form (either deleting that row explicitly or doing
a RejectChanges), the previously selected record should now be displayed
in the form, but the form stays blank. If I go back to my form and
select another row my form remains blank until I reinitialize it.

Ron L


Miha Markic said:
Hi Ron,

What exactly do you mean by bindings are lost?
You can't create new rows?

Do you realize that calling dt.RejectChanges will remove all rows that
aren't yet stored in database (DataRowState.Added) and cancel all other
changes?
Is this the answer?

--
Miha Markic [MVP C#]
RightHand .NET consulting & development www.rthand.com
Blog: http://cs.rthand.com/blogs/blog_with_righthand/

All

I am working with a DataGrid and a form consisting of a number of text,
checkbox, combobox controls, all bound to the same datatable. When I
click on my "New" button, I create a new row, make it the current row,
and allow the user to make changes in the form. If the user desires to
cancel, they can click the "Cancel Changes" button. Here is where my
problems start. Once the Cancel Changes button is clicked, the bindings
on the form appear to be permanently lost until I close and re-open the
form. I have attached the relevant portions of the New and Cancel
button Click Event routines as well as a representative portion of the
binding initializations. Note that I only bind the controls at form
startup.

Can anyone tell me what I am doing wrong here?

TIA
Ron L



Private Sub BtnNew_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles _btnNew.Click
...
' Create a new row in the data table and change the List Tab's
filter so that the new row is the selected item
Dim dt As DataTable = CType(_ppdctlr.ListGrid.DataSource,
DataTable)
Dim dr As DataRow = dt.NewRow
dr.Item("PPDID") = "NewPPD"
dt.Rows.Add(dr)
CType(cm.List, DataView).RowFilter = "PPDID = 'NewPPD'"
...
End Sub

Private Sub CancelChanges(ByVal sender As System.Object, ByVal e As
System.EventArgs) _
Handles _btnCancel.Click
Dim cm As CurrencyManager
Dim drv, newRow As DataRowView
Dim permLevel As Integer

Dim dt As DataTable = CType(_ppdctlr.ListGrid.DataSource,
DataTable)
dt.RejectChanges()


' reset the previously selected ID.
_listCtlr.SetCurrentRow(_listSelectedID)
drv = CType(cm.List,
DataView).Item(_ppdctlr.ListGrid.CurrentRowIndex)
permLevel = PermissionLevel(drv)
...
End Sub

Private Sub BindUserControls()
Dim dt As DataTable = _ppdctlr.ListGrid.DataSource

_txtPPDID.DataBindings.Add("text", dt, "PPDID")
_txtGlobalID.DataBindings.Add("text", dt, "GlobalID")
_txtDuplicateOf.DataBindings.Add("text", dt, "DuplicateOf")
_txtCPRRequestSubmitted.DataBindings.Add("text", dt,
"CPRRequestedDate")
_cbCPRRequired.DataBindings.Add("Checked", dt, "CPRRequired")
_cbDPRRequired.DataBindings.Add("Checked", dt, "DPRRequired")
_txtTitle.DataBindings.Add("text", dt, "Title")
_txtModDate.DataBindings.Add("text", dt, "ModifiedDate")
_cboType.DataBindings.Add("text", dt, "Type")
_cboProblemArea.DataBindings.Add("text", dt, "ProblemArea")
...
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top