Databinding Issue with tab control

Databinding Issue with tab control

Post by mdhama » Sat, 12 Nov 2005 22:19:21

In my application I retrieve a single record from the database and create a
dataset using it. The form contains a tabcontrol with serveral pages. Each
page has few controls added to it. The dataset is bound to different
textboxes on different pages. I have a "Save" button to save the changes to
the database.

By default, the form is loaded with first page of the tab control having
focus. I can see the controls on the first page being filled up with the
values from the dataset. Now if I try to access programatically (on click
event of save button) text of any textboxes on subsequent pages (page 2,
page 3, ...) it returns blank eventhough the underlying dataset has values
for it. The controls on the subsequent pages are filled only when the focus
is set on subsequent pages of the tab control. Basically the controls on
subsequent pages are not getting initialised with values from the dataset.

Is this a default behaviour when using databinding with tab control or a
bug. Is there a workaround for this?

For example:

create a form with a tabcontrol having 2 pages (page 1 and page 2).

On page 1 and 2 add 2 textboxes.

Also add a save button to the form.

In my sample code, the Textbox3 and textbox4 are initialized only when we
set the focus on the page2. To see this once the form is loaded click on
"Save" button it will display values of Textbox3 and textbox4 as blank. Now
go to Page2. on set focus to page2, textchanged event of both textboxes is
fired and textboxes and assigned values to it. Now if you click on "save"
button it will display the values.

Dataset code and bindings code:

Private Function makedataset()
dsTopics = New DataSet("Topics")

dsTopics.Tables.Add("table1") ' table with 4 columns





Dim dr As DataRow = dsTopics.Tables("table1").NewRow()

dr("col1") = "col1"

dr("col2") = "col2"

dr("col3") = "col3"

dr("col4") = "col4"


End Function

Private Function CreateBindings()

TextBox1.DataBindings.Add("Text", dsTopics, "table1.col1") ' on page 1

TextBox2.DataBindings.Add("Text", dsTopics, "table1.col2") ' on page 1

TextBox3.DataBindings.Add("Text", dsTopics, "table1.col3") ' on page 2

TextBox4.DataBindings.Add("Text", dsTopics, "table1.col4") ' on page 2

End Function

Private Sub cmdSaveData_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdSaveData.Click

MsgBox("On Save - TextBox3 : " + TextBox3.Text)

MsgBox("On Save - TextBox4 : " + TextBox4.Text)

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load



End Sub

Private Sub TextBox3_TextChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TextBox3.TextChanged

MsgBox("Text Changed Event - TextBox3 : " + TextBox3.Text)

MsgBox("Text Changed Event - TextBox4 : " + TextBox4.Text)

End Sub

Databinding Issue with tab control

Post by Bart Mermu » Sun, 13 Nov 2005 00:05:36


"mdhaman" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...

I guess default behaviour, because the Controls haven't been visible, they
haven't been created and therefore do not yet participate in DataBinding.

Not that i know.

Instead, use a CurrencyManager to get the DataRowView that is currently
shown and get the values from that:

Private Sub cmdSaveData_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles cmdSaveData.Click
Dim cm As CurrencyManager = DirectCast( _
BindingContext(dsTopics, "table1"), _
CurrencyManager )


Dim drv As DataRowView = DirectCast( _
cm.Current, DataRowView )

MsgBox("On Save - TextBox3 : " + drv("col3") )
MsgBox("On Save - TextBox4 : " + drv("col4") )

End Sub