Run time
Working with NextGrid v6 in run time is much easier than working with standard ListView or StringGrid components.
Adding Grid and View
Most of the times when working with NextGrid, the component is placed on a form, Views are added and set, as long as columns, all in the design-time mode. Then, rows can be added from code. But, if you need to create grid and views completely from code, this article is for you.
First, we need to declare variables for Grid and View objects.
...
uses
NxGrid6, NxGridView6;
type
TForm1 = class(TForm)
SalesGrid: TNextGrid6;
ReportView: TNxReportGridView6;
end;
class TForm1 : public TForm { published: // IDE-managed Components void fastcall FormCreate(TObject *Sender); private: // User declarations TNextGrid6 *SalesGrid; TNxReportGridView6 *ReportView; public: // User declarations fastcall TForm1(TComponent* Owner); };
Then we need to create a Grid object.
SalesGrid := TNextGrid6.Create(self); SalesGrid.Parent := Self;
Now we will create View and add it to Views array.
ReportView := TNxReportGridView6.Create(self); SalesGrid.Views.Add(ReportView);
Finally we set our View as currently active View for Grid.
SalesGrid.ActiveView := MyReportGridView;
You may create several GridView object and simply point
ActiveView
property to one of them when you want to switch View.
Adding columns
Adding new columns in run-time is pretty straightforward. It is only important to add
NxColumns6
unit into uses section of the unit.
A column may be added in a few ways:
NextGrid61.Columns.Add(TNxTextColumn6, 'First Name', 'FN');
The first parameter expects meta-class of the new column. Here we can put any column class we need to add. Such as
TNxTextColumn6
, TNxCheckBoxColumn6
, TNxNumberColumn6
etc. The second parameter specifies Caption
for Column's Header
.
Adding rows
After columns are added and configured within Columns Editor, rows may be added.
Row can be added with using
AddRow
method. After calling this method, each column will get one new empty cell at the end, and you will have one new row in you Grid. This method set LastAddedRow
property which may be useful in many situations.
If you like to add more rows in same time use:
AddRow(RowCount);
Example
begin NextGrid.AddRow; // Adds a single row NextGrid.AddRow(4); // Adds 4 rows NextGrid.AddRow(10000);
{ NextGrid->AddRow; // Adds a single row NextGrid->AddRow(4); // Adds 4 rows NextGrid->AddRow(10000);
Also,
AddCells
method for adding several Cells with Values at once. This method will add as many new rows as needed to add all cells.
NextGrid.AddCells(['Mike', '23', 'True', 'Lisa', '178', 'False'], [1, 4, 5]);
NextGrid->AddCells(["Mike", "23", "true", "Lisa", "178", "false"], [1, 4, 5]);
Inserting new row at specific position may be done with InsertRow method. This method also set
LastAddedRow
property.
InsertRow(Pos);
After row is added
LastAddedRow
property may be used for formatting newly added row:
Example
NextGrid.Row[NextGrid61.LastAddedRow].Height := 40; NextGrid.BestFitRow(NextGrid61.LastAddedRow); NextGrid.SelectedRow := NextGrid61.LastAddedRow;
Modifying rows
Deleting row is done by calling
DeleteRow
method:
NextGrid.DeleteRow(RowIndex);
Property
RowCount
return how many rows are total in grid. Setting this property (since this is read-write property) to value lower than current end rows will be deleted, otherwise new rows will be added.
Working with cells
In NextGrid v6 each Cell is an object with own properties and methods.
Access to a single cell is done by
Cell
property. Cell
property is a 2 dimensions array property. ColumnIndex
and RowIndex
must be specified in order to access cell.
NextGrid.Cell[4, 3].AsString := 'test'; NextGrid.Cell[4, 5].Color := clRed; NextGrid.Cell[4, 7].Font.Name := 'Verdana';
NextGrid->Cell[4, 3]->AsString = "test"; NextGrid->Cell[4, 4]->Color = clRed; NextGrid->Cell[4, 7]->Font->Name := 'Verdana';
Each Cell own value,
Color
, Hint
, Text
properties.
Setting
Hint
is done:
NextGrid.Cell[1, 1].Hint := 'Cell Comment';
NextGrid->Cell[1, 1]->Hint = "Cell Comment";
Setting, or determining value of cell is done by these properties:
Example:
Cell[4, 2].AsFloat := 3.4; Cell[4, 2].AsDateTime := Now(); Cell[4, 2].AsBoolean := False; Cell[4, 2].AsString := 'This is Cell Value';
Cell[4][2]->AsFloat = 3.4; Cell[4][2]->AsDateTime = Now(); Cell[4][2]->AsBoolean = false; Cell[4][2]->AsString = "This is Cell Value";
Property Cells is also multi-dimensional property and it is equivalent to
Cell[Col, Row].AsString
property.
NextGrid.Cells[2, 2] := 'Cell Text';
NextGrid->Cells[2, 2] = "Cell Text";
Any .As property for any Column type, it is important to remember that you non-numeric value can't set to numeric column types, and invalid date (in invalid format) using AsString for
TNxDateColumn
can't be set.
Examples for CheckBox Column:
Cell[5, 1].AsBoolean := True; // correct Cell[5, 1].AsString := 'True'; // correct Cell[5, 1].AsString := 'Yes'; // incorrect
Examples for Icon Column:
Cell[5, 1].AsInteger := 6; // correct Cell[5, 1].AsFloat := 6; // correct Cell[5, 1].AsString := '6'; // correct Cell[5, 1].AsString := 'six'; // incorrect
Also, Numeric Column types (TNxNumberColumn6
, TNxProgressColumn6
...) are most optimised for using AsFloat
property for setting Value; Date column is most optimised for using AsDateTime
; CheckBox Column for using AsBoolean
;