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

[Solved] Changing row colors in RowLoaded event not working

4 Answers 181 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Barry
Top achievements
Rank 1
Barry asked on 25 Sep 2010, 05:32 AM

 

Hello,

I am currently modifying row colors in my Grid when the row content meets the specific text that I want. The code works perfectly when the data is not that big. But if I have sufficient number of rows that triggers a scroll bar, every time I scroll down, the color gets assigned randomly to the new rows even if the row did not meet the criteria.

Below is the code.


private
void gvMain_RowLoaded(object sender, RowLoadedEventArgs e)

 

{

 

 

var a = e.Row as GridViewRow;

 

 

if (a != null)

 

{

 

if ((a.Cells[0].Content.ToString() == "Tittle1") || (a.Cells[0].Content.ToString() == "Tittle2") || (a.Cells[0].Content.ToString() == "Tittle3")||(a.Cells[0].Content.ToString()=="Tittle4"))

 

{

a.Height = 35;

a.Background =

new SolidColorBrush(Colors.LightGray);

 

a.FontSize = 15;

a.FontWeight=

FontWeights.Bold;

 

}

 

else if

 

 

//Set row color of cells

 

((a.Cells[0].Content.ToString() ==

"Private") || (a.Cells[0].Content.ToString() == "Public"))

 

{

a.Height = 25;

a.Background =

new SolidColorBrush(Colors.Blue);

 

a.FontSize = 12;

a.FontWeight =

FontWeights.Bold;

 

}

 

else

 

{

a.FontWeight =

FontWeights.Bold;

 

}

 

}

 

}


Please advice

4 Answers, 1 is accepted

Sort by
0
Yavor Georgiev
Telerik team
answered on 25 Sep 2010, 11:38 AM
Hello Barry,

 This happens because of virtualization - in order to preserve memory, rows are recycled so that if you set an explicit property on a row and this row is recycled, the property won't be cleared. I suggest you use the RowStyleSelector property of RadGridView with a custom StyleSelector.

Greetings,
Yavor Georgiev
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
JR
Top achievements
Rank 1
answered on 27 Sep 2010, 11:22 AM
Hello Yavor, 

    Are there other ways to go around the issue other than using the RowStyleSelector property? I'm asking because im using an older version of the control. Base from http://blogs.telerik.com/vladimirenchev/posts/10-04-01/conditional_styles_and_templates_with_radgridview_for_silverlight_and_wpf.aspx, StyleSelector was added Q1 2010 Service Pack 1.

Thanks in Advance.
0
Yavor Georgiev
Telerik team
answered on 27 Sep 2010, 12:11 PM
Hello JR,

 You can use RowLoaded to set the color, but you have to reset the Background property first. Example:
var row = e.Row as GridViewRow;
row.Background = null;
  
if (myPredicate)
{
   row.Background = myBrush;
}


Regards,
Yavor Georgiev
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
JR
Top achievements
Rank 1
answered on 27 Sep 2010, 01:51 PM

I made i little modification and the code worked. For some reason i need to check if the row is not null otherwise it will encounter a nullexception error. Thanks Yavor for the help.

var a = e.Row as GridViewRow;
  
if (a != null)
{
    a.Background = null;
    if(myPredicate)
    {
         row.Background = myBrush;
    }
}

            

Tags
GridView
Asked by
Barry
Top achievements
Rank 1
Answers by
Yavor Georgiev
Telerik team
JR
Top achievements
Rank 1
Share this question
or