Click to See Complete Forum and Search --> : Load a datagrid from another form


enigmaos
August 5th, 2002, 03:53 PM
Does anybody know how to load a datagrid on FORM1 from FORM2

I have two forms - FORM1 and FORM2.

FORM1 has a datagrid (DATAGRID1) and a button (BUTTON1).

FORM2 has a button (BUTTON1) and 4 textboxes (TEXTBOX1, TEXTBOX2, TEXTBOX3 and TEXTBOX4).

FORM1 is for displaying the information entered in FORM2.

On FORM2, when all four textboxes are filled, then the button will be clicked to save the data and then call LOADDATAGRID sub in FORM1 to load/refersh DATAGRID1.

I'm stuck. :( Everything works fine until the LOADDATAGRID is called. Nothing is loaded into the grid even though there are some data in the database.

Anybody??

Thanks!!!!

DSJ
August 5th, 2002, 05:24 PM
So when you step thru in the debugger what happens?

enigmaos
August 6th, 2002, 08:08 AM
when using debugger, from FORM2, by clicking the button, it calls LOADDATAGRID sub in FORM1. It steps throught the code just fine. Dataadapter, dataset, and datatable are all set and no error. Then it steps through DATAGRID1.DataSource with no error, but nothing is in the datagrid:

DATAGRID1.DataSource = myDataSet.Tables(myTable)

:confused:

Thanks!

DSJ
August 6th, 2002, 09:06 AM
I'm not too familiar with the data connection/control classes yet, but I would suspect that somewhere along the line you will need to execute some kind of "Select" to tell it what records to retrieve...

enigmaos
August 6th, 2002, 10:12 AM
Yes, there's a select statement, and it works fine.

I have created another button on FORM1 just to test the connection and the datagrid, and when I click that button, the datagrid is filled with data. So, it appears to be when FORM2 calls the sub to fill the datagrid, it doesn't fill it.

