cells forecolor and backcolor taking random values on refresh

9 posts, 2 answers
  1. Yaroslav
    Yaroslav avatar
    24 posts
    Member since:
    Nov 2011

    Posted 27 Feb 2012 Link to this post

    Hi, using the sample provided on the link GridView, formatting cells I'm trying to dinamically change cells foreground.

    The color is changed indeed but then I'm having a big problem. When I scroll the grid, either horizontally or vertically, diferent cells, other than the one I changed, change also their foreground color to the one I'm using. Same problem with the background color of the cell.

    Here is a code snippet showing the funcionality I'm trying to achieve, very similar to the sample provided on the link.

    Select Case e.Column.Name
        Case "TestID"
           Select Case e.Row.Cells("Status").Value
                Case 1
                    e.CellElement.ForeColor = Color.Green
                Case Else
                    e.CellElement.ResetValue(LightVisualElement.ForeColorProperty, Telerik.WinControls.ValueResetFlags.Local)
        End Select
    End Select
  2. Answer
    erwin
    erwin avatar
    358 posts
    Member since:
    Dec 2006

    Posted 27 Feb 2012 Link to this post

    Hi Yaroslav,

    due to the internal reuse of Cells in the Grid, once you use the CellFormatting Event, you have to reset changes to all cells.

    In your case, you have to call the Reset Function for all Cells not just for Column 'TestID'.

    Regards
    Erwin

  3. UI for WinForms is Visual Studio 2017 Ready
  4. Yaroslav
    Yaroslav avatar
    24 posts
    Member since:
    Nov 2011

    Posted 27 Feb 2012 Link to this post

    Thanks, you saved my day.

    And just to extend a little bit more the question. I created a enum set to declare several colors, here is a code snippet
        Public Enum ColorStatus
            First = &HFF1308&
            Second = &HFFFFFF&
        End Enum
     
    e.CellElement.RowInfo.Cells("Status").Style.ForeColor = System.Drawing.ColorTranslator.FromHtml(ColorStatus.First)

    Then I apply one of the colors to a cell (the one that was causing problems) but the ColorTranslator.FromHtml is not recognizing the colors. Any clue?
  5. erwin
    erwin avatar
    358 posts
    Member since:
    Dec 2006

    Posted 27 Feb 2012 Link to this post

    Glad I could be of help. Can't help you with the second question since I'm a C# guy.

    According to
    http://msdn.microsoft.com/de-de/library/system.drawing.colortranslator.fromhtml.aspx
    you have to supply a string value.

    Regards
    Erwin

  6. Yaroslav
    Yaroslav avatar
    24 posts
    Member since:
    Nov 2011

    Posted 28 Feb 2012 Link to this post

    Thanks again, I will give it a try
  7. Answer
    Ivan Petrov
    Admin
    Ivan Petrov avatar
    701 posts

    Posted 01 Mar 2012 Link to this post

    Hello guys,

    Thank you both for writing.

    I am glad you have been able to found your way through Yaroslav's first problem. 
    On the second problem. First, some background. A color value needs 32 bits to store its value. Integer values are 32 bits, but one is reserved for the sign and an integer cannot store all possible color values. You can store only the RGB values of the color in an int, but when you format it into a color string, it will be transformed with leading zeroes which is how it is represented in memory and you will get a transparent color. For this purpose you need a unsigned integer where all 32 bits are used for storing data. Unfortunately, you cannot use unsigned integers as values for an enumeration. A possible solution would be to store the RGB values in a enumeration and then to add it to a unsigned integer with leading FF instead of 00 so you would get the actual color. Here is an example of this:
    Enum ColorStatus
      First = &Hff1308
      Second = &Hffffff
    End Enum
     
    Dim colorValue As UInteger = &HFF000000UI + CInt(ColorStatus.First)
    e.CellElement.RowInfo.Cells("Status").Style.ForeColor = System.Drawing.ColorTranslator.FromHtml(String.Format("#{0:X}", colorValue))

    I hope this will be useful for you. Should you have further questions, I would be glad to help.

    Greetings,
    Ivan Petrov
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
  8. Yaroslav
    Yaroslav avatar
    24 posts
    Member since:
    Nov 2011

    Posted 04 Apr 2012 Link to this post

    Hi, I unsubscribed from this thread but came back to it to refresh the reset issue and saw the Ivan Petrov answer...thanks very much! it is perfectly working now, I can use enumerates to handle my different status colors for all the app.
  9. Yaroslav
    Yaroslav avatar
    24 posts
    Member since:
    Nov 2011

    Posted 09 Apr 2012 Link to this post

    I'm having again the refresh issue on my grids. Whenever I scroll right and then left many of the icons I'm displaying on the cells are repeated on other cells. This is causing me problems as not only is drawing icons where there none but also where I have inactive cells (grayed icon) is painting the enabled icon, but the button remains grayed.

    Tried ther reset recommendation from Erwin but is not working.

    Thanks!
  10. Stefan
    Admin
    Stefan avatar
    2891 posts

    Posted 12 Apr 2012 Link to this post

    Hello Yaroslav,

    Thank you for writing back.

    The ResetValue method should reset the styles for the cells and prevent such a behavior. Since you are mentioning this does not work in your case, I will need to ask you for a sample project demonstrating this issue, so I can investigate it locally and provide you with adequate support. You will need to open a new support ticket, where attachments are allowed. Please mention this forum thread in the ticket as well.
     
    Regards,
    Stefan
    the Telerik team
    RadControls for WinForms Q1'12 release is now live! Check out what's new or download a free trial >>
Back to Top
UI for WinForms is Visual Studio 2017 Ready