Library Vcl NextSuite 6 NextDBGrid6 Articles Working with Cells

Working with Cells

Introduction

Same as NextGrid, NextDBGrid also includes handy Cell property which allows browsing data in form of two dimensional array.

Example

pas
MyStringVar := NextDBGrid61.Cell[2, 2].AsString;

Working with this property is slightly different, and in some situations more restricted than in non-db NextGrid since DB version of our grid doesn't handle own data structure, but rally on data from associated TDataSet.

In standard NextGrid (non-db) version every cell is permanently created after method such as AddRow, InsertRow etc. is called. In NextDBGrid we don't always know if row is added, deleted or changed. Therefore we can't add or delete cells in appropriate situations, but we are accessing data (cell) on request.

Help from Interfaces

In Delphi object created implementing interface have different life cycle than standard object. They are not released by calling destructor, instead they are managed by using reference counting technique and released when there is no more references to the object.

Using interfaces for cells in NextDBGrid allows us to access desired cell (and TField), read or modify value and then leave cell to be released automatically.

Example

pas
procedure TForm1.Button1Click(Sender: TObject);
begin
  NextDBGrid61.Cell[1, 4].AsString := Edit1.Text;
end;

  1. By calling Cell property we position to the field associated by 2nd column and 4th record inside TDataSet.
  2. New TNxFieldCell object is created "on-fly", and value of TField is transferred to cell.

- Standard cell properties such as AsString, AsInteger etc. can be used in this step for examining value of field.

  1. We set AsString property.
  2. 1. DataSet enter Edit mode and value is transferred to Field.
  3. 2. Post method is called.
  4. Process leave Button1Click event, and since Cell's reference is not saved inside any variable, cell is destroyed automatically.

Usage

Accessing Cell property is suitable for smaller editing/reading, on data already buffered. For example if user have selected or changed record.

If you need to alter big number of records, or data is stored on remote server it is better to use SQL than moving trough DataSet and altering values.

Rows

Same as in NextGrid there is a Row array property for accessing row properties:

pas
NextDBGrid61.Row[3].Height := 32;

Same as for Cell property, method of keeping row properties is different than in standard NextGrid.

If one of important row properties such as Height or Selected is changed, row object is created and placed into array. Next time when row is being drawn, grid will lookup this array to check if row is stored. If not, default values will be used.

Sign in