C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !






How to add colors to gridview cells based on condition


Posted Date: 29 May 2008    Resource Type: Code Snippets    Category: ASP.NET GridView

Posted By: Srinivas        Member Level: Gold
Rating:     Points: 10



We can add colors to the grid rows based on conditions.
suppose if we have a table where we store the empoyee names
and we should show the colors for rows of the grid based on the experinece of employee.

say if experinece is 2 years show green
if exp is 4 show red
and so on

We should handle using the gridview RowDataBound event


DataSourceID="SqlDataSource1" onrowdatabound="GridView1_RowDataBound">

SortExpression="experience" />






protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
try
{
if (e.Row.RowType.Equals( DataControlRowType.DataRow))
{
DataRowView drv = (DataRowView)e.Row.DataItem;
for (int i = 0; i < drv.DataView.Table.Columns.Count; i++)
{

if ((int)DataBinder.Eval(e.Row.DataItem, "experience") == 2)
e.Row.Cells[i].BackColor = System.Drawing.Color.LawnGreen;
if ((int)DataBinder.Eval(e.Row.DataItem, "experiece") == 4)
e.Row.Cells[i].BackColor = System.Drawing.Color.Red;
}
}
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}





Responses

Author: Kapil Dhawan    18 Jun 2008Member Level: Gold   Points : 2
Hello
Nice piece of code
Thanks for sharing your knowledge with us.
I hope to see more good code from your side
This code will help lots of guys
Thanks to you
Regards,
Kapil




Author: Simarjit    13 Aug 2008Member Level: Silver   Points : 0
nice code..

with regards

Simarjit Singh


Author: Raj Kumar Prajapati    27 Aug 2008Member Level: Bronze   Points : 1
ITS WORKING
Hello
Nice piece of code
Thanks for sharing your knowledge with us.
I hope to see more good code from your side
This code will help lots of guys
Thanks to you
Regards,
RAJ PRAJAPATI



Feedbacks      
Popular Tags   What are tags ?   Search Tags  
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: How to do Grid view sorting
Previous Resource: Insert/ADD image to SqlServer in ASP.Net 2.0
Return to Discussion Resource Index
Post New Resource
Category: ASP.NET GridView


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

silicone halloween masks

Contact Us    Privacy Policy    Terms Of Use