Hi akhter,
Please refer below sample code.
HTML
<asp:GridView ID="GVbalSummary" runat="server" AutoGenerateColumns="false" CssClass="mygrdContent"
HeaderStyle-BackColor="#66ccff" HeaderStyle-CssClass="header" HeaderStyle-ForeColor="White"
OnRowDataBound="OnRowDataBound" PagerStyle-CssClass="pager" RowStyle-CssClass="rows"
ShowFooter="true" Width="803px">
<Columns>
<asp:TemplateField HeaderStyle-HorizontalAlign="Left" HeaderStyle-Width="1%" HeaderText="Sr No">
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
<HeaderStyle CssClass="table_04" HorizontalAlign="Left" />
<ItemStyle CssClass="table_02" HorizontalAlign="Left" />
</asp:TemplateField>
<asp:TemplateField HeaderText="Codeitem" HeaderStyle-Width="2%" Visible="true">
<ItemTemplate>
<asp:Label ID="Codeitem" runat="server" Text='<%#Bind("Codeitem")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="2%" HeaderText="Item Name">
<ItemTemplate>
<asp:Label ID="Descriptionitem" runat="server" Text='<%#Bind("Descriptionitem")%>'></asp:Label>
</ItemTemplate>
<HeaderStyle Width="2%"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="2%" HeaderText="Rate">
<ItemTemplate>
<asp:Label ID="Assign_Item_Rate" runat="server" Text='<%#Bind("Assign_Item_Rate")%>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="Label1" runat="server" Text="Total"></asp:Label>
</FooterTemplate>
<HeaderStyle Width="2%"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="2%" HeaderText="QTY">
<ItemTemplate>
<asp:Label ID="QTY" runat="server" Text='<%#Bind("QTY")%>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal" runat="server"></asp:Label>
</FooterTemplate>
<HeaderStyle Width="2%"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="2%" HeaderText="Weight">
<ItemTemplate>
<asp:Label ID="BWeight" runat="server" Text='<%#Bind("BWeight")%>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal1" runat="server"></asp:Label>
</FooterTemplate>
<HeaderStyle Width="2%"></HeaderStyle>
</asp:TemplateField>
<asp:TemplateField HeaderStyle-Width="2%" HeaderText="Total_Amount">
<ItemTemplate>
<asp:Label ID="Total_Amount" runat="server" Text='<%#Bind("Total_Amount")%>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
<asp:Label ID="lblTotal2" runat="server"></asp:Label>
</FooterTemplate>
<HeaderStyle Width="2%"></HeaderStyle>
</asp:TemplateField>
</Columns>
<HeaderStyle BackColor="#66CCFF" CssClass="header" ForeColor="White"></HeaderStyle>
<PagerStyle CssClass="pager"></PagerStyle>
<RowStyle CssClass="rows"></RowStyle>
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Drawing;
VB
Imports System.Data
Imports System.Drawing
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[6] { new DataColumn("Codeitem"), new DataColumn("Descriptionitem"), new DataColumn("Assign_Item_Rate"),
new DataColumn("QTY"), new DataColumn("BWeight"), new DataColumn("Total_Amount") });
dt.Rows.Add("Shirt", "Good Quality", "NA", "2", "1000g", "1000");
dt.Rows.Add("T-Shirt", "Good Fabricx", "300", "5", "2500g", "1500");
dt.Rows.Add("Track-Pant", "Comforting", "400", "3", "1500g", "1200");
dt.Rows.Add("Jeans", "Good Material", "1500", "1", "1000g", "1500");
GridView.DataSource = dt;
GridView.DataBind();
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
Label cell = e.Row.FindControl("Assign_Item_Rate") as Label;
string rate = cell.Text.Trim().ToUpper();
if (rate == "NA")
{
cell.BackColor = Color.Red;
}
else
{
cell.BackColor = Color.Orange;
}
}
}
VB
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(5) {New DataColumn("Codeitem"), New DataColumn("Descriptionitem"), New DataColumn("Assign_Item_Rate"), New DataColumn("QTY"), New DataColumn("BWeight"), New DataColumn("Total_Amount")})
dt.Rows.Add("Shirt", "Good Quality", "NA", "2", "1000g", "1000")
dt.Rows.Add("T-Shirt", "Good Fabricx", "300", "5", "2500g", "1500")
dt.Rows.Add("Track-Pant", "Comforting", "400", "3", "1500g", "1200")
dt.Rows.Add("Jeans", "Good Material", "1500", "1", "1000g", "1500")
GVbalSummary.DataSource = dt
GVbalSummary.DataBind()
End Sub
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim cell As Label = TryCast(e.Row.FindControl("Assign_Item_Rate"), Label)
Dim rate As String = cell.Text.Trim().ToUpper()
If rate = "NA" Then
cell.BackColor = Color.Red
Else
cell.BackColor = Color.Orange
End If
End If
End Sub