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

CellFormatting/RowFormatting and performance

3 Answers 349 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Johannes
Top achievements
Rank 1
Johannes asked on 24 Jan 2014, 08:41 AM

I have a lot of RadGridViews inside my applications and as long as there are columns of type GridViewTextBoxColumn only I never encountered perfomance issues. Now I have to do some formatting (very simple logic in my opinion, see code below) and my RadGridView is much slower, especially scrolling.



I tried using CellFormatting and RowFormatting event (not the same time) but both events result in poor performance. Also I am wondering why the documentation recommends using CellFormatting instead of RowFormatting. I have to display 1000-10000 records with 15 columns. Isn't RowFormatting executed one time per (visible) row while CellFormatting will be executed 15 times per row?



Here is my RowFormatting code. Of course the code differs a little bit when I use it in CellFormatting but the idea is the same. All I want to do is set some Images:



// Check column "EventTypeID" and set corresponding icon in column "EventTypeImage"
 
var cell = row.Cells["EventTypeImage"];
 
if (cell != null && cell.Value == null)
{
  switch ((byte) row.Cells["EventTypeID"].Value)
  {
    case (byte) EEventType.Error:
    {
      cell.Value = Resources.ErrorIcon;
      break;
    }
 
    // other cases
  }
}
 
// 4 more GridViewTextBoxColumns that I check and set corresponding icon in a GridViewImageColumn

Performance is good as long as there are few records. But beginning with 500+ rows I have to deal with big performance impacts. I also encountered some crazy behavior with CellFormatting. When I put a counter in my code and write this counter to console whenever CellFormatting event is raised the event never stops from raising so the application hangs forever. I don't know whats going on there, why is this event raising endless just because I write down some debug info to console?



I am using latest version of RadControls for WinForms Q3.

3 Answers, 1 is accepted

Sort by
0
George
Telerik team
answered on 29 Jan 2014, 09:48 AM
Hello Johannes,

Thank you for contacting us.

RadGridView uses virtualization, which means that the Visible Cells are being reused and synchronized with their corresponding Data Cells. The RowFormatting event is used to format the Visual Row Element. Accessing the cells (as you did) from the row returns the Data Cells, which should not be modified in Formatting events, since it can cause performance issues (as in your case). With that said, your assumption that RowFormatting will be fired once is correct, but does not provide a way to format the Visual Cells.

As a solution to this case, I would recommend you to set the values of the cells outside of the Formatting events. If you have further difficulties, please provide me with further information about your case and I will do my best to help you.

I hope this helps.

Regards,
George
Telerik
TRY TELERIK'S NEWEST PRODUCT - APPLICATION ANALYTICS for WINFORMS.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
0
Johannes
Top achievements
Rank 1
answered on 07 Feb 2014, 10:29 AM
Thank you for your answer. I tried like you suggested. I removed the formatting events and instead I used a method to set the images on the columns ONE TIME (directly after data is read and bound to GridView). However with my code snippet from above I still encountered very poor perfomance so I switched back to CellFormatting event.

CellFormatting event is very slow with this line of code:
e.CellElement.Value = Resources.ErrorIcon; // Set value of current cell. This tooks minutes to complete.

If above line is replaced with this line everything runs without any perfomance issues:
e.CellElement.Image = Resources.ErrorIcon; // Set image of current cell. Seems to work instantly.


I don't understand what's the difference maker here but my RadGridView displays the mentioned images very fast now.
0
George
Telerik team
answered on 12 Feb 2014, 09:34 AM
Hi Johannes,

Thank you for replying.

In order to display images properly I suggest to you to bind RadGridView to some data source, since then the grid will optimally set the Image of the cells and will not cause performance issues. Оn the other hand setting the Image property sets the Image only to the cell, which explains why it is fast. However, setting the Value of the cell causes additional formatting events to fire, which explains the performance issues. An example of binding RadGridView so images can be set follows:
public class ImageDummy
{
    public Image Image
    {
        get
        {
            return Properties.Resources.btn1;
        }
    }
}
 
List<ImageDummy> source = new List<ImageDummy>();
for (int i = 0; i < 1000; i++)
{
    source.Add(new ImageDummy());
}
 
this.Grid.DataSource = source;
this.Grid.AutoSizeColumnsMode = GridViewAutoSizeColumnsMode.Fill;

You can also read more about GridViewImageColumn here.

I hope that this was the information that you were looking for. I wish you a great day!

Regards,
George
Telerik

Check out the new Telerik Platform - the only modular platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native apps. Register for the free online keynote and webinar to learn more about the Platform on Wednesday, February 12, 2014 at 11:00 a.m. ET (8:00 a.m. PT).

Tags
GridView
Asked by
Johannes
Top achievements
Rank 1
Answers by
George
Telerik team
Johannes
Top achievements
Rank 1
Share this question
or