Hi nauna,
Refer below sample.
HTML
<asp:GridView runat="server" ID="GridView1" AutoGenerateColumns="false"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:BoundField DataField="CustomerId" HeaderText="CustomerId" />
<asp:BoundField DataField="ContactName" HeaderText="ContactName" />
<asp:BoundField DataField="Country" HeaderText="Country" />
<asp:TemplateField HeaderText="Rank">
<ItemTemplate>
<asp:Label ID="lblRank" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
TestEntities entity = new TestEntities();
var query = (from c in entity.TestCustomers
select c).ToList().Take(4);
List<object> result = new List<object>();
foreach (object item in query)
{
result.Add(item);
}
GridView1.DataSource = result;
GridView1.DataBind();
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if (e.Row.Cells[0].Text == "AAFKM")
{
(e.Row.FindControl("lblRank") as Label).Text = "first";
}
else if (e.Row.Cells[0].Text == "ALFKI")
{
(e.Row.FindControl("lblRank") as Label).Text = "second";
}
else if (e.Row.Cells[0].Text == "ANATR")
{
(e.Row.FindControl("lblRank") as Label).Text = "third";
}
else if (e.Row.Cells[0].Text == "ANTON")
{
(e.Row.FindControl("lblRank") as Label).Text = "fourth";
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Me.IsPostBack Then
Dim entity As TestEntities = New TestEntities()
Dim query = (From c In entity.TestCustomers Select c).ToList().Take(4)
Dim result As List(Of Object) = New List(Of Object)()
For Each item As Object In query
result.Add(item)
Next
GridView1.DataSource = result
GridView1.DataBind()
End If
End Sub
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
If e.Row.Cells(0).Text = "AAFKM" Then
(TryCast(e.Row.FindControl("lblRank"), Label)).Text = "first"
ElseIf e.Row.Cells(0).Text = "ALFKI" Then
(TryCast(e.Row.FindControl("lblRank"), Label)).Text = "second"
ElseIf e.Row.Cells(0).Text = "ANATR" Then
(TryCast(e.Row.FindControl("lblRank"), Label)).Text = "third"
ElseIf e.Row.Cells(0).Text = "ANTON" Then
(TryCast(e.Row.FindControl("lblRank"), Label)).Text = "fourth"
End If
End If
End Sub
Screenshot