Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old December 3rd, 2006, 01:31 AM
su su is offline
Authorized User
Join Date: Nov 2006
Posts: 17
Thanks: 0
Thanked 0 Times in 0 Posts
Default Cell/Font Color in DataGrid.


I am using C#.NET, Visual Web Developer 2005 and SQL Server 2000.

I am using Datagrid, SqlDataAdapter and Dataset for displaying data. One of the resultant column of the Datagrid is composed of concatenation of 2 columns in my database and is displayed like: <yes/no>, <no/no>, <yes/yes>, <no/yes>, <yes/cncld> , <no/cncld>.

This column can have one of these combination in the datagrid column. Now, I want to display all "yes" in green and all "no" in red and all "cncld" in black. so, essentially I want more than one color in my cell. I know that cell.Forecolor property can be applied to the entire cell making <yes/no> completely either green or red. But my task is applying more than one color in one cell.

Is there any way we can do this? I am able to capture the text "yes" , "no" and "cncld" as a string, but how do i then apply color? Should this be done in html file or the code behind file?

I am concatenating the two columns in html as:

<asp:Label runat="server" Text='<%# FormatSalesOPDName(DataBinder.Eval(Container, "DataItem.ORD_StatusUSASales"), DataBinder.Eval(Container, "DataItem.ORD_StatusUSAOPD")) %>' >

In code-behind:

  protected string FormatSalesOPDName(object ORD_StatusUSASales, object ORD_StatusUSAOPD)
    // Combine the names to get the Sales/OPD First format.
    return (string)ORD_StatusUSASales + "/" + (string)ORD_StatusUSAOPD;

Please HELP!
Old December 3rd, 2006, 04:58 AM
joefawcett's Avatar
Wrox Author
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts

You can write another protected function that returns the appropriate colour based on the rules for combined yes, no and cancelled. Then have ForeColor='<%# GetForeColorForStatus(DataBinder.Eval(Container, "DataItem.ORD_StatusUSASales"), DataBinder.Eval(Container, "DataItem.ORD_StatusUSAOPD")) %>'


Joe (Microsoft MVP - XML)

Similar Threads
Thread Thread Starter Forum Replies Last Post
back color of cell of datagrid rajessnit@gmail.com VB.NET 1 November 18th, 2006 03:28 AM
editing datagrid and changing cell color upon edit matoaka VS.NET 2002/2003 0 August 11th, 2006 11:04 AM
how to get font ,color,font size of text in win32 satishsatao Visual C++ 0 April 5th, 2006 05:05 AM
Font Color OzzyUltra Linux 1 October 22nd, 2004 10:01 AM
paint color cell in datagrid(c#) kobystud C# 0 August 1st, 2004 08:46 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.