Developer

Run-time

Working with NextGrid v6 in run time is much more easy than working with ListView or StringGrid.

Adding Grid and View


Most times you will create Grid, View and even Column in design-time and then add and modify rows. But, if you need to create grid and views completely from code, read this section.

First we need to declare variables for Grid and View objects.

... uses NxGrid6, NxGridView6; type TForm1 = class(TForm) SalesGrid: TNextGrid6; ReportView: TNxReportGridView6; end;
... #include "NxGrid6.hpp" 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); };
First, inside event 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 them when you want to switch View.

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 NextGrid1.AddRow; // Add single row NextGrid1.AddRow(4); // Add 4 rows NextGrid1.AddRow(10000);
{ NextGrid1->AddRow; // Add single row NextGrid1->AddRow(4); // Add 4 rows NextGrid1->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.

NextGrid1.AddCells(['Mike', '23', 'True', 'Lisa', '178', 'False'], [1, 4, 5]);
NextGrid1->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:

NextGrid61.Row[NextGrid61.LastAddedRow].Height := 40; NextGrid61.BestFitRow(NextGrid61.LastAddedRow); NextGrid61.SelectedRow := NextGrid61.LastAddedRow;

Modifying rows


Deleting row is done with:

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 a 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.

NextGrid1.Cell[4, 3].AsString := 'test'; NextGrid1.Cell[4, 5].Color := clRed; NextGrid1.Cell[4, 7].Font.Name := 'Verdana';
NextGrid1->Cell[4, 3]->AsString = "test"; NextGrid1->Cell[4, 4]->Color = clRed; NextGrid1->Cell[4, 7]->Font->Name := 'Verdana';
Each Cell own Value, Color, Hint Text properties.

Setting Hint is done:

NextGrid1.Cell[1, 1].Hint := 'Cell Comment';
NextGrid1->Cell[1, 1]->Hint = "Cell Comment";
Setting, or determing 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 equialent to Cell[Col, Row].AsString property.

NextGrid1.Cells[2, 2] := 'Cell Text';
NextGrid1->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 optimized for using AsFloat property for setting Value; Date column is most optimized for using AsDateTime; CheckBox Column for using AsBoolean;

Back to Design-time tutorial.

See also