, OnRowDataBound. :
<style>
.class25
{
background-color:Red;
}
.class25a
{
font-weight:bolder;
}
.class23
{
background-color:Yellow;
}
.class23a
{
font-size:20px;
}
.class33
{
background-color:Fuchsia;
}
.class33a
{
font-style:italic;
}
</style>
<asp:GridView ID="customGrid" runat="server" OnRowDataBound="customGrid_RowDataBound">
:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindData();
}
}
private void BindData()
{
List<Employee> emp = new List<Employee>(){
new Employee{Age=25,ID=1,First="John",Last="Smith"},
new Employee{Age=23,ID=2,First="Juan",Last="Cabrera"},
new Employee{Age=33,ID=3,First="Richard",Last="Mar"}
};
customGrid.DataSource = emp;
customGrid.DataBind();
}
protected void customGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if ((e.Row.DataItem as Employee).Age == 25)
e.Row.Cells[0].Attributes.Add("class", "class25 class25a");
else if((e.Row.DataItem as Employee).Age == 23)
e.Row.Cells[0].Attributes.Add("class", "class23 class23a");
else
e.Row.Cells[0].Attributes.Add("class", "class33 class33a");
}
}
public class Employee
{
public int ID { get; set; }
public int Age { get; set; }
public string First { get; set; }
public string Last { get; set; }
}
:
