Surely other developers of winforms apps faced this problem. Do you just
abandon the possibility of using design time binding and do all binding
in code? I have done that for one project. We did create dataset
instances in our forms simply for designtime uses so we could format
grid columns, etc..
Has anyone tried to go ahead and give each form its own dataset
instance, then use Merge()/GetChanges() at runtime anytime there is a
focus change so you can keep the 'master' dataset up to date and sync
changes among forms?
Please don't top-post.
If I understand your question you want to do something similar to
this: when your application starts, display a main form in which you
load, bind and display a data set. Then when a user presses a button,
display a second form and also bind its controls to the same dataset
as used in the main form.
This is no problem, and you can do most of it with the design time
tools. Here is one way of doing it. It's a bit long-winded, but it
shows what is happening. You can shortcut this by dragging and
dropping from the Data Sources window if you want. That creates
controls, dataset variables and binding sources automatically.
1. Design your dataset. I usually do this in its own assembly, to form
a Data-access layer.
2. On Form1, drag the dataset from the toolbox to the form. It will
appear at the bottom on the component tray. This will create a dataset
variable in your form. You can name it whatever you want.
3. I usually do all binding via BindingSources, so the next step is to
drag a BindingSource onto Form1 and rename it to something sensible.
In the properties window, edit its DataSource property. Under "Other
Data Sources -> Form1 List instances" you will see the data set. Pick
it. Then set the DataMember to whatever table you want. Configure
Filter and Sort properties as appropriate. Repeat for as many binding
sources as you want.
4. Add controls to the form and configure as appropriate. Again, this
can all be done in the properties window. For example, drag a text box
to the form. Under Data (Data Bindings), pick "Advanced". You can set
one or more bindings in here. In the binding combo you will want to
pick something from the BindingSource that you configured eg
"bindingSource1 - Name".
5. Repeat the design on your second form, but with one slight
modification: change the data set's modifier from Private to Public.
6. Now imagine we have a button click handler on Form1. We just need
to get Form2 to use the dataset we are managing on Form1:
using (Form2 f = new Form2()) {
f.theDataSet = this.theDataSet;
f.ShowDialog();
}
Alternatively, you can keep the variable private and create a Public
property on the form which allows you to set it.
The only bit of code you need to write by hand is something in Form1
to load data into the DataSet.
Get your dataset variables and binding sources set and the rest
follows pretty easily. I like to do it this way because it keeps the
binding code hidden in the Designer.cs, and lets me focus on my code.
BTW, one thing you will find with this approach is that when you are
on Form2 changes you make will be immediately reflected in Form1
unless you suspend binding. You will see this when you have the same
piece of data displayed in both forms simultaneously.