Hi kankon,
Use GridView OnRowDataBound event.
Check this example. Now please take its reference and correct your code.
HTML
<asp:RadioButtonList ID="rblCountries" runat="server" AutoPostBack="true" Height="31px"
OnSelectedIndexChanged="OnSelectedIndexChanged" RepeatDirection="Horizontal"
RepeatLayout="Flow" BorderStyle="Solid" BackColor="#EFEFEF">
<asp:ListItem Value="Section1">Section1</asp:ListItem>
<asp:ListItem Value="Section2">Section2</asp:ListItem>
<asp:ListItem Value="Section3">Section3</asp:ListItem>
</asp:RadioButtonList>
<hr />
<asp:GridView ID="GridView1" runat="server" AlternatingRowStyle-BackColor="white" AutoGenerateColumns="False"
CssClass="Grid" Font-Names="Arial" Font-Size="11pt" ShowHeaderWhenEmpty="True"
Style="font-size: medium" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px"
CellPadding="3" ForeColor="Black" GridLines="Vertical" Width="100%" OnRowDataBound="OnRowDataBound">
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="checkAll" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Eval("IsSelected2") %>' />
</ItemTemplate>
<ItemStyle HorizontalAlign="Center" Width="5px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="م">
<ItemTemplate>
<asp:Label ID="lblRowNumber" Text='<%# Container.DataItemIndex + 1 %>' runat="server" />
</ItemTemplate>
<ControlStyle Width="30px" />
<ItemStyle HorizontalAlign="Center" Width="30px" />
</asp:TemplateField>
<asp:TemplateField HeaderText="الإسم">
<ItemTemplate>
<asp:Label ID="nametxt" Text='<%# Eval("name") %>' runat="server" />
</ItemTemplate>
<ItemStyle Width="250px" HorizontalAlign="Right"></ItemStyle>
</asp:TemplateField>
<asp:BoundField DataField="blockrasme">
<ItemStyle Width="100px" HorizontalAlign="Center"></ItemStyle>
</asp:BoundField>
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data;
using System.Drawing;
VB.Net
Imports System.Data
Imports System.Drawing
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid4();
}
}
private void BindGrid4()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[]
{
new DataColumn("IsSelected2",typeof(bool)),
new DataColumn("name"),
new DataColumn("blockrasme")
});
dt.Rows.Add(false, "Shirt", "Section1");
dt.Rows.Add(false, "Jeans", "Section10");
dt.Rows.Add(true, "Trousers", "Section9");
dt.Rows.Add(false, "Tie", "Section10");
dt.Rows.Add(true, "Cap", "Section9");
dt.Rows.Add(false, "Hat", "Section9");
dt.Rows.Add(false, "Scarf", "Section1");
dt.Rows.Add(false, "Belt", "Section10");
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string section = e.Row.Cells[3].Text.Trim().ToLower();
switch (section)
{
case "section1":
e.Row.BackColor = Color.Red;
break;
case "section9":
e.Row.BackColor = Color.Green;
break;
case "section10":
e.Row.BackColor = Color.Yellow;
break;
default:
e.Row.BackColor = Color.White;
break;
}
}
}
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
BindGrid4();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindGrid4()
End If
End Sub
Private Sub BindGrid4()
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {
New DataColumn("IsSelected2", GetType(Boolean)),
New DataColumn("name"),
New DataColumn("blockrasme")})
dt.Rows.Add(False, "Shirt", "Section1")
dt.Rows.Add(False, "Jeans", "Section10")
dt.Rows.Add(True, "Trousers", "Section9")
dt.Rows.Add(False, "Tie", "Section10")
dt.Rows.Add(True, "Cap", "Section9")
dt.Rows.Add(False, "Hat", "Section9")
dt.Rows.Add(False, "Scarf", "Section1")
dt.Rows.Add(False, "Belt", "Section10")
GridView1.DataSource = dt
GridView1.DataBind()
End Sub
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim section As String = e.Row.Cells(3).Text.Trim().ToLower()
Select Case section
Case "section1"
e.Row.BackColor = Color.Red
Case "section9"
e.Row.BackColor = Color.Green
Case "section10"
e.Row.BackColor = Color.Yellow
Case Else
e.Row.BackColor = Color.White
End Select
End If
End Sub
Protected Sub OnSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
BindGrid4()
End Sub
Screenshot