:(

Akim
August 6th, 2002, 01:51 PM
What if you try to use:
DATAGRID1.SetDataBinding (myDataSet, "TableName")
?

enigmaos
August 6th, 2002, 03:30 PM
I tried, and it didn't display the data in the grid at all.


Public Function DisplayData(ByVal grdThis As DataGrid)
Dim Service As OrderDeskCLib.OrderDesk.ServiceDB = New OrderDeskCLib.OrderDesk.ServiceDB()
Dim tmpTable As String = "tmpServices"
Dim myDataSet As DataSet

myDataSet = Service.GetAllTmpServices("12345", tmpTable)
'grdThis.DataSource = myDataSet.Tables(tmpTable)
grdThis.SetDataBinding(myDataSet, tmpTable)
End Function

:(

Akim
August 6th, 2002, 04:00 PM
Try to declare your function "Public Shared" and grid as well.

enigmaos
August 6th, 2002, 04:54 PM
the function is in a module. If I change the function to Public Shared Function, then it would error out - "Methods in a Module cannot be declared 'Shared'."

LOADDATAGRID is a pulic sub in FORM1.

:(

Akim
August 6th, 2002, 05:05 PM
Ok.
DisplayData is in a module.
LOADDATAGRID is a pulic sub in FORM1.

You need to call LOADDATAGRID from FORM2?

Post your LOADDATAGRID function as well.

enigmaos
August 7th, 2002, 08:19 AM
LOADDATAGRID is identical to DisplayData except it's in FORM1 instead of MODULE1

Public Sub LOADDATAGRID()
Dim Service As OrderDeskCLib.OrderDesk.ServiceDB = New OrderDeskCLib.OrderDesk.ServiceDB()
Dim tmpTable As String = "tmpServices"
Dim myDataSet As DataSet

myDataSet = Service.GetAllTmpServices("12345", tmpTable)
Me.DataGrid1.DataSource = myDataSet.Tables(tmpTable)
Me.DataGrid1.ResetText()
End Sub

Akim
August 7th, 2002, 08:39 AM
Check the attachment.

Edit: That wasn't a best example!
Check a new one further.

Akim
August 7th, 2002, 08:42 AM
Just change the connection string and Query in the Form1 and in the Module.

enigmaos
August 7th, 2002, 08:53 AM
thanks! I'm trying your code now. I will you know. :)

Akim
August 7th, 2002, 08:55 AM
Ok. good luck.

enigmaos
August 7th, 2002, 09:22 AM
I don't see DataGrid1 on FORM1. Is it invisible?

Akim
August 7th, 2002, 09:32 AM
You don't see it, because it's declared as Public Shared.
You'll see the grid when you run app.

Akim
August 7th, 2002, 09:37 AM
I don't know your requirements, but, honestly, I'd do that differently. It never was a good practice to use controls from one form in another.

Either you can combine both your forms in one or load Form1 after Form2 is unloaded (just call LOADGRID in form load/activate even)

Just my .02$

enigmaos
August 7th, 2002, 09:40 AM
Received this error message: "Object reference not set to an instance of an object." at the line below in LOADGRID and DisplayData:

grdThis.SetDataBinding(dsF1, "Jobs")

any idea? :confused:

Akim
August 7th, 2002, 09:46 AM
Tell me what did you change in the original code (post your code here)

enigmaos
August 7th, 2002, 09:54 AM
I changed the connection type to SQLDataAdapter, the connection string and the select statement in both LOADGRID and DisplayData. Everything else is the same.

FORM1

Public Shared Function LOADGRID(ByVal grdThis As DataGrid)
Dim sSQLF1 As String = "SELECT * FROM tblMaster"
Dim dsF1 As DataSet
Dim daF1 As SqlDataAdapter
Dim sConnF1 As String = "SERVER=WNTS01;DATABASE=NODsql;UID=sa;PWD=;"
Dim cnF1 As SqlConnection = New SqlConnection(sConnF1)
dsF1 = New DataSet()
daF1 = New SqlDataAdapter(sSQLF1, cnF1)
daF1.Fill(dsF1, "Jobs")
grdThis.SetDataBinding(dsF1, "Jobs")
End Function

MODULE1

Public ds As DataSet
Public da As SqlDataAdapter
Public sConn As String = "SERVER=WNTS01;DATABASE=NODsql;UID=sa;PWD=;"
'Change connection string to your DB
Public cn As SqlConnection = New SqlConnection(sConn)

Public Function DisplayData(ByVal grdThis As DataGrid)
Dim sSQL As String = "SELECT * FROM tblMaster"
'Change SELECT for your SELECT
ds = New DataSet()
da = New SqlDataAdapter(sSQL, cn)
da.Fill(ds, "Jobs")
grdThis.SetDataBinding(ds, "Jobs")
End Function

Akim
August 7th, 2002, 10:28 AM
Honestly, I don't know why it doesn't work on your comp, because it works perfect on mine.
Do you have .NET SP1 installed?

Try to play with it a little. Remove the line that gives an error. Don't pass your Grid into the function, use direct refference instead...

Public Shared Function LOADGRID()
Dim sSQLF1 As String = "SELECT * FROM tblMaster"
Dim dsF1 As DataSet
Dim daF1 As SqlDataAdapter
Dim sConnF1 As String = "SERVER=WNTS01;DATABASE=NODsql;UID=sa;PWD=;"
Dim cnF1 As SqlConnection = New SqlConnection(sConnF1)
dsF1 = New DataSet()
daF1 = New SqlDataAdapter(sSQLF1, cnF1)
daF1.Fill(dsF1, "Jobs")
DataGrid1.SetDataBinding(dsF1, "Jobs")
End Function

YOu got the idea.

enigmaos
August 7th, 2002, 10:38 AM
thanks for your help! :)

By the way, where is the grid declared in Form1? I did a search for it and found none.

How and where is it declared as a public shared in the app and not found in code?

Akim
August 7th, 2002, 10:44 AM
Here:

#Region " Windows Form Designer generated code "

Public Sub New()
MyBase.New()

'This call is required by the Windows Form Designer.
InitializeComponent()

'Add any initialization after the InitializeComponent() call

End Sub

'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub

'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer

'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Public Shared WithEvents DataGrid1 As System.Windows.Forms.DataGrid
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.DataGrid1 = New System.Windows.Forms.DataGrid()
Me.Button1 = New System.Windows.Forms.Button()
Me.Button2 = New System.Windows.Forms.Button()
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'DataGrid1
'
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point(24, 8)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(616, 288)
Me.DataGrid1.TabIndex = 0
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(24, 312)
Me.Button1.Name = "Button1"
Me.Button1.TabIndex = 1
Me.Button1.Text = "Button1"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(128, 312)
Me.Button2.Name = "Button2"
Me.Button2.TabIndex = 2
Me.Button2.Text = "Button2"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(680, 373)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button2, Me.Button1, Me.DataGrid1})
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)

