How to cram into one DataGrid results from more than 1 queries?

A

antonyliu2002

I want to put results from more than 1 sql queries into a signle
datagrid,

For example, in my web application, I submit this following query to my
oracle database:

Select NetID, Firstname, Lastname from Student;

Then I put the result into my DataGrid. This is successful.

Following this, I issue the following query:

Select NetID, Firstname, Lastname from Faculty;

and I want to append the result to the same DataGrid.

Right now, my application is apparently overwriting the previous
records inserted into the DataGrid.

How can I append the result of the second query to the same DataGrid?
Thanks
 
G

Guest

The syntax for a SQL UNION QUERY:

select NetID,Firstname, Lastname from Student
UNION
select NetID,Firstname, Lastname from Faculty;

This will return a single resultset which you can bind to your grid.

Peter
 
G

Guest

You could also fill the same dataset for your both querry before binding it
to you grid.
As tables name are different they will be added to the dataset table
collection
Then when it will be bind to your grid you will have both querry

Drawback in that solution is than you will have to deploy you table tree in
grid view
 
A

antonyliu2002

Hmm, that is a great solution, I thought about this actually, but I am
not a good DB person, so I did not come up with the SQL Union solution.

Thanks a lot!
 
A

antonyliu2002

This sounds like a great solution, too, but I am new to the .NET
framework, would you mind briefing me a little bit about deploying
table tree in grid view? What is a table tree?

Or if you could please just give me a link, that would save you some
time.

Thanks.
 
S

sloan

Look up DataSet.Merge.


It does well for merging ROWS.

I'd go for a strongly typed dataset also.


Add New Item / DataSet
rename it "MyFirstStrongDS"

Click "XML" at the bottom of the Designer.

Its too much to explain, but I'll post the code.

Copy and Paste this in:


<?xml version="1.0" encoding="utf-8" ?>
<xs:schema id="MyFirstStrongDS" xmlns=""
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"
xmlns:codegen="urn:schemas-microsoft-com:xml-msprop">
<xs:element name="MyFirstStrongDS" msdata:IsDataSet="true">
<xs:complexType>
<xs:choice maxOccurs="unbounded">

<xs:element name="Publishers">
<xs:complexType>
<xs:sequence>
<xs:element name="NetID" type="xs:string" minOccurs="0" />
<xs:element name="Firstname" type="xs:string" minOccurs="0" />
<xs:element name="Lastname" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>

</xs:element>
</xs:schema>





In your code, you'll have


MyFirstStrongDS ds1 = //code to populate the DS from the Student table

MyFirstStrongDS ds2 = //code to populate the DS from the Faculty table

ds2.Merge(ds1);


Console.WriteLine(ds2.GetXml());


Something like that.


The strongly typed is optional, but the .Merge is a good place for you, if
you don't control the database, and can't create a UNION query.

...
 

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

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top