Results 1 to 12 of 12

Thread: [RESOLVED] How to get a second form to use DataSet in a parent form

  1. #1

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,104

    Resolved [RESOLVED] How to get a second form to use DataSet in a parent form

    I created a strongly typed ADO DataSet using the designer. I added a DataGridView to my main form and the wizard popped up and created the DataSet and binding objects for me in the bottom of the form designer. I wrote some code to allow the user to select some rows using a Boolean column. Other procedures in the main form will reference these selections and other data in the DataSet. Now I want to move that DataGridView to a selection form. If I do like I did in the main form it creates new DataSet objects in that form. But I want it to bind to the extant DataSet in the main form while giving me all the features like column definitions that the strong data typing gives me. Understand that this selection form will change data in a DataTable and I need those changes available in the main form when the user closes the selection form. How do I do that?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: How to get a second form to use DataSet in a parent form

    You can still add the grid and the BindingSource in the designer. You just can't add the DataSet and table adapter(s). You would pass the DataSet from the first form to the second in exactly the same way as you would any other data, i.e. using a parameter in a constructor or other method or using a property. When the second form receives the DataSet, it binds it to the BindingSource and binds that to the grid.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: How to get a second form to use DataSet in a parent form

    Note that you could even drag the table from the Data Sources window to generate everything and then just delete the DataSet and table adapter. That way, all the columns will be created in the grid automatically too.

  4. #4

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,104

    Re: How to get a second form to use DataSet in a parent form

    Great idea. I can't wait to try it. Thank you.

  5. #5

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,104

    Re: How to get a second form to use DataSet in a parent form

    Thanks for your help. I finally got back to this and have been working on it for about an hour but I can't get it to work. I created a New() and passed the DataSet through but it still isn't working. I must be doing something stupid. I'll make a simple project and try it there. Then if if still doesn't work I can post some simplified code.

  6. #6

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,104

    Re: How to get a second form to use DataSet in a parent form

    I tried to follow your instructions to the best of my understanding but I still have the same problem. The first issue I notice is if in the form designer I delete the DataSet and adapter I'm left with a binding source, table adapter manager and a binding navigator. But now all my column definitions disappear. So I think I'm getting something wrong there.

  7. #7

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,104

    Re: How to get a second form to use DataSet in a parent form

    Using mostly default VS names I created Form1, Form2, and DataSet1. I added a table "Officers" from my SQL Server to DataSet1. Using the ToolBox I dragged over a DataGridView. In the wizard I picked the Officers table as the source. It created object DataSet1, OfficersBindingSource, and OfficersTableAdapter. I set the Modifiers attribute to public. I added a Button1 to Form1. Filled the table in Form1 load event and verified data. IN the button click I instantiate a object by Form2 and set the DataSet therein to the DataSet in Form1. It doesn't work. I'm sure I'm doing something stupid. Any ideas?
    vb.net Code:
    1. Private Sub Button1_Click() Handles Button1.Click
    2.     Dim objForm2 As New Form2
    3.     objForm2.DataSet1 = objDataSet1
    4.     objForm2.ShowDialog()
    5. End Sub
    6. Private Sub Form1_Load() Handles MyBase.Load
    7.     Dim objadapter As New DataSet1TableAdapters.OfficersTableAdapter
    8.     objadapter.Fill(objDataSet1.Officers)
    9. End Sub

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: How to get a second form to use DataSet in a parent form

    If you want to build it al manually from scratch then you need to provide all that the automation does. That would mean adding a DataGridView and a BindingSource in the designer at least and, if you want one, a BindingNavigator. If you do add a BindingNavigator then assign your BindingSource to its BindingSource property. In code, you'd need to take a DataSet via a property, constructor or other method and bind it to the BindingSource and bind that to the grid. When you bind the grid, columns will be added automatically. If you want something other than the default column configuration then you can add the columns and configure them as you want in the designer, making sure to set the DataPropertyName property of each to the name of the DataColumn you want to bind it to. You might also need to declare a DataSet field if you need to be able to refer to the DataSet in code.

    If you drag a table from the Data Sources window to set everything up automatically then you will need to delete the DataSet and table adapter afterwards. You'll also want to delete the table adapter manager and, if you don't want it, the BindingNavigator. I've never done that but, if it removes all the grid columns, you can do as above to re-add them, i.e. let them be created when you bind or add them in the designer. Again, you'll need to take DataSet in via a property, constructor or method and bind it.

    Your constructor in the dialogue form might look something like this:
    vb.net Code:
    1. Public Sub New(data As DataSet1)
    2.     BindingSource1.DataSource = data.Table1
    3.     DataGridView1.DataSource = BindingSource1
    4. End Sub
    If you use a property then that would look similar:
    vb.net Code:
    1. Private _data As DataSet1
    2.  
    3. Public Property Data As DataSet1
    4.     Get
    5.         Return _data
    6.     End Get
    7.     Set
    8.         _data = value
    9.         BindingSource1.DataSource = _data.Table1
    10.         DataGridView1.DataSource = BindingSource1
    11.     End Set
    12. End Property

  9. #9

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,104

    Re: How to get a second form to use DataSet in a parent form

    Thank you! I rely on the wizards to much and don't realize what they do. With yoru instructions I'll learn to change the tire myself instead of calling the auto club.

  10. #10

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,104

    Re: How to get a second form to use DataSet in a parent form

    I'm trying this out now. I created the two forms and a DataSet. I dropped a binding source and a DataGridView in Form2.
    Warning 1 'Public Sub New(data As DataSet1)' in designer-generated type 'WindowsApplication1.Form2' should call InitializeComponent method.
    So I added "InitializeComponent()", I have no idea why, and it seems to satisfy AutoSense. I followed your passage of the DataSet by instantiation and it works perfectly. Thank you very much. Now I get to try this on the real deal.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: How to get a second form to use DataSet in a parent form

    Quote Originally Posted by cory_jackson View Post
    I'm trying this out now. I created the two forms and a DataSet. I dropped a binding source and a DataGridView in Form2.
    So I added "InitializeComponent()", I have no idea why, and it seems to satisfy AutoSense. I followed your passage of the DataSet by instantiation and it works perfectly. Thank you very much. Now I get to try this on the real deal.
    Sorry, I omitted that part without thinking because I typed that code directly into the forum. If you write a parameterless constructor in the IDE yourself then it should add that call automatically, because it will delete the default parameterless constructor that is added to the designer code file automatically. If you write a constructor with parameters then you need to either add that call yourself or else call the parameterless constructor yourself. If you don't want a parameterless constructor at all, so that the form can only be created by providing arguments, then you should write a parameterless constructor first, so the IDE will delete the default one, and then add the parameter.

    As for InitializeComponent, it's the method that contains all the code generated by the designer. For instance, if you set the Text property of a TextBox in the designer, a line is added to set that property in the InitializeComponent method. If you don't call that method then your form controls won't be configured.

    I suggest that you check out a designer code file to see what's in there. Select your project in the Solution Explorer and click the Show All Files button. You can then expand the node for your form and open the designer code file.

  12. #12

    Thread Starter
    Frenzied Member cory_jackson's Avatar
    Join Date
    Dec 2011
    Location
    Fallbrook, California
    Posts
    1,104

    Re: [RESOLVED] How to get a second form to use DataSet in a parent form

    I finally have time to review what you wrote in detail as I walk though the steps out outlined and you have solved many longstanding mysteries for me. I knew much of the things I do in the designer have to be auto-generating code someplace. I knew this was there from some mention before so I am happy to come back to it. But, of course, I have questions.

    I created a blank form and looked at the designer code as well. It has InitializeComponent and Dispose methods. You and others like the DotNetPerls site say that this method is called by the form constructor. You also talk about how it will delete the constructor depending on how I create my own constructor. It all makes sense except I can't find this form constructor method anywhere. I want to see it get deleted. Where is it? I don't find any "New" methods. And I searched for InitializeComponent elsewhere but cant' find it. Where is it?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width