Documents

NextGrid & NextDBGrid

NextGrid

NextDBGrid

NextInspector

NextSheet

NextCollection

Misc

Labs

NextGrid .NET

Optimize NextGrid

Related Articles:

Info
  • Skill:
  • Version: 1.0.0
  • Author: Bojan Nikolic
  • Created:
  • Updated: 2008-04-22

Introduction



NextGrid is very fast component. Adding rows, updating data, sorting columns, drawing cells... is very fast already, but with small tweaks you can speed up component even more.

1. Speed Up Adding Rows



As you know, to add single row in NextGrid, you can use NextGrid.AddRow() method. If you like to add multiple rows at time, you can use: NextGrid.AddRow(RowCount):

Example:

NextGrid.AddRow(10);
NextGrid.AddRow(100000);


NextGrid->AddRow(10);
NextGrid->AddRow(100000);


So, you don't need to create for loop and to call AddRow in each pass. NextGrid will do this for you and skip calling some methods every time which may speed up a lot so adding rows will be much faster after calling AddRow(100000) once, instead of calling AddRow method 100,000 times inside loop!

2. How to speed-up adding/changing many cells?



Before you start with adding or changing values of huge amount of cells (typically inside some for, while, repeat...until loop), call next method:

NextGrid1.BeginUpdate;

now add, change, move or delete cells.

after you finish, call next method:

NextGrid1.EndUpdate;

What BeginUpdate do?



This call prevent NextGrid from drawing EACH new or changed Cell until operation is finished. Also all complicated calculations needed for drawing Cell is skiped too. This can speed up your code a lot!

We will draw this rows ONLY after we finish with operation, and ONLY we will draw visible Cells on screen. After you finish with adding rows, ALLWAYS call EndUpdate.

Here you can see example how to add 100000 rows and to place Value in each Cell:

NextGrid.BeginUpdate;
for i := 0 to 100000 do
begin
  NextGrid1.Cell[1, i].AsBoolean := False;
  NextGrid1.Cell[2, i].AsString := Names[Random(1000)]   ' '   Names[Random(1000)];
  NextGrid1.Cell[3, i].AsInteger := Random(2);
  NextGrid1.Cell[4, i].AsDateTime := Today-Random(500);
  NextGrid1.Cell[5, i].AsFloat := Random(2000);
  NextGrid1.Cell[6, i].AsInteger := Random(101);
  NextGrid1.Cell[7, i].AsString := Cards[Random(4)];
  NextGrid1.Cell[8, i].AsInteger := Random(6);
  NextGrid1.Cell[9, i].AsInteger := Random(2);
end;
NextGrid.EndUpdate;


When you don't need BeginUpdate and EndUpdate methods?

If you work with one rows or up to 1000 rows, you don't need to use BeginUpdate and EndUpdate, because speed will be almost same.

3. Chosing Column Type



In TNxTextColumn you can put any kind of information: Text, Numbers, Date. Also, you can calculate fkSum, fkCount, fkAverage... formulas in footer of TNxTextColumn.

But, storing numbers or dates inside TNxTextColumn is slower than storing number inside TNxNumberColumn or date inside TNxDateColumn. Text column type will save given number as string which occupy more memory and takes more time to convert it back to number.

So, if you plan to have column with only numbers, chose TNxNumberColumn. This column draw, change, sort and calculate formulas for numbers much faster than TNxTextColumn do. This is because Double is native type for this column. TNxTextColumn need to do lots of conversion (FloatToStr, StrToFloat...) to sort numbers or to display it.

Was This Article Useful?

Comments

2008-09-09 09:26:30

Best is using try/finally:
ngData.BeginUpdate;
try
...hard work here...
finally
ngData.EndUpdate;
end;
Only constructive comments, code contributions... will be publishes. Questions, non-official discussion will not be published.