Populate DataGrid from a dataset w/multiple tables?

B

Brian

I have a dataset containing 2 tables. I need to fill a datagrid using
data from both of these.

If I could create a SQL Statement to fill the datagrid, it would look
like this:

SELECT fields.fieldname, fieldvalues.value FROM fields, fieldvalues


I am having trouble finding documentation explaining how to populate
my datagrid using MULTIPLE tables like this. I'm willing to bet this
is just a few lines of code--does anyone have an example?

Thanks,
Brian
 
W

William Ryan eMVP

I think you may want to switch tacks on this and go a totally different
direction. Create two dataadapters, one for fields, and one for
fieldvalues. Use one dataset and fill two different tables:

dataAdapter1.Fill(dataSetName, "Fields");
dataAdapter2.Fill(dataSetName, "FieldValues");
dataGrid1.DataSource =- dataSetName;
Now you can use the following to add a DataRelation:
http://www.knowdotnet.com/articles/datarelation.html. At this point you can
bind the grid to the dataset (if need be, manipulate the displaymember and
valuemember properties) and you'll see a + sign on the parent rows. When you
expand it the child rows will appear right underneath it slightly indented.

To upudate the rows, just use

dataAdapter1.Update(dataSetName, "Fields");
dataAdapter2.Update(dataSetName, "FieldValues");
making sure to update the parent table first so no integrity constraints get
violated. You can get a whole lot fancier with validation rules if you want
client side and you can (and may want to) use a StronglyTyped DataSet which
defines the Relations already.

Using Joins is not advised b/c 1) you pull over redundant data which is
wasteful in every regard 2) You won't be able to use a CommandBuilder or
the DataAdpaterConfiguration wizard to build the commands for you 3) Writing
your own logic will be somewhere between a total pain in the a55 and
impossible.

Use the datarelation and life will be simple - other than defining the
relation which takes 3 lines of code at most (and can be done on one),
that's all the code for the crux of your problem. HTH,

Bill

BTW, I have a bunch of other stuff on advanced filtering and sorting on the
data access section of www.knowdotnet.com
--


Brian said:
I have a dataset containing 2 tables. I need to fill a datagrid using
data from both of these.

If I could create a SQL Statement to fill the datagrid, it would look
like this:

SELECT fields.fieldname, fieldvalues.value FROM fields, fieldvalues


I am having trouble finding documentation explaining how to populate
my datagrid using MULTIPLE tables like this. I'm willing to bet this
is just a few lines of code--does anyone have an example?

Thanks,
Brian

W.G. Ryan, eMVP

http://forums.devbuzz.com/
http://www.knowdotnet.com/williamryan.html
http://www.msmvps.com/WilliamRyan/
http://www.devbuzz.com/content/zinc_personal_media_center_pg1.asp
 

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