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

Use of CellFormatting event

5 Answers 836 Views
GridView
This is a migrated thread and some comments may be shown as answers.
pierre-jean
Top achievements
Rank 1
Veteran
Iron
pierre-jean asked on 22 Oct 2019, 02:00 PM

Hello

I wonder if I make the correct use of the CellFormatting Event:

I have a grid that contains only one line

In the CellFormatting event I modify the cell text as following:

        If cell.ColumnInfo.Name = "licence_type_id" Then
            Dim LicenceType As wsWSTLicence.licence_type = wsLicenceTypes.Find(Function(x) x.id.Equals(cell.Value))
            cell.Text = LicenceType.Code & " (" & LicenceType.Description & ")"
            cell.TextAlignment = ContentAlignment.MiddleCenter
        End If

It works fine, however I realized that the code within the If Statement is called 6 times (which is the number of columns in my grid ..)
Is this normal ?
I would think that if my processing was heavy this would negatively impact the performance.

 

Also what is the difference between then CellFormatting Event and the ViewCellFormatting Event

 

Thanks in advance

Pierre-Jean

5 Answers, 1 is accepted

Sort by
0
LT
Top achievements
Rank 1
answered on 23 Oct 2019, 04:49 PM

Hi

Per this article here :

[quote]

Formatting non-data cells
The ViewCellFormatting event is fired for all cells. So if you want to format the grouping row or the header cells, you should use this event.

[/quote]

As for the CellFormatting being called multiple times, I have experienced the same thing. It is being called for each column in your row. You probably have 6 columns. The CellFormatting event is being called for each row[column]/cell in that row.

0
Nadya | Tech Support Engineer
Telerik team
answered on 24 Oct 2019, 08:38 AM

Hello,

Thank you for writing!

In order to achieve some visual customization the appropriate way is to make use of the CellFormatting/ViewCellFormatting events in RadGridView. Note that RadGridView uses UI virtualization which means that cell elements are created only for currently visible cells and are being reused during operations like scrolling, filtering, grouping and so on. Hence, the formatting event is fired every time the cell needs to be refreshed when interacting with the mouse/keyboard. Have in mind that it is absolutely normal that the more visual cell elements you have, the more number of calls the CellFormatting event will have. The event ensures that the proper style will be applied to the cells.

However, using this event should not lead to performance issues if you use it for its purpose only. For reference, I prepared a test project with 6 columns with 10 000 rows and set the TextAllignment property to MiddleCenter for all the cells in the grid. As it seems there is no slow performance issue. Could you please specify how many rows/columns you are expecting to have in your grid? How many visible cell elements you have on the form?

The main difference between these two events is that the CellFormatting event is fired for all data cells only while the ViewCellFormatting is fired for all non-data cells like header cells, grouping row cells, search row cells.

I hope this information is useful. Let me know if you need further assistance.

Regards,
Nadya
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.
0
pierre-jean
Top achievements
Rank 1
Veteran
Iron
answered on 25 Oct 2019, 07:10 AM

Hello and thanks for your replies

I understand that using the CellFormatting Event is the right thing to do to modify the cell text property,

However I am still puzzled by the number of calls to the CelleEvent Event.
I have taken your sample code and
1. changed the loop to have only one line in the grid:
                for (int i = 0; i < 1; i++)

2. I have added a debug.print statement in the cell formatting event:
            Debug.Print (e.CellElement.ColumnInfo.Name + "-" + e.CellElement.Value  );
Here below  the content of the debug window :

Is this normal ? and can you explain ?
Thanks a lot

---------------------------------------------------------------------------------

Name-Name 0
Electric car-YES
Make-Tesla
Model-X5
PartID-0
Year-2000
Name-Name 0
Electric car-YES
Make-Tesla
Model-X5
PartID-0
Year-2000
Name-Name 0
Electric car-YES
Make-Tesla
Model-X5
PartID-0
Year-2000
Name-Name 0
Electric car-YES
Make-Tesla
Model-X5
PartID-0
Year-2000
Name-Name 0
Electric car-YES
Make-Tesla
Model-X5
PartID-0
Year-2000
Name-Name 0
Electric car-YES
Make-Tesla
Model-X5
PartID-0
Year-2000
Name-Name 0
Electric car-YES
Make-Tesla
Model-X5
PartID-0
Year-2000
Name-Name 0
Electric car-YES
Make-Tesla
Model-X5
PartID-0
Year-2000
Name-
Name-
Electric car-
Electric car-
Make-
Make-
Model-
Model-
PartID-
PartID-
Year-
Year-
Name-Name 0
Name-Name 0
Electric car-YES
Electric car-YES
Make-Tesla
Make-Tesla
Model-X5
Model-X5
PartID-0
PartID-0
Year-2000
Year-2000
Name-Name 0
Electric car-YES
Make-Tesla
Model-X5
PartID-0
Year-2000
Name-Name 0
Electric car-YES
Make-Tesla
Model-X5
PartID-0
Year-2000
Name-Name 0
Electric car-YES
Make-Tesla
Model-X5
PartID-0
Year-2000
Name-Name 0
Electric car-YES
Make-Tesla
Model-X5
PartID-0
Year-2000
-------------------------------------------------------


 

0
Nadya | Tech Support Engineer
Telerik team
answered on 28 Oct 2019, 02:48 PM

Hello Pierre-Jean,

Yes, I confirm this behavior is normal. The CellFormatting event is expected to be fired a lot of times since it is responsible for the proper styling of the cell elements. If you use it for formatting purposes only it will not affect the performance. 

Please do not hesitate to contact us if you have any other questions or concerns.

Regards,
Nadya
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.
0
pierre-jean
Top achievements
Rank 1
Veteran
Iron
answered on 28 Oct 2019, 02:51 PM

Hello and thank you for your reply and the explanation.

Regards

Pierre-Jean

Tags
GridView
Asked by
pierre-jean
Top achievements
Rank 1
Veteran
Iron
Answers by
LT
Top achievements
Rank 1
Nadya | Tech Support Engineer
Telerik team
pierre-jean
Top achievements
Rank 1
Veteran
Iron
Share this question
or