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 !




Change the color of a datagrid column based on the value in that column


Posted Date: 21 Feb 2007    Resource Type: Code Snippets    Category: Winforms DataGrid

Posted By: Mathews . T. Varghese       Member Level: Gold
Rating:     Points: 10



When we are developing web applications we usually come across developing a lot of reports. when we are developing reports it is normal that we make use of datagrids.

You may have wondered how to highlight some of the columns in the datagrid based on their value.

The code given below helps in giving different colors to Datagrid colums based on the values in it. The below sample code uses a datagrid with template columns.


//Template column of the datagrid









//ItemDataBound for the datagrid

private void dgSiteSummary_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
switch ( e.Item.ItemType )
{
//when we are checking for values equal to '0' we have to check both Items and Alternating Items
case ListItemType.Item:
case ListItemType.AlternatingItem:
if( double.Parse ( ((Label)(e.Item.FindControl("lblLabour"))).Text ) == 0 )
{
//Getting the column based on the cell number. The same can be done using FindControl too
e.Item.Cells[3].BackColor=Color.Lime;
}
}
}




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

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 hide a column in Winforms Datagrid
Previous Resource: Insert a blank row at a particular position in a datagrid
Return to Discussion Resource Index
Post New Resource
Category: Winforms DataGrid


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

internet fax

Contact Us    Privacy Policy    Terms Of Use