Home All Groups Group Topic Archive Search About

2 tables in one DataGrid

Author
22 Mar 2006 9:02 PM
mwinne1
Hello!

I'm looking for an easy way to take 2 tables with different, but
similar, structures and combining them into one DataGrid, with a
hyperlink on each record to send you to a page depending on what table
it's from.

Is there an easy way to at least merge the two tables into one to
display the DataGrid?

Thanks for any help!

Oh yes... I'm using VB

Author
23 Mar 2006 12:48 AM
agapeton
Sorry, I know you wanted VB... you use the Merge though.

using (SqlConnection connection = new SqlConnection("data
source=servername;initial catalog=Northwind;integrated
security=SSPI;persist security info=False;packet size=4096")) {
    string getEmployees = "select * from Employee";
    string getNewEmployees = "select * from NewEmployee";
    DataSet newNorthwind = new DataSet( );

    connection.Open( );

    using (SqlCommand command = new SqlCommand(getEmployees,
connection)) {
        SqlDataAdapter da = new SqlDataAdapter( );
        da.SelectCommand = command;
        da.Fill(newNorthwind, "Employee");
    }

    using (SqlCommand command = new SqlCommand(getNewEmployees,
connection)) {
        SqlDataAdapter da = new SqlDataAdapter( );
        da.SelectCommand = command;
        da.Fill(newNorthwind, "NewEmployee");
    }

    dataGridView1.DataSource = newNorthwind.Tables["Employee"];
    dataGridView2.DataSource = newNorthwind.Tables["NewEmployee"];

    newNorthwind.Tables.Add("AllEmployee");

newNorthwind.Tables["AllEmployee"].Merge(newNorthwind.Tables["Employee"]);

newNorthwind.Tables["AllEmployee"].Merge(newNorthwind.Tables["NewEmployee"]);

    dataGridView3.DataSource = newNorthwind.Tables["AllEmployee"];
}

David Betz
WinFX Harmonics Blog
http://www.davidbetz.net/winfx/
Author
23 Mar 2006 3:18 AM
mwinne1
I think it's easy enough to convert to VB. THis looks great!

However, if one table has a field called "Pizza" and another has a
field called "Hamburger", is there a way that the new datasource can
'merge' those columns into one and called it "Food"?

Thanks for the help!
Author
23 Mar 2006 8:52 AM
Cor Ligthert [MVP]
Just rename one of those (or both) in advance

http://msdn2.microsoft.com/en-us/library/system.data.datacolumn.columnname.aspx

I hope this helps,

Cor

<mwin***@yahoo.com> schreef in bericht
Show quote
news:1143083893.590279.163310@u72g2000cwu.googlegroups.com...
>I think it's easy enough to convert to VB. THis looks great!
>
> However, if one table has a field called "Pizza" and another has a
> field called "Hamburger", is there a way that the new datasource can
> 'merge' those columns into one and called it "Food"?
>
> Thanks for the help!
>

AddThis Social Bookmark Button