Question How to share dataset over multiple forms?

raviteja

Member
Joined
Oct 15, 2019
Messages
8
Programming Experience
Beginner
I have two datatables in main form and also two datatables in child form.so,to handle both the form datatables i want to use a dataset over multipe forms.how it is possible
 
Yes, it is possible. Pass it around using dependency injection via a constructor parameter, or a public property.
 
A DataSet is an object like any other, so you pass it around between forms like you would any other object. A form is an object like any other too, so you pass data between forms in the same way as you do between any other two objects.

If you have created a typed DataSet in your project using the Data Source Wizard then it may be that you are adding a DataSet to your forms in the designer. If you want both forms to use the same DataSet then you can't do that for both. You can do it for one if you like, or neither.

I suggest that you take a look at my three-part blog post on the subject of sharing data among forms here. You may want to skip the first part as it is more relevant for VB than C#. You should definitely read part 2 but the option it covers is the generally not the best. The third part demonstrates a more robust methodology. From memory, it uses properties to pass data in. As suggested by Skydiver, you can also use a constructor with one or more parameters, which is a better option for required data.
 
Last edited:
LOL! I'm actually Sheepings evil twin, but yes Sheepings would have given the same advice, but in a less terse way and more explanatory form.
 
LOL! I'm actually Sheepings evil twin, but yes Sheepings would have given the same advice, but in a less terse way and more explanatory form.
My apologies. Correction made. I'm so cool and hip that I generally just call people by the first letter in their name so if you both have names that begin with S then you're the same person to me. ;)
 
No offense taken. Sheepings is currently very ill with the flu, so he may not be online as much as he usually is. I hope he feels better soon.
 
LOL! I'm actually Sheepings evil twin
This is the first thing I thought of upon reading that line. :LOL:
81fUtuEaU4L._RI_.jpg
No offense taken. Sheepings is currently very ill with the flu, so he may not be online as much as he usually is. I hope he feels better soon.
Thank you Skydiver. It might be a while longer before I will be back posting at my usual rhythm. It is more than a flu, and I know that because it nearly killed me. But with the help of a lot of different medication, I am amending slowly. Hope to be back in full swing soon!

OP :
dependency injection via a constructor
Would be the recommended way to go here!
 

Latest posts

Back
Top Bottom