Please refer this code
HTML
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Id" HeaderText="Id" ItemStyle-Width="30" />
<asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
<asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
<asp:BoundField DataField="MobileNumber" HeaderText="MobileNumber" ItemStyle-Width="150" />
<asp:BoundField DataField="City" HeaderText="City" ItemStyle-Width="150" />
</Columns>
</asp:GridView>
Namespaces
using System.Data;
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3] { new DataColumn("Id", typeof(int)),
new DataColumn("Name", typeof(string)),
new DataColumn("Country",typeof(string)) });
dt.Rows.Add(1, "John Hammond", "United States");
dt.Rows.Add(2, "Mudassar Khan", "India");
dt.Rows.Add(3, "Suzanne Mathews", "France");
dt.Rows.Add(4, "Robert Schidner", "Russia");
DataTable dt2 = new DataTable();
dt2.Columns.Add("MobileNumber");
dt2.Columns.Add("City");
dt2.Rows.Add("111111111", "Washington");
dt2.Rows.Add("222222222", "Mumbai");
dt2.Rows.Add("333333333", "Paris");
dt2.Rows.Add("333333333", "Paris");
//It will skip this row as dt rows count is less than dt2 rows count
dt2.Rows.Add("333333333", "Paris");
for (int i = 0; i < dt2.Columns.Count; i++)
{
dt.Columns.Add(dt2.Columns[i].ColumnName);
}
for (int i = 0; i < dt2.Rows.Count; i++)
{
if (i >= dt.Rows.Count)
{
break;
}
else
{
dt.Rows[i]["MobileNumber"] = dt2.Rows[i]["MobileNumber"];
dt.Rows[i]["City"] = dt2.Rows[i]["City"];
}
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
Screenshot