Same as NextGrid, NextDBGrid also includes handy Cell property which allows browsing data in form of two dimensional array.
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
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.
procedure TForm1.Button1Click(Sender: TObject); begin NextDBGrid61.Cell[1, 4].AsString := Edit1.Text; end;
- By calling Cell property we position to the field associated by 2nd column and 4th record inside TDataSet.
- 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.
- We set AsString property.
- 1. DataSet enter Edit mode and value is transferred to Field.
- 2. Post method is called.
- Process leave Button1Click event, and since Cell's reference is not saved inside any variable, cell is destroyed automatically.
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.
Same as in NextGrid there is a Row array property for accessing row properties:
NextDBGrid61.Row.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.