|
-
Oct 30th, 2003, 07:14 AM
#1
Thread Starter
Lively Member
Inheritance problem??
Hi again,
I'm having a problem with inheritance (i suppose). I have a form (frmsearch) in which the user can type some keywords and based on the input I create an SQL-string for an oledbdataadapter. With this dataadapter I fill a dataset.
Everything works fine, but now I want to display the results (the dataset) in a datagrid in another form (frmShow). I don't know how to do this. I tried to inherit the frmsearch in frmShow but this shows also all the controls in frmShow and this is not what I want (it is also not possible to delete them).
Should I use another strategy? If so, can you help me with some VB.NET code to accomplish this?
Many thanks
Tom
-
Oct 30th, 2003, 07:44 AM
#2
Fanatic Member
You could just pass the dataset (ByRef) to the new form for display. You could add a parameter to the new constructor of the new for to take the dataset.
-
Oct 30th, 2003, 07:49 AM
#3
Thread Starter
Lively Member
Thanks crazycoder,
Any suggestion for the corresponding VB.NET code?? I'm new to VB.NET programming
Greetz
tom
-
Oct 30th, 2003, 07:59 AM
#4
Fanatic Member
In your new form you would have:
VB Code:
Dim dsMyDataset As Dataset
Public Sub New(ByRef dsDataset As DataSet)
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
dsMyDataset = dsDataset
End Sub
Then you can use the dsMyDataset in your form whereever you need it.
-
Oct 30th, 2003, 08:16 AM
#5
Thread Starter
Lively Member
Thanks,
But I still don't get it. I hope I'm not asking too stupid questions.
If I have to write this code in the new form, where is the dataset called? Don't I need a 'property'?
Thanks,
Tom
-
Oct 30th, 2003, 08:23 AM
#6
Fanatic Member
You stated that you are populating your dataset (using an adapter) in your first form. So if you have your dataset already, then you can just pass it to the next form and use it. Alternatively, you could populate the dataset in the second form, but then you would have to pass all your parameters that the user selected into the second form.
-
Oct 30th, 2003, 08:37 AM
#7
Thread Starter
Lively Member
VBCrazycoder,
You are correct, I stated that I created the dataset (dsSearch) in the first form and I have to pass it to the second (or called form).
But what I don't understand is that by putting the following code
--------------------------------------------------------------------------------
Dim dsMyDataset As Dataset
Public Sub New(ByRef dsDataset As DataSet)
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
dsMyDataset = dsDataset
End Sub
In the called form, you can pass the dataset from the first form to the second form. Does it have something todo with 'initialize'?
I'm stuck.
Tom
-
Oct 30th, 2003, 08:44 AM
#8
Fanatic Member
All you are doing is passing a reference of the dataset to the second form, and storing it in a local variable so you can use it in code later on, like in the load event.
This is the same as passing a variable to a method.
VB Code:
Public Sub SomeMethod(ByVal myValue As Integer)
....
End Sub
-
Oct 30th, 2003, 09:16 AM
#9
Thread Starter
Lively Member
I have tried it, but it still doesn't work.
The dataset in the first form is called datasetsearch.
The code for the second form is:
Public Class FormWeergaveZoek
Inherits Windows.Forms.Form
Dim dsMyDataset As DataSet
Public Sub New(ByRef Datasetzoeken11 As DataSet)
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
dsMyDataset = Datasetzoeken11
End Sub
Private Sub FormWeergaveZoek_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.DataGrid1.DataMember = "tblCD"
Me.DataGrid1.DataSource = Me.dsMyDataset
End Sub
End Class
The datagrid in the second form is not filled up with the required dataset?
Do you know why?
Tnaks
TOm
-
Oct 30th, 2003, 09:27 AM
#10
Fanatic Member
make sure you are populating the dataset in the first form. You can verify that you have data by doing a dsMyDataset.Tables(0).Rows.Count() - (i think that is the right code - and I think you can substitute the 0 for the table name if needed).
Also, I usually do:
VB Code:
Me.DataGrid1.DataSource = Me.dsMyDataset.Tables(0)
and not set the datamember explicitly.
Also, I am assuming that the first form is still open when you call the second form. If you are closing the first form then you may need to pass the dataset by value.
Try these suggestions...
-
Oct 30th, 2003, 09:46 AM
#11
Thread Starter
Lively Member
thanks VBcrazycoder,
I tried your suggestions:
-I checked if the dataset is filled in the first form: it is because i have integrated a datagrid based on the dataset in the first form and this datagrid gets filled.
-The first form is still open when the second one is displayed
-I adapted the code to fill the datagrid as you suggested
But when I compile and run the program I get the following error when the second form opens:
"An unhandled exception of type 'System.NullReferenceException' occurred in Zoekprogramma.exe Additional information: Object reference not set to an instance of an object."
Can you help me please?
Thanks, i appreciate your help
-
Oct 30th, 2003, 10:50 AM
#12
IN the first form this is how you should be passing the dataset to the second:
VB Code:
'this would be in the first form where it opens the 2nd
'this is where it actually gets passed in the constructor or 'New' statement
Dim frm As New FormWeergaveZoek(datasetsearch)
frm.Show()'or ShowDialog
-
Oct 30th, 2003, 11:10 AM
#13
Thread Starter
Lively Member
Thanks Edneis
Your suggestion works, I just have to adapt the datagrid in the second form a little bit because it doesn't directly shows the information (the user still has to click on the +sign).
Thank you very much
-
Oct 30th, 2003, 11:35 AM
#14
To solve the 'have to click the +' problem then just use one of the tables in the dataset as the datasource instead of the dataset itself.
DataGrid1.DataSource=datasetSearch.Tables(0)
-
Oct 30th, 2003, 11:57 AM
#15
Fanatic Member
Or set the AllowNavigation property of the grid to false - but you still have to set the datasource to the specific table.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|