Modify DataGrid cell color based on value from another cell

Posted on 2005-04-26
Last Modified: 2010-04-07
Hello, there are several similar postings on this subject, but I was not able to glean a solution for my own project from them.

What I am doing is building a simple database / app for a college course which sorts the named web-safe colors by red, then green, then blue.

Being new to, I tried to use the "ItemCreated" event of the datagrid, like this:

Private Sub DataGrid1_ItemCreated(ByVal sender As Object, _
            ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemCreated
      Dim dgi As DataGrid = sender
      Dim c As Color
      Dim strColor As String

      strColor = e.Item.Cells(5).Text
      Response.Write(strColor & "<BR>")
      e.Item.Cells(1).BackColor = c.FromName(strColor)
End Sub

But the problem is that strColor only gets a value from the first iteration, which is the header cell. What it's supposed to do is to take the color name from column #5, and assign it to column #1, which is just so that you can see the color in the datagrid.

If need be, I could instead use an RGB value from another cell, as that's available, too, from the database that populates the grid.

Or am I going about this wrong? The object is "web safe colors", but I noticed that my list is very similar to the colors in the System.Drawing.Colors Members list... Could I enumerate them, instead?

This project is not a direct assignment, so I'm not cheating by asking you to do my homework, but I'm offering 400 points because I do need understand this better!

Thanks in advance!
Question by:CoastalData
    LVL 3

    Author Comment

    LVL 3

    Author Comment

    Hey, I answered my own question! Here's the code that I ended up using:

    Private Sub DataGrid1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemDataBound
          Dim strColor As String = e.Item.Cells(5).Text
          Dim r, g, b As String
          r = (e.Item.Cells(1).Text)
          g = (e.Item.Cells(3).Text)
          b = (e.Item.Cells(2).Text)
                e.Item.Cells(0).BackColor = Color.FromArgb(r, g, b)
          Catch ex As Exception
          End Try
    End Sub

    I discovered I was using the wrong event, and also went to using r, g, and b values seperately.

    Thanks any how!!!!

    Accepted Solution

    PAQed with points refunded (400)

    Community Support Moderator

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
    ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now