This is a discussion on Merging two Data Tables and displaying in DataGrid within the VB.NET Programming forums, part of the Software Development category; Hi all, Here am going to explain how to merge two data table in to a data set and displaying ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| Hi all, Here am going to explain how to merge two data table in to a data set and displaying in datagrid. Normally one datagrid is bound with one DataTable. However, consider the case of two tables both having a common key column called ID. Say one table contains personal information about employees and another table contains some official information. EmployeeID is the common in both the tables and both tables have same number of rows (equal to the no of employees). How to display such tables in one DataGrid?
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| Sponsored Links |
| |||
| First of all, you need to create two tables as like the following, Code: CREATE TABLE [dbo].[Table1] ( [EmployeeID] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [Name] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY] Code: CREATE TABLE [dbo].[Table2] ( [EmployeeID] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NOT NULL , [Department] [char] (10) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| Note, that the two tables can be linked via EmployeeID column. This is very simplified view of the tables to keep the example simple.
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| Now am going to fill the datasets. Here I am going to fill two separate DataSets from these two tables. If you wish you can also populate them in the same DataSet. Code: Dim connstr As String = "Integrated Security=SSPI;
User ID=sa;Initial Catalog=Northwind;Data Source=SERVER\netsdk"
Dim cnn As New SqlConnection(connstr)
Dim da1 As New SqlDataAdapter("select * from table1", cnn)
Dim da2 As New SqlDataAdapter("select * from table2", cnn)
Dim ds1 As New DataSet()
Dim ds2 As New DataSet()
da1.Fill(ds1, "table1")
da2.Fill(ds2, "table1")
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| Now, we will set the primary keys for both of the tables. Why is it necessary? When we merge results of these two DataSets primary key plays important role. Code: Dim pk1(0) As DataColumn
Dim pk2(0) As DataColumn
pk1(0) = ds1.Tables(0).Columns("EmployeeID")
ds1.Tables(0).PrimaryKey = pk1
pk2(0) = ds2.Tables(0).Columns("EmployeeID")
ds2.Tables(0).PrimaryKey = pk2
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| Merging DataSets In this step, we will merge the two DataSets by calling the Merge method of the DataSet class. Code: ds1.Merge(ds2, False, MissingSchemaAction.Add)
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| Binding to DataGrid Finally we will bind our DataGrid with the above merged dataset. Code: DataGrid1.DataSource=ds1 DataGrid1.DataBind()
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
| |||
| yeap...there is a way to merge datatables. See the follwing sample code. Code: private static void DemonstrateMergeTable()
{
DataTable table1 = new DataTable("Items");
// Add columns
DataColumn idColumn = new DataColumn("id", typeof(System.Int32));
DataColumn itemColumn = new DataColumn("item", typeof(System.Int32));
table1.Columns.Add(idColumn);
table1.Columns.Add(itemColumn);
// Set the primary key column.
table1.PrimaryKey = new DataColumn[] { idColumn };
// Add RowChanged event handler for the table.
table1.RowChanged += new
System.Data.DataRowChangeEventHandler(Row_Changed);
// Add ten rows.
DataRow row;
for (int i = 0; i <= 9; i++)
{
row = table1.NewRow();
row["id"] = i;
row["item"] = i;
table1.Rows.Add(row);
}
// Accept changes.
table1.AcceptChanges();
PrintValues(table1, "Original values");
// Create a second DataTable identical to the first.
DataTable table2 = table1.Clone();
// Add column to the second column, so that the
// schemas no longer match.
table2.Columns.Add("newColumn", typeof(System.String));
// Add three rows. Note that the id column can't be the
// same as existing rows in the original table.
row = table2.NewRow();
row["id"] = 14;
row["item"] = 774;
row["newColumn"] = "new column 1";
table2.Rows.Add(row);
row = table2.NewRow();
row["id"] = 12;
row["item"] = 555;
row["newColumn"] = "new column 2";
table2.Rows.Add(row);
row = table2.NewRow();
row["id"] = 13;
row["item"] = 665;
row["newColumn"] = "new column 3";
table2.Rows.Add(row);
// Merge table2 into the table1.
Console.WriteLine("Merging");
table1.Merge(table2, false, MissingSchemaAction.Add);
PrintValues(table1, "Merged With table1, schema added");
} Code: private static void Row_Changed(object sender,
DataRowChangeEventArgs e)
{
Console.WriteLine("Row changed {0}\t{1}", e.Action,
e.Row.ItemArray[0]);
} Code: private static void PrintValues(DataTable table, string label)
{
// Display the values in the supplied DataTable:
Console.WriteLine(label);
foreach (DataRow row in table.Rows)
{
foreach (DataColumn col in table.Columns)
{
Console.Write("\t " + row[col].ToString());
}
Console.WriteLine();
}
}
__________________ S.VinothkumaR Behind me is infinite power, Before me is Endless Possibility, Around me is Boundless Opportunity, Why should I fear! |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| merging wav files | Decoy F 13 | The Lounge | 0 | 02-16-2008 12:19 AM |
| Displaying Progress bar | abhilashdas | Flash Actionscript Programming | 5 | 12-24-2007 03:19 AM |
| please tell me how to import excel data to sql server without datagrid | oxygen | C# Programming | 2 | 12-16-2007 08:33 PM |
| Merging the files | Shanthi | Testing Tools | 1 | 08-27-2007 04:46 AM |
| Setup Project - Displaying Messages using Launch Conditions | raja | C# Programming | 0 | 05-08-2007 02:16 AM |