End Sub

#End Region


As I've stated before, it's not the best method of doing that :)

enigmaos
August 7th, 2002, 11:13 AM
aaaaa, I see. My app didn't have Me.DataGrid1 = New System.Windows.Forms.DataGrid(). It only had Public Shared WithEvents DataGrid1 As System.Windows.Forms.DataGrid

***After I added that line, it ran without getting error. BUT, the datagrid IS NOT showing on FORM1 for some reason.



'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Public Shared WithEvents DataGrid1 As System.Windows.Forms.DataGrid
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.DataGrid1 = New System.Windows.Forms.DataGrid()
Me.Button1 = New System.Windows.Forms.Button()
Me.Button2 = New System.Windows.Forms.Button()

Akim
August 7th, 2002, 11:20 AM
Try the following:
1. Change "Public Shared WithEvents DataGrid1 As System.Windows.Forms.DataGrid" to "Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid"
2. Delete Datagrid and place a new one.
3. Cahnge "Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid" to "Public Shared WithEvents DataGrid1 As System.Windows.Forms.DataGrid"

enigmaos
August 7th, 2002, 11:38 AM
It's working!! :cool:

I had to remove DataGrid1, added a new one, and changed it to "PUBLIC SHARED WITHEVENTS"

THANKS! :)

Akim
August 7th, 2002, 11:55 AM
Welcome.

nolc
August 21st, 2002, 03:29 PM
Hey guys,
I have done the same thing to two different DataGrids in an app of mine, BUT during development and runtime....the code for the DataGrids disappears in the "Windows Form Designer generated code" Region and I have to keep on re-adding the code. Is there a better way of making this functionality work? BECAUSE I'm finding myself wasting too much time re-adding the DataGrids and code.

thanks guys

Akim
August 21st, 2002, 04:40 PM
nolc,

Thanks for returning my attention to this topic.
I want to apologize for the "bad" code that I have previously posted! I should've seen that there has to be a better way!
I have an excuse - I haven't open VB (both, 6.0 and .NET) for a couple of months.

So, I'm attaching a new, revised version of my previous app.
Again, that might be not the most efficient way of doing that, but it has few advantages agains previos:
1. You don't have to change "Windows Form Designer generated code"
2. It works :)

nolc
August 21st, 2002, 04:59 PM
SWEET...THANK YOU! I will try it out and get back to you tomorrow. Thanks again.

nolc
August 21st, 2002, 05:27 PM
Hey Akim,
I have a question....is it good practice to have the project load at Sub Main? I noticed when I commented out the code below and changed the Startup Object to Form1 my DataGrid wouldn't fill. Maybe I should load at Sub main?


Sub main()
frmForm1.ShowDialog()
End Sub

Would this clear up my other problem I am having?....PROBLEM: I have a Main MDIParent form which can call it's 2 children forms(Form1 and Form2)...but when Form1 calls another form like Form2 in your project....Form2 is outside the MDIParent form. How can I keep all forms inside the MDIparent?

FORM FAMILY TREE:
frmMAIN (MDIParent)
***frmChild1
- -Can call FORM1(Form with Datagrid, Load Form2 button, Fill button)
- -Can call FORM2(Fill Form1.Datagrid button)

***frmChild2

Akim
August 21st, 2002, 05:50 PM
nolc,

I haven't worked with MDI much, but in your case you can consider your MDI parent as a Sub main (set it as startup form).
Just declare Form1 as public.

P.S. Honestly, I've never worked with MDI in my entire programming life. I don't even know how it works - well, I know how it works, but have no idea how to set everything up to work right (yet).
*note to self: re-write something using MDI ASAP :rolleyes:

nolc
August 22nd, 2002, 09:04 AM
Thanks Akim...