Hi,
I'm using Version 2011.1.11.419, and have been tasked to do some performance tuning I noticed that the Cellformatting event gets called a bit more often than I would like, especially at form loading. I found that if I place the code to set the datasource for the radGridView in the load event of the form, that it gets called much more frequently than if placed in the forms constructor.
In the code snippets below, I'm creating a simple grid with 5 columns and 2 rows - all of type GridTextBoxColumn. When assigning the datasource in the constructor, it generates 25 calls to the CellFormatting event. If I move the datasource assignment to the form load event, it generates 35 calls. The problem is even more pronounced when I call the radGridView.LoadLayout function. If placed in the constructor, it still calls the CellFormatting event 25 times, but when I move both the datasource and LoadLayout calls to the form load event, CellFormatting gets called a whopping 90 times! Note that the xml file that I'm using is nothing crazy - simply the output from saving the contents of this grid in Visual Studio using the radGridView property builder.
I've tried to use radGridView.BeginInit, and radGridView.SuspendLayout to decrease the number of calls while the grid is being set up, but neither seem to have any effect. Should I resign myself to moving radGridView code to the forms constructor, or is there a way to accomplish this in the form load event?
I'm using Version 2011.1.11.419, and have been tasked to do some performance tuning I noticed that the Cellformatting event gets called a bit more often than I would like, especially at form loading. I found that if I place the code to set the datasource for the radGridView in the load event of the form, that it gets called much more frequently than if placed in the forms constructor.
In the code snippets below, I'm creating a simple grid with 5 columns and 2 rows - all of type GridTextBoxColumn. When assigning the datasource in the constructor, it generates 25 calls to the CellFormatting event. If I move the datasource assignment to the form load event, it generates 35 calls. The problem is even more pronounced when I call the radGridView.LoadLayout function. If placed in the constructor, it still calls the CellFormatting event 25 times, but when I move both the datasource and LoadLayout calls to the form load event, CellFormatting gets called a whopping 90 times! Note that the xml file that I'm using is nothing crazy - simply the output from saving the contents of this grid in Visual Studio using the radGridView property builder.
I've tried to use radGridView.BeginInit, and radGridView.SuspendLayout to decrease the number of calls while the grid is being set up, but neither seem to have any effect. Should I resign myself to moving radGridView code to the forms constructor, or is there a way to accomplish this in the form load event?
using
System;
using
System.Collections.Generic;
using
System.ComponentModel;
using
System.Data;
using
System.Drawing;
using
System.Linq;
using
System.Text;
using
System.Windows.Forms;
using
Telerik.WinControls.UI;
namespace
RadGridTest
{
public
partial
class
Form1 : Form
{
private
Telerik.WinControls.UI.RadGridView radGridView1;
private
int
execCount = 0;
private
int
beginTime;
private
int
endTime;
private
BindingList<MyData> list;
public
Form1()
{
beginTime = Environment.TickCount;
InitializeComponent();
list =
new
BindingList<MyData>(); ;
for
(
int
i = 0; i <= 1; i++)
{
list.Add(
new
MyData(100, i, i));
}
//radGridView1.DataSource = list;
//radGridView1.LoadLayout("gridFormat.xml");
}
private
void
Form1_Load(
object
sender, EventArgs e)
{
// These commented lines cause much additional overhead in calling the CellFormatting function
// from the form_load event rather than the constructor.
radGridView1.DataSource = list;
radGridView1.LoadLayout(
"gridFormat.xml"
);
endTime = Environment.TickCount;
MessageBox.Show(
"Load Time: "
+ (endTime - beginTime).ToString() + Environment.NewLine +
"gridformat calls: "
+ execCount.ToString());
}
void
radGridView1_CellFormatting(
object
sender, Telerik.WinControls.UI.CellFormattingEventArgs e)
{
execCount++;
if
(e.CellElement.ColumnInfo
is
GridViewDataColumn)
{
GridViewDataColumn dataCol = e.CellElement.ColumnInfo
as
GridViewDataColumn;
if
(e.CellElement.Text !=
""
)
switch
(dataCol.FieldName)
{
case
"B"
: e.CellElement.Text = Convert.ToDecimal(e.CellElement.Value).ToString(
"C"
);
break
;
case
"D"
:
case
"E"
:
if
(Convert.ToDecimal(e.CellElement.Value) % 2 == 0)
e.CellElement.Text = Convert.ToDecimal(e.CellElement.Value).ToString(
"#,##0.0000"
);
else
e.CellElement.Text = Convert.ToDecimal(e.CellElement.Value).ToString(
"#,##0.00"
);
break
;
}
}
}
}
public
class
MyData
{
public
string
A {
get
;
set
; }
public
int
B {
get
;
set
; }
public
string
C {
get
;
set
; }
public
int
D {
get
;
set
; }
public
int
E {
get
;
set
; }
public
MyData(
int
b,
int
d,
int
e)
{
A =
"Money"
;
B = b;
C =
"Float"
;
D = d;
E = e;
}
}
}
// radGridView designer settings below
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private
void
InitializeComponent()
{
Telerik.WinControls.UI.GridViewTextBoxColumn gridViewTextBoxColumn1 =
new
Telerik.WinControls.UI.GridViewTextBoxColumn();
Telerik.WinControls.UI.GridViewTextBoxColumn gridViewTextBoxColumn2 =
new
Telerik.WinControls.UI.GridViewTextBoxColumn();
Telerik.WinControls.UI.GridViewTextBoxColumn gridViewTextBoxColumn3 =
new
Telerik.WinControls.UI.GridViewTextBoxColumn();
Telerik.WinControls.UI.GridViewTextBoxColumn gridViewTextBoxColumn4 =
new
Telerik.WinControls.UI.GridViewTextBoxColumn();
Telerik.WinControls.UI.GridViewTextBoxColumn gridViewTextBoxColumn5 =
new
Telerik.WinControls.UI.GridViewTextBoxColumn();
this
.radGridView1 =
new
Telerik.WinControls.UI.RadGridView();
((System.ComponentModel.ISupportInitialize)(
this
.radGridView1)).BeginInit();
((System.ComponentModel.ISupportInitialize)(
this
.radGridView1.MasterTemplate)).BeginInit();
this
.SuspendLayout();
//
// radGridView1
//
this
.radGridView1.BackColor = System.Drawing.SystemColors.Control;
this
.radGridView1.Cursor = System.Windows.Forms.Cursors.Default;
this
.radGridView1.Dock = System.Windows.Forms.DockStyle.Fill;
this
.radGridView1.Font =
new
System.Drawing.Font(
"Segoe UI"
, 8.25F);
this
.radGridView1.ForeColor = System.Drawing.SystemColors.ControlText;
this
.radGridView1.ImeMode = System.Windows.Forms.ImeMode.NoControl;
this
.radGridView1.Location =
new
System.Drawing.Point(0, 0);
//
// radGridView1
//
this
.radGridView1.MasterTemplate.AutoGenerateColumns =
false
;
gridViewTextBoxColumn1.FieldName =
"A"
;
gridViewTextBoxColumn1.FormatInfo =
new
System.Globalization.CultureInfo(
""
);
gridViewTextBoxColumn1.FormatString =
""
;
gridViewTextBoxColumn1.HeaderText =
"column1"
;
gridViewTextBoxColumn1.Name =
"column1"
;
gridViewTextBoxColumn1.Width = 76;
gridViewTextBoxColumn2.FieldName =
"B"
;
gridViewTextBoxColumn2.FormatInfo =
new
System.Globalization.CultureInfo(
""
);
gridViewTextBoxColumn2.FormatString =
""
;
gridViewTextBoxColumn2.HeaderText =
"column2"
;
gridViewTextBoxColumn2.Name =
"column2"
;
gridViewTextBoxColumn2.Width = 81;
gridViewTextBoxColumn3.FieldName =
"C"
;
gridViewTextBoxColumn3.FormatInfo =
new
System.Globalization.CultureInfo(
""
);
gridViewTextBoxColumn3.FormatString =
""
;
gridViewTextBoxColumn3.HeaderText =
"column3"
;
gridViewTextBoxColumn3.Name =
"column3"
;
gridViewTextBoxColumn3.Width = 82;
gridViewTextBoxColumn4.FieldName =
"D"
;
gridViewTextBoxColumn4.FormatInfo =
new
System.Globalization.CultureInfo(
""
);
gridViewTextBoxColumn4.FormatString =
""
;
gridViewTextBoxColumn4.HeaderText =
"column4"
;
gridViewTextBoxColumn4.Name =
"column4"
;
gridViewTextBoxColumn4.Width = 90;
gridViewTextBoxColumn5.FieldName =
"E"
;
gridViewTextBoxColumn5.FormatInfo =
new
System.Globalization.CultureInfo(
""
);
gridViewTextBoxColumn5.FormatString =
""
;
gridViewTextBoxColumn5.HeaderText =
"column5"
;
gridViewTextBoxColumn5.Name =
"ProgressBar"
;
gridViewTextBoxColumn5.Width = 88;
this
.radGridView1.MasterTemplate.Columns.AddRange(
new
Telerik.WinControls.UI.GridViewDataColumn[] {
gridViewTextBoxColumn1,
gridViewTextBoxColumn2,
gridViewTextBoxColumn3,
gridViewTextBoxColumn4,
gridViewTextBoxColumn5});
this
.radGridView1.Name =
"radGridView1"
;
this
.radGridView1.Padding =
new
System.Windows.Forms.Padding(0, 0, 0, 1);
this
.radGridView1.ReadOnly =
true
;
this
.radGridView1.RightToLeft = System.Windows.Forms.RightToLeft.No;
//
//
//
this
.radGridView1.RootElement.Padding =
new
System.Windows.Forms.Padding(0, 0, 0, 1);
this
.radGridView1.ShowGroupPanel =
false
;
this
.radGridView1.Size =
new
System.Drawing.Size(599, 416);
this
.radGridView1.TabIndex = 0;
this
.radGridView1.Text =
"radGridView1"
;
this
.radGridView1.CellFormatting +=
new
Telerik.WinControls.UI.CellFormattingEventHandler(
this
.radGridView1_CellFormatting);
//
// Form1
//
this
.AutoScaleDimensions =
new
System.Drawing.SizeF(6F, 13F);
this
.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this
.ClientSize =
new
System.Drawing.Size(599, 416);
this
.Controls.Add(
this
.radGridView1);
this
.Name =
"Form1"
;
this
.Text =
"Form1"
;
this
.Load +=
new
System.EventHandler(
this
.Form1_Load);
((System.ComponentModel.ISupportInitialize)(
this
.radGridView1.MasterTemplate)).EndInit();
((System.ComponentModel.ISupportInitialize)(
this
.radGridView1)).EndInit();
this
.ResumeLayout(
false
);
}
#endregion