Problem graphically binding to an externally referenced DataSet inWinForms Designer

L

Lorenz Kahl

Hi,

unfortunately I couldn't make up a subject, that describes the problem
better. Here's what I tried to do:

I'm writing a WinForms application that consists of three layers
(assemblies / projects), the WinForms-UI / the business logic layer
(BLL) / the data access layer (DAL).
The DAL defines a typed DataSet 'MemberDataSet'. It also has a class
'MemberDALC' with a method 'GetAllMembers()' which returns a properly
filled instance of the MemberDataSet.
Next I would like to use the VS.NET designer surface to bind my controls
to this DataSet graphically(!). So I pulled a DataSet-component on the
surface and chose the externally referenced DataSet definition
DAL.MemberDataSet. Then I can bind everything as usual.
So far everything works just fine.

NOW THE PROBLEM:

The generated code of the designer instantiates a new (empty) instance
of the DataSet using the default constructor like this:

InitializeComponent()
{
this.memberDataSet1 = new DAL.MemberDataSet();
....
}

Well, that's fine - but as I said it's empty. I would like to have a
filled DataSet, when the form loads. So I did this:

private void FrmTest_Load(object sender, System.EventArgs e)
{
this.memberDataSet1 = (new MemberDALC()).GetAllMembers();
....
}

This shows no effect and all bound controls will still be shown empty,
although the memberDataSet1 actually holds the data after this function
call.

I guess my problem is that I'm lacking real experience with
WinForms-Databinding and I really just have to call an additional
'refresh', 'update' or 'redraw'-function. But I couldn't find anything.

I found two workarounds though. One is to replace the generated code in
the InitializeComponent-method to call the GetAllMembers method directly
instead of the DataSet-constructor. But I don't like messing with
auto-generated code plus VS.NET gives me several warnings afterwards.
The second way I figured out is to call Merge() on the empty DataSet
like so:

private void FrmTest_Load(object sender, System.EventArgs e) {
this.memberDataSet1.Merge((new MemberDALC()).GetAllMembers());
}

That works but looks kind of awkward to me. Any other suggestions will
be greatly appreciated. Sorry for this long post ;-)

Best regards,
Lorenz
 
M

Miha Markic [MVP C#]

Hi Lorenz,

Lorenz Kahl said:
Hi,

unfortunately I couldn't make up a subject, that describes the problem
better. Here's what I tried to do:

I'm writing a WinForms application that consists of three layers
(assemblies / projects), the WinForms-UI / the business logic layer (BLL)
/ the data access layer (DAL).
The DAL defines a typed DataSet 'MemberDataSet'. It also has a class
'MemberDALC' with a method 'GetAllMembers()' which returns a properly
filled instance of the MemberDataSet.
Next I would like to use the VS.NET designer surface to bind my controls
to this DataSet graphically(!). So I pulled a DataSet-component on the
surface and chose the externally referenced DataSet definition
DAL.MemberDataSet. Then I can bind everything as usual.
So far everything works just fine.

NOW THE PROBLEM:

The generated code of the designer instantiates a new (empty) instance of
the DataSet using the default constructor like this:

InitializeComponent()
{
this.memberDataSet1 = new DAL.MemberDataSet();
...
}

Well, that's fine - but as I said it's empty. I would like to have a
filled DataSet, when the form loads. So I did this:

private void FrmTest_Load(object sender, System.EventArgs e)
{
this.memberDataSet1 = (new MemberDALC()).GetAllMembers();
...
}

This shows no effect and all bound controls will still be shown empty,
although the memberDataSet1 actually holds the data after this function
call.

Sure, you bound the original instance, while you've substitued it with new
one.
I guess my problem is that I'm lacking real experience with
WinForms-Databinding and I really just have to call an additional
'refresh', 'update' or 'redraw'-function. But I couldn't find anything.

No, see above.
I found two workarounds though. One is to replace the generated code in
the InitializeComponent-method to call the GetAllMembers method directly
instead of the DataSet-constructor. But I don't like messing with
auto-generated code plus VS.NET gives me several warnings afterwards.
The second way I figured out is to call Merge() on the empty DataSet like
so:

private void FrmTest_Load(object sender, System.EventArgs e) {
this.memberDataSet1.Merge((new MemberDALC()).GetAllMembers());
}

That works but looks kind of awkward to me. Any other suggestions will be
greatly appreciated. Sorry for this long post ;-)

Merge is by far the simpliest method.
If you want a better solution, do the databinding manually (by code).
 
L

Lorenz Kahl

Hi Miha,

thanks for the quick answer.
Sure, you bound the original instance, while you've substitued it with new
one.

I thought by doing so I'm just changing / replacing the reference to
another instance of the same type. Didn't I?
Merge is by far the simpliest method.
If you want a better solution, do the databinding manually (by code).

Ok. I think I can get used to using the merge-method - it works fine -
just thought I've overlooked something. What do you mean with 'better'?
Is it just more performant, or am I missing out other important features?

Thanks again,
bye,
Lorenz
 
M

Miha Markic [MVP C#]

Lorenz Kahl said:
Hi Miha,

thanks for the quick answer.


I thought by doing so I'm just changing / replacing the reference to
another instance of the same type. Didn't I?

Yes, you are changing the instance.
However, the binding code occurs before - it binds using original instance.
Only after binding occurs you do change the instance which binding does not
know about at all.
Ok. I think I can get used to using the merge-method - it works fine -
just thought I've overlooked something. What do you mean with 'better'? Is
it just more performant, or am I missing out other important features?

A bit less overhead.
In manual way you use the dataset directly and do not need the Merge which
creates a copy of original dataset (time consuming, memory overhead).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top