Sharing datasources among forms - VS2005


S

Steve Marshall

I have come across the situation several times where a number of forms
in an app use the same datasource, for example to populate a combo
drop-down. I'm sure we've all had this. Using the IDE results in
every form getting its own Dataset, BindingSource and TableAdapter,
plus code in the Open event to fill the table in the dataset. This
strikes me as incredibly inefficient - there is simply no need for
every form to go to the database to read this data in again. What I
want is a way to set up and populate a dataset once, then just refer
to it in all the forms that might need it. Is there a way to do this
that fits in with the IDE? If there is it isn't obvious to me.
 
Ad

Advertisements

C

Cor Ligthert [MVP]

Steve,

It is strange, because the version 2003 has this behaviour. The version 2005
creates it on its own datasource where the tableadapter can be build in. It
is a class on its own including data and connection to the database.

Can you tell us how you build up your tableadapter?

Cor
 
R

RobinS

If you were really concerned about the performance, you could create a
public class with shared properties that point to datatables that you're
using to bind to comboboxes all over your app, and access that everywhere.

Robin S.
 
S

Steve Marshall

Yes, an approach like that has occurred to me, and I will pursue it.
It just doesn't seem to fit as naturally as I would like with the way
the IDE does things. Thanks for the reply.
 
Ad

Advertisements

B

Bart Mermuys

Hi,

Steve Marshall said:
Yes, an approach like that has occurred to me, and I will pursue it.
It just doesn't seem to fit as naturally as I would like with the way
the IDE does things. Thanks for the reply.

<text snipped>

Here is some addition info:

After compiling the class (example) below, you can create an object Data
Source from it (Data Source's window, add new Data Source). Once you have
the Data Source you can use it with the WinForm designer and the DataTable
will only be loaded once when it's first used.

Imports WindowsApplication1.SomeDBDataSetTableAdapters

Public Class GlobalCompanyDataSource
Implements System.ComponentModel.IListSource

Public Shared _Table As SomeDBDataSet.CompanyDataTable

Public Shared ReadOnly Property Table() As
SomeDBDataSet.CompanyDataTable
Get
If (_Table Is Nothing) Then
_Table = New SomeDBDataSet.CompanyDataTable
Dim cta As New CompanyTableAdapter
cta.Fill(_Table)
cta.Dispose()
End If
Return _Table
End Get
End Property

Public ReadOnly Property ContainsListCollection() As Boolean Implements
System.ComponentModel.IListSource.ContainsListCollection
Get
Return False
End Get
End Property

Public Function GetList() As System.Collections.IList Implements
System.ComponentModel.IListSource.GetList
Return Table.DefaultView
End Function
End Class

If your form needs to contain global and a non global DataTable's which are
part of the same DataSet, then you could just use the DataSet on the Form
and then replace the fill line in Form_Load for the global DataTable, this
way you can still enforce referential integrity (fk constraints).

Private Sub Form_Load(...)
' fill lookup (global)
SomeDBDataSet.Company.Merge(CompanyGlobalSource.Table)

' fill master (non-global)
EmployeeTableAdapter.Fill(SomeDBDataSet.Employee)
End Sub


HTH,
Greetings
 

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