2 tables in one DataGrid

  • Thread starter Thread starter mwinne1
  • Start date Start date
M

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
 
Since no one has replied yet I'll start it off. My first guess would be if you had the two tables in seperate datasets and gave them the same name, then merged the datasets it might work. And then something about the MissingSchemaAction enum, or is that only for DataAdapters?
 
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/
 
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!
 
Back
Top