databinding untyped dataset and visual design

G

Guest

Hi,
I have lots of trival databound forms that maintain simple tables that I
want to port to winforms and edit with a datagridview or similar.

I would like to achieve the following: Deriving from a baseclass
'DataBoundForm' or similar I want to be able to set a design-time sql select
statement and be able to visually do databinding and datagrid column
formattind etc using an untyped dataset with the schema of the sql select
statement.

1. I don't want to have to create lots of typed datasets and table adapters
so I have created a generic TableAdapter object implementing a generic
interface similar to those generated by the IDE. This accepts a simple Select
sql statement and creates all the CRUD stuff with a commandbuilder etc and
also can be the design time source of the schema info.

2. I have created a base form that has an instance of this tableadapter and
at design-time only, fills and returns an untyped dataset from the Sql
statement provided. The idea is to then be able to access this on the derived
form to use as the datasource for databinding, whether it be a datagridview
or the like.


3. What I would like to know is: is it possible to get this populated
untyped dataset to show up in the IDE as a valid datasource available for
databinding and doing the design work on the derived form.

Potential problems which I imagine might exist but don't really know about:
1. While the untyped dataset is populated during the instanciation of the
base class, this schema info etc may not be sufficient for the IDE to see it
as a valid data source.
2. If using a DataGridView, again the datagridview may not be able to
extract the schema to visually set up column formatting etc at designtime,
but I won't know this until I can solve point 1.

Any illumination on whether my goal is futile or if anyone can shed some
light on the matter would be excellent.

Thanks in advance.
Simon
 
L

Linda Liu [MSFT]

Hello Simon,
Thank you posting.
This is a quick note to let you know that I am performing research on this
issue and will get back to you as soon as possible. I appreciate your
patience.


Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
L

Linda Liu [MSFT]

Hi Simon,
Would you show me your define code of the generic TableAdapter class?
Thanks.



Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
L

Linda Liu [MSFT]

Hi Simon,
Firstly I¡¯m sorry leaving you hanging.
To answer your first question about the schema, we can get the actual
data type of each data column by accessing DataColumn¡¯s DataType property
(Also with other properties like ColumnName and Expression).
For next question: how did we bind an untyped dataset to DataGridView
at design time? As we know, when bind a DataGridView, we need to first
specify its Data Source property, which has to be a component defined in
current project, for example a strong typed dataset, or a bindingsource
control. The data source can be of any components that implements one of
the following interfaces:

¡¤ The IList interface, including one-dimensional arrays.
¡¤ The IListSource interface, such as the DataTable and DataSet classes.
¡¤ The IBindingList interface, such as the BindingList class.
¡¤ The IBindingListView interface, such as the BindingSource class.

This make me think we can create a UserControl in the project and
implements the IListSource interface, which can be bound to the datasource
of the datagridview.
However, my test failed with following facts: even we can bind such a
list to datagridview, we cannot retrieve dynamic column information form
it. When datagridview get data from such a list, it will get an item as
object and get its fixed public properties as columns, the columns cannot
be dynamic. In other words, a fixed class definition or a schema is still
required here.
Therefore, I have to tell a bad news here, when bind to datagridview
at design time, the datasource have to be a strong typed dataset, or a list
containing predefined objects.
I think there are two work around here:

1. You can use a DataAdapter object to get data from database
according to a given SQL Statement in run time and fill the data into an
untyped DataTable. And then bind a DataGridView to this untyped DataTable.
2. You can generate a TableAdapter for each database table you want
and bind a DataGridView to the DataTable the TableAdapter generated.

Hope this is helpful to you.
Please let me know if you have any other concerns, or need anything
else.



Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
G

Guest

Hi Linda,
Firstly sorry for the long delay in responding, have had a long Easter Break
away from the PC :0).
Secondly I suspected the difficulties with the schema which is why I tried
to pre-empt this by having the dataset instanciated in the base class so that
even at design time in the derived class there would be a populated instance
of the dataset available for the IDE to discover its schema.
It looks like the implementation of IListSource in the DataTable does not
differ even when the datatype is available as you say.
Could I implement say IListSource in the DataAdapter class that I have built
where the interface methods do the schema and type discovery using the
DataColumn type.
Then at design-time I could bind to this class and at runtime bind to the
datatable itself?
If possible what might the implementation of IListSource.GetList look like?
Could it be as simple as returning the DataColumnCollection of the populated
DataTable?

Cheers
Simon
 
L

Linda Liu [MSFT]

Hi Simon,

Thank you for your reply.
This is a quick note to let you know that I am performing research on this
issue and will get back to you as soon as possible. I appreciate your
patience.


Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 
G

Guest

Hi Linda,
I have succeeded in building an Object that is a cross between a generic
TableAdapter and the Web.SqlDataSource object.
I implemented IListSource as you rightly pointed out and in the GetList
method I created a dynamic assembly that emits an object with properties
built from the names and types of the datatable columns created by the Select
Statement.

My 'TableAdapter' then appears as an available datasource to bind to at
design-time allowing me to use the IDE to do all the binding graphically and
configure DataGridView columns etc.
Then at run-time I simply bind the BindingSource or DataGridView etc to
whatever untyped dataset I like I have or need at runtime.

This saves me having **lots** of trivial typed datasets and tableadapters
and is especially useful if I want a single WebService method to supply
various untyped Datasets/Tables for lookup lists etc without having lots of
typed GetListxxxx type methods for every dataset required.

Thanks for your input.
If anyone is interested in the code for any of this let me know.
Cheers Simon.
 
L

Linda Liu [MSFT]

Hi Simon,

Thank you for your reply and the detailed additional feedback on how you
were successfully in resolving this issue.
I am very glad to hear that you have resolved this problem.
If you have any other question or concerns, please do not hesitate to
contact us. It is always our pleasure to be of assistance.

Have a nice day!


Sincerely,
Linda Liu
Microsoft Online Community Support

====================================================
When responding to posts,please "Reply to Group" via
your newsreader so that others may learn and benefit
from your issue.
====================================================
 

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