Optimizing NextGrid6
NextGrid6 is very fast component out of the box. Adding rows, updating data, sorting columns, drawing cells... is very fast already, but with small tweaks you can speed up component even more especially for situations when large number of cells are updated.
Speed Up Adding Rows
As you know, to add single row in NextGrid, you can use AddRow
method. If you like to add multiple rows at time, you can use AddRow(RowCount)
:
Example
NextGrid61.AddRow(10); NextGrid61.AddRow(100000);
NextGrid61->AddRow(10); NextGrid61->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!
// slower for i := 1 to 10000 do begin NextGrid61.AddRow; end; // faster NextGrid61.AddRow(10000);
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
BeginUpdate
method.
NextGrid61.BeginUpdate;
Now add, change, move or delete cells.
After you finish, call
EndUpdate
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 skipped 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, always call
EndUpdate
.
Here you can see example how to add 100000 rows and to place Value in each Cell:
NextGrid61.BeginUpdate; for i := 0 to 100000 do begin NextGrid61.Cell[1, i].AsBoolean := False; NextGrid61.Cell[2, i].AsString := Names[Random(1000)]; NextGrid61.Cell[3, i].AsInteger := Random(2); NextGrid61.Cell[4, i].AsDateTime := Today-Random(500); NextGrid61.Cell[5, i].AsFloat := Random(2000); NextGrid61.Cell[6, i].AsInteger := Random(101); NextGrid61.Cell[7, i].AsString := Cards[Random(4)]; NextGrid61.Cell[8, i].AsInteger := Random(6); NextGrid61.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 the same.
Choosing Column Type
In TNxTextColumn6
you can put any kind of information: Text, Numbers, Date. Also, you can calculate fkSum
, fkCount
, fkAverage
... formulas in footer of TNxTextColumn6
.
But, storing numbers or dates inside TNxTextColumn6 is slower than storing number inside TNxNumberColumn6
or date inside TNxDateColumn6
. 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 TNxNumberColumn6
. This column draw, change, sort and calculate formulas for numbers much faster than TNxTextColumn6
do. This is because Double is native type for this column. TNxTextColumn6
need to do lots of conversion (FloatToStr, StrToFloat...) to sort numbers or to display it.
Optimise hiding large number of rows
Row may be hidden by using Visible property of Row object and for smaller number of rows is perfectly fine to use this property. For hiding large number of rows (e.g. more than 50,000) it is better to use HideAll
method of TNextGrid6.
HideAll
method require TNxHideAllFunc
function as parameter.
Example:
private function HideAllFunc(Index: Integer): Boolean; ... function TForm1.HideAllFunc(Index: Integer): Boolean; begin Result := (Index mod 2) = 0; end; procedure TForm1.ButtonTestBugClick(Sender: TObject); var i : Integer; begin NextGrid61.HideAll(HideAllFunc); end;