locked
Calendar Cell Color Change RRS feed

  • Question

  • So I'm using VB.NET 2005 to create an ASP page.  I have a calendar object and I need to find a way to change the BackColor of the cell from a database table.  I've tried every way I can think of with no luck so far.  This is what I have for code so far which works for some situations but not for all the ones I would like and to hard code it would be very tedious.

    If e.Day.Date.Day = CDate(strDay).Day And e.Day.Date.Month = CDate(strDay).Month Then
              e.Cell.Controls.Add(New LiteralControl(strHoliday))
              e.Cell.BorderColor = Drawing.Color.Black
              e.Cell.BorderWidth = 1
              e.Cell.BorderStyle = BorderStyle.Solid
              If bolOff = True Then
                e.Cell.BackColor = Drawing.Color.Crimson
              Else
                e.Cell.BackColor = Drawing.Color.AliceBlue
              End If
    
            End If
    
    I've tried creating a Drawing.Color variable and then having (for example) AliceBlue in the table, Drawing.Color.AliceBlue, Color.AliceBlue.  I've tried making it a string and that doesn't work.
    Friday, October 22, 2010 12:06 PM

Answers

  • Color is a Structure, but you can convert it into an integer that you could store in your database

    Dim colorValue As Integer = Drawing.Color.Crimson.ToArgb()
    When you want to use the value from the database, you can convert it back to a color.

    e.Cell.BackColor = Drawing.Color.FromArgb(colorValue)
    

    • Marked as answer by temlehdrol Friday, October 22, 2010 2:41 PM
    Friday, October 22, 2010 1:27 PM

All replies

  • What is the problem you are seeing? Does the BackColor not change?
    Friday, October 22, 2010 12:30 PM
  • Sorry, generally it actually leaves the entire cell blank with just the date and none of the info from the database.  I can't remember which way but one of them it just leaves the background white.  Or I get a error saying in the case of it being a string that it's not the right type which is expected.
    Friday, October 22, 2010 12:56 PM
  • I believe the code that you posted should change the BackColor of the cell as long as the If statement in the first line is true. So perhaps the problem is that the test is never true. Try setting a breakpoint on that statement, and check the values of e.Day.Date and strDate to make sure they are what you expect them to be, then check that you actually execute the code in the Then clause.
    Friday, October 22, 2010 1:10 PM
  • Oh sorry, I guess I wasn't clear... the code does work, but I'd rather not "hard code" the color.  I'd prefer it to be something along the lines of:

    e.Cell.BackColor = colorVariable (from SQL Table)

    Guess that is what I get for doing way too many things at once, again sorry for not being clear.

    Friday, October 22, 2010 1:14 PM
  • Color is a Structure, but you can convert it into an integer that you could store in your database

    Dim colorValue As Integer = Drawing.Color.Crimson.ToArgb()
    When you want to use the value from the database, you can convert it back to a color.

    e.Cell.BackColor = Drawing.Color.FromArgb(colorValue)
    

    • Marked as answer by temlehdrol Friday, October 22, 2010 2:41 PM
    Friday, October 22, 2010 1:27 PM
  • Ok that works, though I do have a question.  As of right now I have to put for example:
    Dim colorValue as Integer = Drawing.Color.Crimson.ToArgb() with a break point to get the value of -235116.

    I can create a page to list colors and their values or something, it should work.

    Friday, October 22, 2010 2:16 PM
  • Do you mean you are using the break point to find out the Argb value of Crimson is -235116 and then you manually enter -235116 in your database?

    Couldn't you write code to update the database with the color values you want to be able to use?

    Friday, October 22, 2010 2:25 PM
  • I created a "admin" page to do just that.  Basically lists what is in the Database and allows me to make changes.  Got to thinking about it after my last post... Thanks for your help.
    Friday, October 22, 2010 2:41 PM