How To Join Two DataTables Using C# In ADO.NET

In this tutorial, I will explain about Data Table by using Primary Key Property. Primary Key Property Return Type is "DataColumn" it was an Array. Return An array of System.Data.DataColumn objects. if there are Exceptions, then it will fire the System.Data.DataException.

Gets or sets an array of columns that function as primary keys for the data.

Here I show two Code Snippets,
  1. The first One with Primary Key Property
  2. The second one without Primary Key Property.
Without Primary Key Property

Here we are creating two Data Tables after that we are merging the two tables by using Merge Property. In this example, I am not using the database values just initializing the values in TableName.Rows.Add() method.

Steps To Create Data Table,
  1. First, create the Data Table instance
  2. Create Columns
  3. Add Rows
Program
  1. using System;  
  2. using System.Data;  
  3. public partial class _Default: System.Web.UI.Page {  
  4.         DataTable dt;  
  5.         DataRow dr;  
  6.         protected void Page_Load(object sender, EventArgs e) {  
  7.             if (!this.IsPostBack) {  
  8.                 DataTable dt = new DataTable();  
  9.                 dt.Columns.AddRange(new DataColumn[2] {  
  10.                     new DataColumn("ID"typeof(int)),  
  11.                         new DataColumn("RS"typeof(int))  
  12.                 });  
  13.                 dt.Rows.Add(1, 100);  
  14.                 dt.Rows.Add(2, 200);  
  15.                 dt.Rows.Add(5, 500);  
  16.                 dt.Rows.Add(7, 700);  
  17.                 GridView3.DataSource = dt;  
  18.                 GridView3.DataBind();  
  19.                 DataTable dt2 = new DataTable();  
  20.                 dt2.Columns.AddRange(new DataColumn[2] {  
  21.                     new DataColumn("ID"typeof(int)),  
  22.                         new DataColumn("name"typeof(string))  
  23.                 });  
  24.                 dt2.Rows.Add(1, "A");  
  25.                 dt2.Rows.Add(2, "B");  
  26.                 dt2.Rows.Add(3, "C");  
  27.                 dt2.Rows.Add(7, "I");  
  28.                 GridView2.DataSource = dt2;  
  29.                 GridView2.DataBind();  
  30.                 dt.Merge(dt2);  
  31.                 DataTable result = dt;  
  32.                 GridView4.DataSource = result;  
  33.                 GridView4.DataBind();  
  34.             }  
  35.         }  
Output looks like the below image.

With Primary Key Property

Here also, we are creating two Data Tables with Primary Key after that we are merging the two tables by using Merge Property. In this example, i am not using the database values just initializing the values in TableName.Rows.Add() method.

Steps to create Data Table,
  1. First, create the Data Table instance
  2. Create Columns
  3. Asign the Primary Key For Required Column 
  4. Add Rows
Program
  1. using System;  
  2. using System.Data;  
  3. public partial class _Default: System.Web.UI.Page {  
  4.         DataTable dt;  
  5.         DataRow dr;  
  6.         protected void Page_Load(object sender, EventArgs e) {  
  7.             if (!this.IsPostBack) {  
  8.                 DataTable dt = new DataTable();  
  9.                 dt.Columns.AddRange(new DataColumn[2] {  
  10.                     new DataColumn("ID"typeof(int)),  
  11.                         new DataColumn("RS"typeof(int))  
  12.                 });  
  13.                 dt.PrimaryKey = new DataColumn[] {  
  14.                     dt.Columns["ID"]  
  15.                 };  
  16.                 dt.Rows.Add(1, 100);  
  17.                 dt.Rows.Add(2, 200);  
  18.                 dt.Rows.Add(5, 500);  
  19.                 dt.Rows.Add(7, 700);  
  20.                 GridView3.DataSource = dt;  
  21.                 GridView3.DataBind();  
  22.                 DataTable dt2 = new DataTable();  
  23.                 dt2.Columns.AddRange(new DataColumn[2] {  
  24.                     new DataColumn("ID"typeof(int)),  
  25.                         new DataColumn("name"typeof(string))  
  26.                 });  
  27.                 dt2.PrimaryKey = new DataColumn[] {  
  28.                     dt2.Columns["ID"]  
  29.                 };  
  30.                 dt2.Rows.Add(1, "A");  
  31.                 dt2.Rows.Add(2, "B");  
  32.                 dt2.Rows.Add(3, "C");  
  33.                 dt2.Rows.Add(7, "I");  
  34.                 GridView2.DataSource = dt2;  
  35.                 GridView2.DataBind();  
  36.                 dt.Merge(dt2);  
  37.                 DataTable result = dt;  
  38.                 GridView4.DataSource = result;  
  39.                 GridView4.DataBind();  
  40.             }  
  41.         }  
Out Put Look Like Below Image,
 
 
Finally, we have learned how to use the Data Table Primary Key Property in ADO.NET. I hope you enjoyed the article. And feel free to ask the any question in any technology.