Step 1:- Server side code
public string GetFruitName(int id)
{
string strName
= string.Empty;
switch (id)
{
case 1:
strName
= "Mango";
break;
case 2:
strName
= "Apple";
break;
case 3:
strName
= "Graps";
break;
case 4:
strName
= "Orange";
break;
case 5:
strName
= "Pomegranate";
break;
default:
strName
= "";
break;
}
return strName;
}
Step 2:-
Design side code
<asp:TemplateField HeaderText="Fruit
Name">
<ItemTemplate>
<asp:Label ID="lblFruitName" runat="server" Text='<%#GetFruitName(Convert.ToInt32(Eval("FruitId"))) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Step 3:-
Default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Demo.Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" ClientIDMode="Static">
<Columns>
<asp:BoundField DataField="FruitId" HeaderText="Fruit
Id" />
<asp:TemplateField HeaderText="Fruit
Name">
<ItemTemplate>
<asp:Label ID="lblFruitName" runat="server" Text='<%#GetFruitName(Convert.ToInt32(Eval("FruitId"))) %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
Step 4:-
Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Demo
{
public partial class Default: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
GridView1.DataSource
= GenerateData();
GridView1.DataBind();
}
}
private DataTable GenerateData()
{
DataTable dt
= new DataTable();
DataRow dr
= null;
dt.Columns.Add(new DataColumn("FruitId", typeof(int)));
dt.Columns.Add(new DataColumn("FruitName", typeof(string)));
int totalRow
= 5;
for (int i
= 1; i <= totalRow; i++)
{
dr
= dt.NewRow();
dr["FruitId"] = i;
dr["FruitName"] = "";
dt.Rows.Add(dr);
}
return dt;
}
}
}