Create DataView or DataTable from DataSet

C

Christine Y.

Hello,

I know the subject sounds sort of backwards, but I created a DataSet by
using three different DataAdapters with different queries (each query
joins two tables in my database) and filled individual DataSets for
each DataAdapter. Then I merged the three DataSets to one, and now I
have a DataSet but no defined tables, etc. (or, at least not that I
know of!).

I want to create a DataView so I can sort contents of a DataGrid (whose
datasource is based on my resulting DataSet), but I don't know how to
create a DataView if I never defined DataTables. Maybe there was a more
straightforward way of defining the original DataSet? I'm trying to
keep it simple and not too complicated. My project is using C# and not
VB. Any help would be appreciated!

Thanks,
Christine
 
W

W.G. Ryan - MVP

<<I know the subject sounds sort of backwards, but I created a DataSet by
using three different DataAdapters with different queries (each query joins
two tables in my database) and filled individual DataSets for each
DataAdapter. Then I merged the three DataSets to one, and now I have a
DataSet but no defined tables, etc. (or, at least not that I know of!).>>
There's nothing backwards sounding about using the multiple datasets in and
of itself. However if you are using joins on the database side, just
remember that you aren't going to be able to use a commandBuilder or the
adapter's Update method to send back any changes if there are any, and
however you slice it, rolling your own commands will probably be a little
convoluted.

To create a DataView you can use this...

DataView dv = MyDataSet.Mytable.DefaultView;

or DataView dv = new DataView();
dv.Table = MyDataSet.MyTable;

now, specifically to your problem, after the merge, check the merged
dataset's Tables.Count property (I'm not sure if these are typed datasets or
not so I'm just going to use untyped syntax, you can convert as necessary.
using System.Diagnostics ;// at the top of the module
//Code to perform merge.
Debug.Assert(MyDataSet.Table.Count > 0; "We don't have any tables, something
screwy is going on");

I'm pretty sure that you have tables in there, but let's just verify that.
If you don't have any tables, then you have a much different problem. If
you have a table at this point, then it's just a matter of referencing it.
I know that in the past, I've had some problems on occassion when using
Typed datasets b/c by incorrectly setting the table mappings, I filled a
second table that I wasn't expecting to fill. The named table however was
empty. So it looked like the table wasn't filling (technically it wasn't)
although the real problem was that a new table was being created and
filled - the query to fill the table was working perfectly which is what I
originally thought was the problem.

Christine Y. said:
Hello,

I know the subject sounds sort of backwards, but I created a DataSet by
using three different DataAdapters with different queries (each query
joins two tables in my database) and filled individual DataSets for
each DataAdapter. Then I merged the three DataSets to one, and now I
have a DataSet but no defined tables, etc. (or, at least not that I
know of!).

I want to create a DataView so I can sort contents of a DataGrid (whose
datasource is based on my resulting DataSet), but I don't know how to
create a DataView if I never defined DataTables.
-- You can create a View by using DataView dv = new DataView();// There are
three overloads as well that allow you to specify the table you're going to
use, the RowFilter and the Sort. However you typically can just use the
Table's DefaultView unless you specifically need a new/separate dataview
(for example, if you're binding sepaarate controls to the same data, but
want it sorted differently or filtered differently). But the main point on
this is that in order for a DataView to be of any value, it needs a source.
While DataViews aren't the exact metaphorical equivalent of a database view,
they are close and in the sense that they are dependent on another object,
they're the same. So without a table to base the view on, they won't
provide any value.

Maybe there was a more
 

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