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

RowFormatting confusion

4 Answers 114 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 18 Feb 2011, 04:29 PM
I am evaluating the Telerik winform controls, and have special interest in the GridView. In evaluating, I am using the RowFormatting event to change the font of the row on all even rows (to look at performance and usability). I have a very simple datasource with 1000 records. I have to admit that so far I have been impressed with the ease of handling when it comes to the grid and it's properties.

Then I came upon the RowFormatting event. Works awesome!... until I have to scroll. Then it goes haywire and ends up applying the formatting to every row. How do I fix this... or how do I apply my formatting correctly? My code is very simple:

private void _billGrid_RowFormatting(object sender, Telerik.WinControls.UI.RowFormattingEventArgs e)
{
       if (e.RowElement.RowInfo.Index % 2 == 0)
       {
           e.RowElement.Font = new Font("Tahoma", 14, FontStyle.Bold);
       }
}

4 Answers, 1 is accepted

Sort by
0
Accepted
Richard Slade
Top achievements
Rank 2
answered on 18 Feb 2011, 04:42 PM
Hello Chris,

Only a slight change to be made. You need to reset the font property if it doesn't meet your criteria
private void radGridView1_RowFormatting(object sender, Telerik.WinControls.UI.RowFormattingEventArgs e)
{
    if (e.RowElement.RowInfo.Index % 2 == 0) { e.RowElement.Font = new Font("Tahoma", 14, FontStyle.Bold); }
    else
    { e.RowElement.ResetValue(LightVisualElement.FontProperty); }
}

This is because the RadGridView uses UI Virtualization to essentially "re-use" cells as you scroll up and down the grid. For more information, see the documentation for Row Formatting and logical vs visual structure

Hope that helps
Richard
0
Chris
Top achievements
Rank 1
answered on 18 Feb 2011, 04:53 PM
Awesome! That did it. But, for my understanding, the RowFormatting event tries to apply formatting to every row, which is why the else was required, and all because of the UI Virtualization.
0
Richard Slade
Top achievements
Rank 2
answered on 18 Feb 2011, 04:59 PM
Hi,

Yes, basically think of it in that data is only rendered for visible cells. When you scroll through the data, instead of the rows and cells moving, the cells basically stay static and the data is moved into them. It's the simplest way I can think to explain it. (the links I added before explain it fully).
Glad that helped but let me know if you need more information
Richard
0
Richard Slade
Top achievements
Rank 2
answered on 18 Feb 2011, 05:00 PM
Sorry, one more thing. Please remember to mark as answer so others can find the solution too
Thanks again
Richard
Tags
GridView
Asked by
Chris
Top achievements
Rank 1
Answers by
Richard Slade
Top achievements
Rank 2
Chris
Top achievements
Rank 1
Share this question
or