Hi zah,
You can either handle it in procedure to get the name instead of id using sub query or handle it from code behind.
Refer below example using code behind.
HTML
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound">
<Columns>
<asp:BoundField DataField="OrderId" HeaderText="Order Id" />
<asp:BoundField DataField="CustomerID" HeaderText="Customer Name" />
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="lblName" Text='<%# Eval("CustomerID") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="Freight" HeaderText="Freight" />
</Columns>
</asp:GridView>
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.BindGrid();
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string customerID = e.Row.Cells[1].Text;
e.Row.Cells[1].Text = GetNameById(customerID);
(e.Row.FindControl("lblName") as Label).Text = GetNameById(customerID);
}
}
private void BindGrid()
{
string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT TOP 5 OrderId,CustomerID,Freight FROM Orders"))
{
//cmd.Parameters.AddWithValue("@Action", "SELECT");
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}
}
}
private string GetNameById(string id)
{
string name = "";
string constr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT ContactName FROM Customers WHERE CustomerID = @Id"))
{
cmd.Parameters.AddWithValue("@Id", id);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
name = Convert.ToString(cmd.ExecuteScalar());
con.Close();
}
}
return name;
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Me.BindGrid()
End If
End Sub
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim customerID As String = e.Row.Cells(1).Text
e.Row.Cells(1).Text = GetNameById(customerID)
TryCast(e.Row.FindControl("lblName"), Label).Text = GetNameById(customerID)
End If
End Sub
Private Sub BindGrid()
Dim constr As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT TOP 5 OrderId,CustomerID,Freight FROM Orders")
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.CommandType = CommandType.Text
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
GridView1.DataSource = dt
GridView1.DataBind()
End Using
End Using
End Using
End Using
End Sub
Private Function GetNameById(ByVal id As String) As String
Dim name As String = ""
Dim constr As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT ContactName FROM Customers WHERE CustomerID = @Id")
cmd.Parameters.AddWithValue("@Id", id)
cmd.CommandType = CommandType.Text
cmd.Connection = con
con.Open()
name = Convert.ToString(cmd.ExecuteScalar())
con.Close()
End Using
End Using
Return name
End Function
Screenshot