set background and foreground color on specified cell

8 posts, 0 answers
  1. pierre-jean
    pierre-jean avatar
    135 posts
    Member since:
    Apr 2011

    Posted 01 Apr Link to this post

    Hello I need to programmatically change the colors of a cell specified by its index , i.e. not the selected cell.

    I know that the colors can/must be set on the cellelement but I have not found how to get the cellelement of a cell by it's index (or column name) in the currentrow

        e.currentrow(7).cellelement does not exists

    and I have tried   e.CurrentRow.Cells(7).style.BackColor=Color.Blue but it has no effects

    Thanks in advance

    Pierre-Jean

  2. Nadya
    Admin
    Nadya avatar
    344 posts

    Posted 01 Apr Link to this post

    Hello Pierre-Jean,

    The appropriate place where you can introduce visual customizations of the cells in RadGridView is the CellFormatting event. When using the CellFormatting event, you have access to the Row, RowIndex as well as Column, and ColumnName properties. Please refer to the following example:

    private void RadGridView1_CellFormatting(object sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
            {
                if (e.Row.IsCurrent && e.ColumnIndex == 1 && e.RowIndex ==2)
                {
                    e.CellElement.BackColor = Color.Yellow;
                    e.CellElement.DrawFill = true;
                    e.CellElement.GradientStyle = GradientStyles.Solid;
                    e.CellElement.ForeColor = Color.Red;
                }
                else
                {
                    e.CellElement.ResetValue(LightVisualElement.BackColorProperty, Telerik.WinControls.ValueResetFlags.Local);
                    e.CellElement.ResetValue(LightVisualElement.DrawFillProperty, Telerik.WinControls.ValueResetFlags.Local);
                    e.CellElement.ResetValue(LightVisualElement.GradientStyleProperty, Telerik.WinControls.ValueResetFlags.Local);
                    e.CellElement.ResetValue(VisualElement.ForeColorProperty, ValueResetFlags.Local);
                }
            }

    More information about formatting cells is available here

    I hope this helps. Should you have further questions I will be glad to help.

    Regards,
    Nadya
    Progress Telerik

    Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
    Our thoughts here at Progress are with those affected by the outbreak.
  3. pierre-jean
    pierre-jean avatar
    135 posts
    Member since:
    Apr 2011

    Posted 01 Apr in reply to Nadya Link to this post

    Hello Nadya

    Thanks for your prompt response however I think that it is not the solution I am seeking
    Your solution permits to change the cell fore and back color of the current cell (nad I already do this i another context)
    While what  I am looking for is a way to change the color of a cell outside of the gridview event, just by knowing the cell location (row and cell in the row) or changing a cell in the currently selected row but not the selected cell
    … but this may not be possible ...
    Thanks

     

     

  4. Nadya
    Admin
    Nadya avatar
    344 posts

    Posted 03 Apr Link to this post

    Hello Piere-Jean,

    I am sorry to hear that this solution is not appropriate for you.

    Actually it is possible to set styles to cells in runtime without using events like CellFormatting or RowFormatting. The GridViewCellInfo.Style property gives direct access to the cell’s visual properties. You can define first what visual setting should the cell use by setting CustomizeFill or CustomizeBorder properties and then apply the customizations to a specified cell by using the Rows and Cells collections.

    You can find more information about using the Style property here. Could you please refer to it and see if this is suitable for your scenario.

    I hope this helps. Do not hesitate to contact me in case of any difficulties.

    Regards,
    Nadya
    Progress Telerik

    Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
    Our thoughts here at Progress are with those affected by the outbreak.
  5. pierre-jean
    pierre-jean avatar
    135 posts
    Member since:
    Apr 2011

    Posted 24 Apr in reply to Nadya Link to this post

    Hello Nadya
    Thanks for your answer
    I can get access to the cell and it’s style property, It does not, however, do quite the job
    Here is my code snippet

            Dim HorseCell As GridViewCellInfo = gvEntry.CurrentRow.Cells("CurrentName")
            HorseCell.Style.CustomizeFill = True
            HorseCell.Style.ForeColor = Color.Red

    This does not change the color of the text
    However if I change the BackColor :

            Dim HorseCell As GridViewCellInfo = gvEntry.CurrentRow.Cells("CurrentName")
            HorseCell.Style.CustomizeFill = True
            HorseCell.Style.GradientStyle = GradientStyles.Solid
            HorseCell.Style.BackColor = Color.Red

    The backcolor does get Red but only after changing focus to another row or another cell

    Can you tell me why I can not change the text (forecolor)
    How to have the change take effect immediately and not after lost of focus

    Thanks in advance
    Pierre Jean
  6. Nadya
    Admin
    Nadya avatar
    344 posts

    Posted 27 Apr Link to this post

    Hello Pierre-Jean,

    Following the provided information I was not able to reproduce the issues that you described. The text color on my side is colored correctly and the back color is applied immediately when the grid is shown. I attached my sample project for your reference. Could you please refer to it and let me know how it differs from your real set up. 

    I am looking forward to your reply.

    Regards,
    Nadya
    Progress Telerik

    Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
    Our thoughts here at Progress are with those affected by the outbreak.
  7. pierre-jean
    pierre-jean avatar
    135 posts
    Member since:
    Apr 2011

    Posted 28 Apr Link to this post

    Hello Nadya

    Thanks for your reply,

    Indeed your test program does the expected job, with one unexpected behavior, if I remove the "setbackcolor", the cell background of the cell becomes darkgrey 

    However I still have the problem in my Program ...

    Here is the code I am using

            Dim HorseCell As GridViewCellInfo = gvEntry.CurrentRow.Cells("CurrentName")

            HorseCell.Style.CustomizeFill = True
            HorseCell.Style.BackColor = Color.Yellow
            HorseCell.Style.ForeColor = Color.Red

     

    This code is invoked on the Click event of a popup menu:
    1. I need to click on another cell or row of the grid to see the change
    2. The BackColor does change to Yellow
    3. The forecolor does not change and remains black

    If I remove the Backcolor= Color.Yellow line the cell backcolor will be drak grey as in your test program

    I don't know if it is relevant but my grid is databound to a database datasource.

    If you have no other suggestion I'll create a standalone project to demonstrate the problem

    Best Regards

    Pierre-Jean 

     

     

  8. Nadya
    Admin
    Nadya avatar
    344 posts

    Posted 30 Apr Link to this post

    Hello Perre-Jean,

    Note, if you want to customize the BackColor of the cell via the Style property it is necessary to set CustomizeFill to true. Thus, the specified color will be set. If you set just CustomizeFill to true without setting the BackColor of the style object, the default dark gray color will be set which is the one that you see. If you do not want to set any color to the BackColor just set the CustomizeFill to false.

    According to the problem that you have when applying ForeColor/BackColor I was not able to reproduce it on my end. As you can see from the attached project everything works fine on my end. This is why I would kindly ask you to modify the project in order to reproduce the problem that you are facing. Thus, we will be able to investigate the precise case and assist you further.

    I am looking forward to your reply.

    Regards,
    Nadya
    Progress Telerik

    Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
    Our thoughts here at Progress are with those affected by the outbreak.
Back to Top