Accessing a dataset from an object on a different form

  • Thread starter Thread starter Mike N.
  • Start date Start date
M

Mike N.

Using VB.Net and ADO:

I've created a public variable on my main form and set it to = a dataset
which was crrated with the designer. I have another form with a datagrid on
it. In the designer, I can't set the datasource property to the dataset on
the main form, although i can do it in code.

Can someone tell me how to declare a dataset, either in a form or module,
that I can use globally throughout my application, including in the
designer? I have a hierarchical dataset and need to use a hierarchical grid
control, setting all of it's various properties and structure at run time is
problematic.

Thanks!
 
Hi Mike,

When you create a dataset in the designer, you will find the dataset.vb
class file in your solution explorer, when you do "show all files" in the
tree beneath the XSD.

You can give the reference from an object to another class, if you create
the right methods for that in those other classes (Do not try to change that
XSD vb class, when you do that you can never more use the designer for that
dataset).

I hope this helps?

Cor
 
Hmm, color me dense:

I can create a dataset class using the solution explorer, and I can create a
global instance of it on my main form. I can then use that instance as a
datasouce in a table in code.

What I can't do is to use that global dataset in the designer as the
datasouce. I guess the datasouce property doesn't show datasouces unless
they are bound to forms somehow at design time. That's what I'm trying to
accomplish.

It sounds like you are saying this can be done in the forms class definition
somehow, but I don't know exactly how to do this.

Thanks for replying.

Mike
 
Hi Mike,

First of all I never do it this way, I made a sample to try it myself
first.
It did what I though it would do.

But I did test nothing further.
So I don't know if there are side effects.

The sample is bellow

I made a project with 2 forms
I made on form1 a connection draging the SQLadapter on form1 and connected
it the Nortwind table employees
On every form I did drag a datagrid and on the first one a button.
I did using the designer to change the datasource of the datagrid on form1
to dataset11.employees
Than I added the code bellow

The form did show up with the information on form1 in the datagrid
When I did push on button1 the datagrid with the information was showed on
form2 in the datagrid.

I hope this helps?

Cor

\\\
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Private Sub Button1_Click(ByVal sender As _
System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim frm As New Form2
frm.DataSet11 = DataSet11
frm.ShowDialog()
End Sub
Private Sub Form1_Load(ByVal sender As _
Object, ByVal e As System.EventArgs) Handles MyBase.Load
SqlDataAdapter1.Fill(DataSet11)
End Sub
End Class
///
\\\
Public Class Form2
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
#End Region
Friend WithEvents DataSet11 As WindowsApplication8.DataSet1
Private Sub Form2_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Me.DataGrid1.DataSource = DataSet11.Employees
End Sub
End Class
///
 
Back
Top