Help on databinding with tabpages

R

Richard

I have a form with seven tapages. These span only one record with a
large number of fields (textboxes).

On Tabpage1 I display a number of read-only text boxes. This displays
information about a particular Seller and is read from a
SellerDataview at form load.

On Tabpages 2-7, the information comes from another table/another
DealDataview.

What I want is the user to scroll through the records which will
affect only Tabpages 2-7.

My problem is that at load, none of the information from any of the
tabpages displays data. Only on debug do the textboxes read that data
is written to them (Tabpage1), but this data never displays on the
actual form. For tabpages 2-7, nothing seems to be written at all.

I understand that I may need to manually set the binding context of
each tabpage to the form's binding context, but as I am working iwth
two dataviews, I'm a little confused.

HELP?!!




Richard
 
K

Ken Tucker [MVP]

Hi,

Here a link on how to bind a control to a datasource.
http://msdn.microsoft.com/library/d...windowsformscontrolclassdatabindingstopic.asp

VB.net does not update the info in bound controls until they are
shown. Since all the tabpages are not visible when the form is loaded the
controls on those tabpages will not contain data until they are shown.

Ken
-----------------
I have a form with seven tapages. These span only one record with a
large number of fields (textboxes).

On Tabpage1 I display a number of read-only text boxes. This displays
information about a particular Seller and is read from a
SellerDataview at form load.

On Tabpages 2-7, the information comes from another table/another
DealDataview.

What I want is the user to scroll through the records which will
affect only Tabpages 2-7.

My problem is that at load, none of the information from any of the
tabpages displays data. Only on debug do the textboxes read that data
is written to them (Tabpage1), but this data never displays on the
actual form. For tabpages 2-7, nothing seems to be written at all.

I understand that I may need to manually set the binding context of
each tabpage to the form's binding context, but as I am working iwth
two dataviews, I'm a little confused.

HELP?!!




Richard
 
R

Richard

Thanks for this Ken.

The form works well with the controls on the form itself. However on
the tabpages they will not show the data. Even adding the statement

for each tb as tabpage in tabcontrol.pages
tb.bindingcontext = me.bindingcontext
next tb

and, at the end of the load process

tabpage1.show()
tabpage2.show()

seems to have no effect.

Or do I need to show each tabpage before I am able to bind the
controls that exist on each tabpage?

Richard
 
R

Richard

Hi Ken,

I've spent another day on this but am really not getting very far.

My databinding is fine. If I move one of the text boxes to the main
form, the data displays as usual. But as soon as they are on the
tabpage, I am unable to get the tabpages to display any data
whatsoever from the dataset.

So the problem must be getting the tabpage to display the information,
or at least getting the tabpage to allow proper binding. No
exceptions are thrown up but there is no data.

I have tried setting the binding context of each page to the form. I
have tried making each page visiible before binding. And I have tried
showing each form before doing the actual binding.

All to no avail. What am I doing wrong?


Richard
 
K

Ken Tucker [MVP]

Hi,

I would have to see some code to find your problem. Here is a
simple example. I have a datagrid and tabcontrol on the form. I added 3
tabpages to the tabcontrol. On tabpage1 I added textbox1, on tabpage2 I
added textbox2, and tabpage3 I added textbox3.

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim strConn As String
Dim strSQL As String
Dim daEmployees As OleDbDataAdapter
Dim conn As OleDbConnection
Dim ds As New DataSet

strConn = "Provider = Microsoft.Jet.OLEDB.4.0;"
strConn &= "Data Source =C:\Northwind.mdb;"

conn = New OleDbConnection(strConn)

daEmployees = New OleDbDataAdapter("Select * From Products", conn)
daEmployees.Fill(ds, "Products")

DataGrid1.DataSource = ds.Tables("Products")


DataGrid1.DataSource = ds.Tables("Products")
ds.Tables("Products").DefaultView.AllowNew = False

TextBox1.DataBindings.Add("Text", ds.Tables("Products"),
"ProductName")
TextBox2.DataBindings.Add("Text", ds.Tables("Products"),
"UnitsInStock")
TextBox3.DataBindings.Add("Text", ds.Tables("Products"),
"UnitsOnOrder")
End Sub


Ken
------------------
Hi Ken,

I've spent another day on this but am really not getting very far.

My databinding is fine. If I move one of the text boxes to the main
form, the data displays as usual. But as soon as they are on the
tabpage, I am unable to get the tabpages to display any data
whatsoever from the dataset.

So the problem must be getting the tabpage to display the information,
or at least getting the tabpage to allow proper binding. No
exceptions are thrown up but there is no data.

I have tried setting the binding context of each page to the form. I
have tried making each page visiible before binding. And I have tried
showing each form before doing the actual binding.

All to no avail. What am I doing wrong?


Richard
 
R

Richard

Here is the code so far.
I have simplified it for readability.

Public Class frmDeals
Inherits System.Windows.Forms.Form
Protected Const SQL_CONNECTION_STRING As String = _
"Server=CONRAD;" & _
"Database=OTwoSourcing;" & _
"UID=sa;password=;"

Public strConnection As String = SQL_CONNECTION_STRING
Private cnnOTwo As SqlConnection
Private cmdAdvertising As SqlCommand, cmdCourseType As SqlCommand
-----------------------------------
Dim objConnection As SqlConnection = New SqlConnection(strConnection)

