Hi anvina,
I have created a sample please take its reference.
HTML
<table>
<tr>
<td>
<asp:GridView runat="server" ID="gvEmployees">
</asp:GridView>
</td>
<td>
<asp:GridView runat="server" ID="gvEmployees2">
</asp:GridView>
</td>
</tr>
</table>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.BindEmployees();
}
}
private void BindEmployees()
{
using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conString"].ConnectionString))
{
using (SqlCommand cmd = new SqlCommand("SELECT EmployeeID,FirstName,LastName,City,Country FROM Employees", con))
{
cmd.CommandType = CommandType.Text;
using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
this.gvEmployees.DataSource = dt;
this.gvEmployees.DataBind();
DataTable selected = new DataView(dt).ToTable(false, "EmployeeID", "FirstName", "LastName");
selected.Columns.Add("Name");
foreach (DataRow dr in selected.Rows)
{
dr["Name"] = dr["FirstName"] + " " + dr["LastName"];
}
selected.Columns.Remove("FirstName");
selected.Columns.Remove("LastName");
this.gvEmployees2.DataSource = selected;
this.gvEmployees2.DataBind();
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindEmployees()
End If
End Sub
Private Sub BindEmployees()
Using con As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("conString").ConnectionString)
Using cmd As SqlCommand = New SqlCommand("SELECT EmployeeID,FirstName,LastName,City,Country FROM Employees", con)
cmd.CommandType = CommandType.Text
Using sda As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
sda.Fill(dt)
Me.gvEmployees.DataSource = dt
Me.gvEmployees.DataBind()
Dim selected As DataTable = New DataView(dt).ToTable(False, "EmployeeID", "FirstName", "LastName")
selected.Columns.Add("Name")
For Each dr As DataRow In selected.Rows
dr("Name") = dr("FirstName") & " " & dr("LastName")
Next
selected.Columns.Remove("FirstName")
selected.Columns.Remove("LastName")
Me.gvEmployees2.DataSource = selected
Me.gvEmployees2.DataBind()
End Using
End Using
End Using
End Sub
Screenshot