Hi PriyaKann,
Refer below sample code.
HTML
<asp:GridView runat="server" ID="gvEmployees"></asp:GridView>
Namespaces
C#
using System.Data;
using System.Linq;
VB.Net
Imports System.Data
Imports System.Linq
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt1 = new DataTable();
dt1.Columns.AddRange(new DataColumn[] {
new DataColumn("UserId"),
new DataColumn("Name"),
new DataColumn("Age"),
new DataColumn("Salary") });
dt1.Rows.Add("1", "Arun", 25);
dt1.Rows.Add("2", "Bala", 30);
dt1.Rows.Add("3", "Chandru", 35);
dt1.Rows.Add("4", "Dinesh", 32);
dt1.Rows.Add("5", "Ester", 27);
dt1.PrimaryKey = new DataColumn[] { dt1.Columns["UserId"] };
DataTable dt2 = new DataTable();
dt2.Columns.AddRange(new DataColumn[] {
new DataColumn("UserId"),
new DataColumn("Name"),
new DataColumn("Age"),
new DataColumn("Designation") });
dt2.Rows.Add("2", "Bala", 30, "Er");
dt2.Rows.Add("3", "Chandru", 35, "Se.En");
dt2.Rows.Add("6", "Farooq", 27, "Team Lead");
dt2.Rows.Add("7", "Gokul", 35, "Engineer");
dt2.Rows.Add("8", "Helen", 19, "Site Engineer");
dt2.PrimaryKey = new DataColumn[] { dt2.Columns["UserId"] };
dt1.Merge(dt2, false, MissingSchemaAction.Add);
DataTable dt3 = new DataTable();
dt3.Columns.AddRange(new DataColumn[] {
new DataColumn("UserId"),
new DataColumn("Name"),
new DataColumn("Age") });
var result = (from t1 in dt1.AsEnumerable()
select new
{
UserId = t1["UserId"].ToString(),
UserName = t1["Name"].ToString(),
Age = t1["Age"].ToString()
}).ToList();
foreach (var item in result)
{
dt3.Rows.Add(item.UserId, item.UserName, item.Age);
}
gvEmployees.DataSource = dt3;
gvEmployees.DataBind();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim dt1 As DataTable = New DataTable()
dt1.Columns.AddRange(New DataColumn() {
New DataColumn("UserId"),
New DataColumn("Name"),
New DataColumn("Age"),
New DataColumn("Salary")})
dt1.Rows.Add("1", "Arun", 25)
dt1.Rows.Add("2", "Bala", 30)
dt1.Rows.Add("3", "Chandru", 35)
dt1.Rows.Add("4", "Dinesh", 32)
dt1.Rows.Add("5", "Ester", 27)
dt1.PrimaryKey = New DataColumn() {dt1.Columns("UserId")}
Dim dt2 As DataTable = New DataTable()
dt2.Columns.AddRange(New DataColumn() {
New DataColumn("UserId"),
New DataColumn("Name"),
New DataColumn("Age"),
New DataColumn("Designation")})
dt2.Rows.Add("2", "Bala", 30, "Er")
dt2.Rows.Add("3", "Chandru", 35, "Se.En")
dt2.Rows.Add("6", "Farooq", 27, "Team Lead")
dt2.Rows.Add("7", "Gokul", 35, "Engineer")
dt2.Rows.Add("8", "Helen", 19, "Site Engineer")
dt2.PrimaryKey = New DataColumn() {dt2.Columns("UserId")}
dt1.Merge(dt2, False, MissingSchemaAction.Add)
Dim dt3 As DataTable = New DataTable()
dt3.Columns.AddRange(New DataColumn() {
New DataColumn("UserId"), New DataColumn("Name"),
New DataColumn("Age")})
Dim result = (From t1 In dt1.AsEnumerable()
Select New With {
.UserId = t1("UserId"),
.UserName = t1("Name"),
.Age = t1("Age")
}).ToList()
For Each item In result
dt3.Rows.Add(item.UserId, item.UserName, item.Age)
Next
gvEmployees.DataSource = dt3
gvEmployees.DataBind()
End Sub
Screenshot