Hi amar,
Refer below code.
HTML
<form id="form1" runat="server">
<asp:GridView ID="GridView1" runat="server" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
Font-Names="Arial" Font-Size="10" RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White"
AlternatingRowStyle-ForeColor="#000" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Col1" HeaderText="Col1" />
<asp:BoundField DataField="Col2" HeaderText="Col2" />
<asp:BoundField DataField="Col3" HeaderText="Col3" />
<asp:BoundField DataField="Col4" HeaderText="Col4" />
</Columns>
</asp:GridView>
</form>
Namespaces
C#
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[4]
{
new DataColumn("Col1"),
new DataColumn("Col2"),
new DataColumn("Col3"),
new DataColumn("Col4")
});
DataTable dt1 = new DataTable();
dt1.Columns.AddRange(new DataColumn[4]
{
new DataColumn("Id"),
new DataColumn("fname"),
new DataColumn("Lname"),
new DataColumn("Dept")
});
dt1.Rows.Add("1", "aaa", "xxx", "IT");
dt1.Rows.Add("2", "bbb", "yyy", "SW");
DataRow dr = dt.NewRow();
for (int i = 0; i < dt1.Columns.Count; i++)
{
dr[i] = dt1.Columns[i].ColumnName;
}
dt.Rows.Add(dr.ItemArray);
for (int row = 0; row < dt1.Rows.Count; row++)
{
DataRow dr1 = dt.NewRow();
for (int column = 0; column < dt1.Columns.Count; column++)
{
dr1[column] = dt1.Rows[row][column];
}
dt.Rows.Add(dr1.ItemArray);
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Me.IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(3) {New DataColumn("Col1"), New DataColumn("Col2"), New DataColumn("Col3"), New DataColumn("Col4")})
Dim dt1 As DataTable = New DataTable()
dt1.Columns.AddRange(New DataColumn(3) {New DataColumn("Id"), New DataColumn("fname"), New DataColumn("Lname"), New DataColumn("Dept")})
dt1.Rows.Add("1", "aaa", "xxx", "IT")
dt1.Rows.Add("2", "bbb", "yyy", "SW")
Dim dr As DataRow = dt.NewRow()
For i As Integer = 0 To dt1.Columns.Count - 1
dr(i) = dt1.Columns(i).ColumnName
Next
dt.Rows.Add(dr.ItemArray)
For row As Integer = 0 To dt1.Rows.Count - 1
Dim dr1 As DataRow = dt.NewRow()
For column As Integer = 0 To dt1.Columns.Count - 1
dr1(column) = dt1.Rows(row)(column)
Next
dt.Rows.Add(dr1.ItemArray)
Next
GridView1.DataSource = dt
GridView1.DataBind()
End If
End Sub
Screenshot