This is a migrated thread and some comments may be shown as answers.

BackColor row change when I scroll in gradview

10 Answers 442 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Fernan
Top achievements
Rank 1
Fernan asked on 04 May 2010, 12:25 PM
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?

10 Answers, 1 is accepted

Sort by
0
Svett
Telerik team
answered on 05 May 2010, 11:56 AM
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.
0
Fernan
Top achievements
Rank 1
answered on 06 May 2010, 01:47 PM
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,

0
Fernan
Top achievements
Rank 1
answered on 10 May 2010, 01:33 PM
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.
0
Svett
Telerik team
answered on 10 May 2010, 03:54 PM
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.
0
Fernan
Top achievements
Rank 1
answered on 11 May 2010, 11:28 AM
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
0
Svett
Telerik team
answered on 14 May 2010, 01:48 PM
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.
0
Rahul
Top achievements
Rank 1
answered on 25 Feb 2015, 10:40 AM
It's very good code working fine 

Thanks lot
0
Michael
Top achievements
Rank 1
answered on 14 Nov 2018, 07:06 PM

I am having the same problem. Using rowFormatting does not solve this for me. The row color sticks when scrolling.

 

        private void gvFumigation_RowFormatting(object sender, RowFormattingEventArgs e)
        {
            GridViewDataRowInfo dataRow = e.RowElement.RowInfo as GridViewDataRowInfo;

            if (dataRow == null)
            {
                return;
            }

            if (dataRow.Cells["CompleteDate"].Value == null)
                return;

            if (dataRow.Cells["CutDate"].Value == null)
                return;

            //Get cut and release date
            DateTime releaseDate = new DateTime();
            DateTime cutDate = new DateTime();

            releaseDate = (DateTime)dataRow.Cells["CompleteDate"].Value;
            cutDate = (DateTime)dataRow.Cells["CutDate"].Value;

            //Get release date at 3pm
            TimeSpan span3PM = new TimeSpan(15,0,0);
            DateTime release3PM = new DateTime();
            
            release3PM = releaseDate.Date.Add(span3PM);

            //If release date is after cut date, highlight row red.
            if (releaseDate > cutDate)
            {
                e.RowElement.DrawFill = true;
                e.RowElement.GradientStyle = GradientStyles.Solid;
                e.RowElement.BackColor = Color.Red;
            }
            //If release date is the day before cut date and after 3pm, highlight row yellow
            else if (releaseDate.Date == cutDate.Date.AddDays(-1))
            {
                if (releaseDate >= release3PM)
                {
                    e.RowElement.DrawFill = true;
                    e.RowElement.GradientStyle = GradientStyles.Solid;
                    e.RowElement.BackColor = Color.Yellow;
                }
            }
            else
            {
                //leave everything as is
                e.RowElement.ResetValue(LightVisualElement.BackColorProperty, Telerik.WinControls.ValueResetFlags.Local);
                e.RowElement.ResetValue(LightVisualElement.GradientStyleProperty, Telerik.WinControls.ValueResetFlags.Local);
                e.RowElement.ResetValue(LightVisualElement.DrawFillProperty, Telerik.WinControls.ValueResetFlags.Local);
            }
        }

0
Michael
Top achievements
Rank 1
answered on 14 Nov 2018, 09:00 PM
Nevermind. I figured it out. I needed to reset the cell values if the columns I was checking on was null.
0
Dess | Tech Support Engineer, Principal
Telerik team
answered on 15 Nov 2018, 11:25 AM
Hello, Michael, 

I am glad that the problem you were facing is now resolved. However, in case someone from the community faces a similar problem it would be good to have a look at the following help articles for further information: 

https://docs.telerik.com/devtools/winforms/gridview/cells/formatting-cells
https://docs.telerik.com/devtools/winforms/gridview/rows/formatting-rows

I hope this information helps. 

Regards,
Dess | Tech Support Engineer, Sr.
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
GridView
Asked by
Fernan
Top achievements
Rank 1
Answers by
Svett
Telerik team
Fernan
Top achievements
Rank 1
Rahul
Top achievements
Rank 1
Michael
Top achievements
Rank 1
Dess | Tech Support Engineer, Principal
Telerik team
Share this question
or