BackColor row change when I scroll in gradview

8 posts, 0 answers
  1. Fernan
    Fernan avatar
    15 posts
    Member since:
    May 2010

    Posted 04 May 2010 Link to this post

    hi,

    I have a checkboxcolumn in my radgridview, and when the user set to true a cell, the backcolor of the current row changes . That's working fine. But when I scroll in my View, the backcolor of the row is moved to other row. The second row gets the backcolor of the firstrow and so on.

    The code is :

            private void radGridView1_ValueChanged(object sender, EventArgs e) 
            { 
                if (this.radGridView1.ActiveEditor is RadCheckBoxEditor) 
                { 
                    if ((bool)(this.radGridView1.ActiveEditor.Value)) 
                    { 
                        radGridView1.CurrentCell.RowElement.DrawFill = true
                        radGridView1.CurrentCell.RowElement.BackColor = Color.Red; 
                    } 
                } 
            } 


     What can the resolve this?
  2. Svett
    Admin
    Svett avatar
    728 posts

    Posted 05 May 2010 Link to this post

    Hi Fernan,

    The ValueChanged event is not appropriate to change the appearance of visual elements. The described behavior is caused by grid virtualization feature. RadGridView reuses visual elements (rows and cells) to increase its performance and decrease its memory footprint. You should use RowFormatting instead. You can use the following code to do that:

    private void radGridView_RowFormatting(object sender, RowFormattingEventArgs e)
    {
        GridViewDataRowInfo dataRow = e.RowElement.RowInfo as GridViewDataRowInfo;
     
        if (dataRow == null)
        {
            return;
        }
     
        object columnValue = dataRow.Cells["YourBoolColumn"].Value;
     
        if (columnValue != null && Convert.ToBoolean(columnValue))
        {
            e.RowElement.DrawFill = true;
            e.RowElement.BackColor = Color.Red;
        }
        else
        {
            e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
            e.RowElement.ResetValue(LightVisualElement.BackColorProperty, ValueResetFlags.Local);
        }
     
    }


    Sincerely yours,
    Svett
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  3. UI for WinForms is Visual Studio 2017 Ready
  4. Fernan
    Fernan avatar
    15 posts
    Member since:
    May 2010

    Posted 06 May 2010 Link to this post

    Thank you,

    I've other problem: I need change the row's forecolor.  Then, I change your code for:

    private void radGridView1_RowFormatting(object sender, RowFormattingEventArgs e) 
            { 
                GridViewDataRowInfo dataRow = e.RowElement.RowInfo as GridViewDataRowInfo; 
     
                if (dataRow == null
                { 
                    return
                } 
     
                object columnValue = dataRow.Cells["column1"].Value; 
     
                if (columnValue != DBNull.Value && columnValue!= null && Convert.ToBoolean(columnValue)) 
                { 
                    e.RowElement.DrawFill = true
                    e.RowElement.ForeColor = Color.Red; 

                } 
                else 
                { 
                    e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local); 
                    e.RowElement.ResetValue(LightVisualElement.ForeColorProperty, ValueResetFlags.Local); 
                }             
            } 

    The problem is: The text in the row continue being black color. ¿why?

    Thank you,

  5. Fernan
    Fernan avatar
    15 posts
    Member since:
    May 2010

    Posted 10 May 2010 Link to this post

    Hi,

    Help me please, I need solution this problem. I have to change the row's forecolor, but e.rowelement.forecolor don't change the text color.
  6. Svett
    Admin
    Svett avatar
    728 posts

    Posted 10 May 2010 Link to this post

    Hi Fernan,

    If you need to change the fore color, you have to change cell's fore color instead of row fore color. You can do that in CellFormatting event. You can use the following code snippet as sample:

    private void radGridView_CellFormatting(object sender, CellFormattingEventArgs e)
    {
        GridViewDataRowInfo dataRow = e.CellElement.RowInfo as GridViewDataRowInfo;
     
        if (dataRow == null)
        {
            return;
        }
     
        object columnValue = dataRow.Cells["column1"].Value;
     
        if (columnValue != DBNull.Value && columnValue != null && Convert.ToBoolean(columnValue))
        {
            e.CellElement.ForeColor = Color.Red;
     
        }
        else
        {
            e.CellElement.ResetValue(LightVisualElement.ForeColorProperty, ValueResetFlags.Local);
        }
    }

    A bit off topic, please ask the person who has purchased our controls in your company to add you as a License Developer to the purchase. This will give you full access to the products your company has purchased, to our downloads section, and to our support ticketing system. Additionally, all your questions will be reviewed according to the license you have. More information on License Developers you can find here: www.telerik.com/account/faqs.aspx.

     
    Sincerely yours,
    Svett
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  7. Fernan
    Fernan avatar
    15 posts
    Member since:
    May 2010

    Posted 11 May 2010 Link to this post

    Hi Svett,

    Thank you, but I have other problem with your code. I need each row, whose cell is check to true, have a different color. However, when I set to true the second, third,... row, all the rows, whose cell is check to true , are setting to the same color, exactly to the last select color.

    How do I this property?


    Thank you,

    Fernan
  8. Svett
    Admin
    Svett avatar
    728 posts

    Posted 14 May 2010 Link to this post

    Hello Fernan,

    You can use the Tag property of GridViewRowInfo to store the desired color. Then you can get and apply it to the visual row element. You can use the following code snippet:
    private void radGridView_RowFormatting(object sender, RowFormattingEventArgs e)
    {
        GridViewDataRowInfo dataRow = e.RowElement.RowInfo as GridViewDataRowInfo;
     
        if (dataRow == null)
        {
            return;
        }
     
        object columnValue = dataRow.Cells["column1"].Value;
     
        if (columnValue != DBNull.Value && columnValue != null && Convert.ToBoolean(columnValue))
        {
            if (e.RowElement.RowInfo.Tag == null)
            {
                e.RowElement.RowInfo.Tag = this.GetNextBackGroundColor();
            }
     
            e.RowElement.DrawFill = true;
            e.RowElement.BackColor = (Color)e.RowElement.RowInfo.Tag;
        }
        else
        {
            e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, ValueResetFlags.Local);
            e.RowElement.ResetValue(LightVisualElement.ForeColorProperty, ValueResetFlags.Local);
        }
    }
     
    private Color GetNextBackGroundColor()
    {
        // This is factory method that produces the next new background color
    }

    You only need to implement your algorithm for determining the different background colors. You have to place it inside GetNextBackGroundColor method.

    All the best,
    Svett
    the Telerik team

    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
  9. Rahul
    Rahul avatar
    1 posts
    Member since:
    May 2014

    Posted 25 Feb 2015 in reply to Svett Link to this post

    It's very good code working fine 

    Thanks lot
Back to Top
UI for WinForms is Visual Studio 2017 Ready