Dim objDataAdapter As SqlDataAdapter = New SqlDataAdapter( _
"SELECT * " & _
"FROM Deals " & _
"WHERE SellerID=" & SellerNo & _
" ORDER BY LeadReceivedDate", objConnection)

Dim objDataSet As DataSet
Dim objDataView As DataView
Dim objCurrencyManager As CurrencyManager
------------------------------------
objDataSet = New DataSet
objDataAdapter.Fill(objDataSet, "Deals")
objDataView = New DataView(objDataSet.Tables("Deals"))
objCurrencyManager = CType(Me.BindingContext(objDataView),
CurrencyManager)

' Set the binding context for each page to the form's binding
' context

For Each tp As TabPage In tabDeals.TabPages
tp.BindingContext = Me.BindingContext
Next
---------------------------------------
' Clear the databindings on each page

For Each tp As TabPage In tabDeals.TabPages

Select Case tp.Name
Case "tabProperty"
txtAddress.DataBindings.Clear()
Case "tabFinancial"
txtPrice.DataBindings.Clear()
End Select
Next tp

' Bind all textboxes on each page

For Each tp As TabPage In tabDeals.TabPages

Select Case tp.name

Case "tabProperty"
tp.Show() ' (also tried tp.visible=true)
txtAddress.DataBindings.Add("Text", objDataView,
"Address")

Case "tabFinancial"
tp.show() ' (also tried tp.visible=true)
txtPrice.Databindings.Add("Text",objDataView,"Price")
End Select
Next tp


The value for SellerNo is passed to this form from a calling form.


Richard
 
K

Ken Tucker [MVP]

Hi,

You dont need to set the binding context for the tabpage = to the
binding context for the form. It will automatically be the same.

Ken
-----------------------
Here is the code so far.
I have simplified it for readability.

Public Class frmDeals
Inherits System.Windows.Forms.Form
Protected Const SQL_CONNECTION_STRING As String = _
"Server=CONRAD;" & _
"Database=OTwoSourcing;" & _
"UID=sa;password=;"

Public strConnection As String = SQL_CONNECTION_STRING
Private cnnOTwo As SqlConnection
Private cmdAdvertising As SqlCommand, cmdCourseType As SqlCommand
-----------------------------------
Dim objConnection As SqlConnection = New SqlConnection(strConnection)

Dim objDataAdapter As SqlDataAdapter = New SqlDataAdapter( _
"SELECT * " & _
"FROM Deals " & _
"WHERE SellerID=" & SellerNo & _
" ORDER BY LeadReceivedDate", objConnection)

Dim objDataSet As DataSet
Dim objDataView As DataView
Dim objCurrencyManager As CurrencyManager
------------------------------------
objDataSet = New DataSet
objDataAdapter.Fill(objDataSet, "Deals")
objDataView = New DataView(objDataSet.Tables("Deals"))
objCurrencyManager = CType(Me.BindingContext(objDataView),
CurrencyManager)

' Set the binding context for each page to the form's binding
' context

For Each tp As TabPage In tabDeals.TabPages
tp.BindingContext = Me.BindingContext
Next
---------------------------------------
' Clear the databindings on each page

For Each tp As TabPage In tabDeals.TabPages

Select Case tp.Name
Case "tabProperty"
txtAddress.DataBindings.Clear()
Case "tabFinancial"
txtPrice.DataBindings.Clear()
End Select
Next tp

' Bind all textboxes on each page

For Each tp As TabPage In tabDeals.TabPages

Select Case tp.name

Case "tabProperty"
tp.Show() ' (also tried tp.visible=true)
txtAddress.DataBindings.Add("Text", objDataView,
"Address")

Case "tabFinancial"
tp.show() ' (also tried tp.visible=true)
txtPrice.Databindings.Add("Text",objDataView,"Price")
End Select
Next tp


The value for SellerNo is passed to this form from a calling form.


Richard
 
R

Richard

Okay,

Thank you for this Ken. I'm amost there. I have stripped away all
the coding that was referring to a specific tab page.

What was causing the problem was to do with how I opened the form
specifying the records that the sqldataadapter should load.

On opening the main (opening) form of my project I am creating
instances of all the forms.

On my Deals form (the form with the tabpages) I've created a public
property SellerNo which is set from the Seller form to display all
Deals by a particular seller when I actually open the Deals Form (the
class of which is already loaded at startup remember).

It seems though that because I had already loaded the form at startup,
the sqldataadapter had already loaded the valid records (i.e. where
the SellerID was null, rather than when SellerID=7) and was therefore
ignoring my new request to load it again.

I was calling the Deals form from this button on the Sellers Form

Dim cfrmDeals As New frmDeals
cfrmDeals.SellerNo = Convert.ToInt32(txtSellerID.Text)
cfrmDeals.Visible = True
cfrmDeals.Activate()

I am using a multi-form application, and one which I was creating new
instances of each form on loading my main form


i.e.

cfrmSwitchboard = Me
cfrmSellers = New frmSellers
cfrmDeals = New frmDeals

Hope this is clear.

Is there another way to do what I was looking to do?



Richard
 

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