The following code will show you how to merge the data from the datasets. I added comments to help explain, but in a nutshell, you are going to loop through the data in the second dataset (the data you want to merge). For each item, simply search the first dataset for that item. If it doesn't exist in the first table, create a new row for the new item, populate the columns, and add the row to the table in the dataset. In order to search the first table, I show how you can do that with a dataview on the table in the forst dataset. The Dataview will allow you to call the find method on a primary field. Also, in my example, disregard the populateData function since you are already doing that. I had to add that to populate the datasets in my example with your data. Also, this example is a Console application. The concept is exactly the same whether it is a Windows Form, Web App, or console app. just don't use the Console.Writeline unless you are writing a console application. Here you go:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
namespace MergeDatasetExample
{
class Program
{
static DataSet Dataset1 = new DataSet();
static DataSet Dataset2 = new DataSet();
static void Main(string[] args)
{
// Don't worry about this, just something I need to
// do to provide an example.
populateDatasets();
// Create a dataview for the table in Dataset1.
// This will make it easy to search for values.
// We need to sort on a field or an exception is
// thrown. We will sort on the Empkey field.
DataView dv = new DataView(Dataset1.Tables[0], "",
"Empkey", DataViewRowState.CurrentRows);
// Loop through all of the rows in the table in
// Dataset2 and add the ones not in Dataset1 to
// the table om Dataset2
foreach (DataRow dr in Dataset2.Tables[0].Rows)
{
// See if we can find the Empkey value in the current
// row in the dataview. If so, then skip it, else add it
if (dv.Find(dr["Empkey"]) == -1)
{
DataRow newDr = Dataset1.Tables[0].NewRow();
newDr["Empkey"] = dr["Empkey"];
newDr["Empname"] = dr["Empname"];
Dataset1.Tables[0].Rows.Add(newDr);
}
}
//Display all rows in Dataset1 table[0].
foreach (DataRow dr in Dataset1.Tables[0].Rows)
{
Console.WriteLine("tblkey: {0}, Empkey: {1}, Empname: {2}",
dr["tblkey"], dr["Empkey"], dr["Empname"]);
}
Console.ReadKey();
}
static void populateDatasets()
{
// Create columns for first table
DataTable dt1 = new DataTable("Table1");
DataColumn dc = new DataColumn("tblkey");
dt1.Columns.Add(dc);
dc = new DataColumn("Empkey");
dt1.Columns.Add(dc);
dc = new DataColumn("Empname");
dt1.Columns.Add(dc);
//Add rows to first table
DataRow dr = dt1.NewRow();
dr["tblkey"] = "T101";
dr["Empkey"] = "E10";
dr["Empname"] = "Natraj";
dt1.Rows.Add(dr);
dr = dt1.NewRow();
dr["tblkey"] = "T102";
dr["Empkey"] = "E11";
dr["Empname"] = "Siva";
dt1.Rows.Add(dr);
dr = dt1.NewRow();
dr["tblkey"] = "T103";
dr["Empkey"] = "E14";
dr["Empname"] = "ganesh";
dt1.Rows.Add(dr);
// Add Table1 to Dataset 1
Dataset1.Tables.Add(dt1);
// Create columns for second table
DataTable dt2 = new DataTable("Table2");
dc = new DataColumn("Empkey");
dt2.Columns.Add(dc);
dc = new DataColumn("Empname");
dt2.Columns.Add(dc);
//Add rows to second table
dr = dt2.NewRow();
dr["Empkey"] = "E10";
dr["Empname"] = "karthi";
dt2.Rows.Add(dr);
dr = dt2.NewRow();
dr["Empkey"] = "E11";
dr["Empname"] = "thriu";
dt2.Rows.Add(dr);
dr = dt2.NewRow();
dr["Empkey"] = "E13";
dr["Empname"] = "maran";
dt2.Rows.Add(dr);
// Add Table2 to Dataset 2
Dataset2.Tables.Add(dt2);
}
}
}
If you really want to understand this code, just create a console application and use debug to go through each line. That will really help your understanding.