Library Vcl NextSuite 6 NextGrid6 Articles Quick Start Run time

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.

pas
...
uses
  NxGrid6, NxGridView6;

type
  TForm1 = class(TForm)
    SalesGrid: TNextGrid6;
    ReportView: TNxReportGridView6;
  end;

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

pas
SalesGrid := TNextGrid6.Create(self);
SalesGrid.Parent := Self;

Now we will create View and add it to Views array.

pas
ReportView := TNxReportGridView6.Create(self);
SalesGrid.Views.Add(ReportView);

Finally we set our View as currently active View for Grid.

pas
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:

pas
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:

pas
AddRow(RowCount);

Example

pas
begin
  NextGrid.AddRow; // Adds a single row

  NextGrid.AddRow(4); // Adds 4 rows

  NextGrid.AddRow(10000);

cpp
{
  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.

pas
NextGrid.AddCells(['Mike', '23', 'True', 'Lisa', '178', 'False'], [1, 4, 5]);

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

pas
InsertRow(Pos);

After row is added LastAddedRow property may be used for formatting newly added row:

Example

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

Modifying rows

Deleting row is done by calling DeleteRow method:

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

pas
NextGrid.Cell[4, 3].AsString := 'test';
NextGrid.Cell[4, 5].Color := clRed;
NextGrid.Cell[4, 7].Font.Name := 'Verdana';

cpp
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:

pas
NextGrid.Cell[1, 1].Hint := 'Cell Comment';

cpp
NextGrid->Cell[1, 1]->Hint = "Cell Comment";

Setting, or determining value of cell is done by these properties:

Example:

pas
Cell[4, 2].AsFloat := 3.4;
Cell[4, 2].AsDateTime := Now();
Cell[4, 2].AsBoolean := False;
Cell[4, 2].AsString := 'This is Cell Value';

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

pas
NextGrid.Cells[2, 2] := 'Cell Text';

cpp
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:

pas
Cell[5, 1].AsBoolean := True; // correct

Cell[5, 1].AsString := 'True'; // correct

Cell[5, 1].AsString := 'Yes'; // incorrect

Examples for Icon Column:

pas
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;

Sign in