Smart Mobile Studio
  • News
  • Forums
  • Download
  • Store
  • Showcases
    • Featured demos
    • The Smart Contest 2013, Round 1 – Graphics
  • Documentation
    • Get the book
    • System requirements
    • Prerequisites
    • Getting started
      • Introduction
      • Application architecture
      • The application object
      • Forms and navigation
      • Message dialogs
      • Themes and styles
    • Project types
      • Visual project
      • Game project
      • Console project
    • Layout manager
    • Networking
      • TW3HttpRequest
      • TW3JSONP
      • Loading files
  • About

Working with the grid component (part 1)

Posted on 10.02.2014 by Bojan Nikolic Posted in Developers log, Documentation 2 Comments

Smart Mobile Studio 2.0 introduced an important visual component – a grid. As its use is not obvious, we have prepared a series of articles which will introduce the component and demonstrate the usage. Articles are written by the author of the grid component, Bojan Nikolic (Berg Soft).

In this article we’ll cover the most basic steps in grid usage.

  1. Placing W3Grid on a form
  2. Adding columns and setting column properties
  3. Adding rows and filling cells with data

1. Placing W3Grid on a form

1.1. Design-time

To create a TW3Grid component while designing a form, first click on the TW3Grid icon on the Data tab of the component palette:
W3Grid

Then, click on the design area. Now you may position and/or resize the grid as any other component.

1.2. Run-time

To create a W3Grid object in run-time, use the same approach as for creating any component in Smart Mobile Studio:

uses
  W3Grid;

type
  TForm1 = class(TW3form)
  private
    {$I 'Form1:intf'}
    MyGrid: TW3Grid;
  end;

implementation

{ TForm1}

// Create Grid object inside InitializeObject or inside some event
procedure TForm1.InitializeObject;
begin
  inherited;
  {$I 'Form1:impl'}
  MyGrid := TW3Grid.Create(Self);
  MyGrid.Visible := True;

It is important to remember to add the W3Grid unit to the uses section of your unit.

After the grid is created, you’ll most probably want to place it in a specific position. To do this, use the standard Top and Left properties. SetBounds method may also be used.

MyGrid.Top := 40;
MyGrid.Left := 8;

Beside the standard TW3Component properties and methods, TW3Grid includes specific properties and methods to control the look & feel of the control. Commonly you’ll want to set the HeaderHeigh and RowHeight. HeaderHeight specifies the vertical size (height) in logical pixels of the header area. RowHeight specifies the vertical size of every row added afterwards.

MyGrid.HeaderHeight := 26;
MyGrid.RowHeight := 24;

2. Adding columns and setting column properties

Before rows are added, columns must be added first. W3Grid is capable of showing and interacting with data in various ways. The type of a data in a cell is determined by the type of the column. For example, a cell may be displayed as a simple text, as a progress bar, toggle switch, an image ….

Smart Mobile Studio 2.0 supports following buit-in column types:

ClassAccept as value
TW3TextColumnany string
TW3NumberColumnnumbers (integer and float)
TW3ButtonColumnany string
TW3ToggleSwitchColumnboolean
TW3ProgressColumnnumbers (integer and float)
TW3SpriteColumnbounds format

To add a column, first create a TW3TextColumn object.

uses
  W3Columns;
…
var
  MyTextColumn: TW3TextColumn;
begin
  MyTextColumn := TW3TextColumn.Create(MyGrid as IW3ColumnsControl);

Then add the object to the Columns collection.

 MyGrid.Columns.Add(MyTextColumn);

As every column descends from the TW3Column class, all column types share common properties and methods. Notable properties are Width and Header. To specify a width of cells within a column, simply set the Width property.

MyTextColumn.Width := 120;

After column is added, it may be accessed via the Columns property via its index (zero-based):

MyGrid.Columns[4].Width := 80;

As the grid includes the header area, every column may specify its own header title. This may be set via the Header property.

MyGrid.Columns[4].Header.Caption := 'First Name';

Every column type introduces specific properties and methods. For example TW3NumberColumn contains Precision property which only makes sense for this column type. Typecasting must be used to access type-specific properties or methods. For example:

TW3NumberColumn(MyGrid.Columns[2]).Precision := 2;
TW3TextColumn(MyGrid.Columns[2]).TextBefore := '$';

3. Adding rows and filling cells with data

3.1. Adding rows

After columns are added and configured, rows may be added. Most common way to add a row is by using the AddRow method.

MyGrid.AddRow;

The line above will add one new row at the end of the grid. To add multiple rows at a time, simply specify the optional Count parameter.

MyGrid.AddRow(20)

A handy TW3Grid.LastAddedRow property returns (zero-based) index of the last added row.

When a new row is added, each column creates its own type of cell inside the row. For example,  a toggle switch column will create a cell with a toggle switch within, a progress column will create a  progress bar inside a cell etc.

3.2. Filling cells with data

After cells are added, we may fill them with data. Every cell is an object and includes (Variant) Value property storing cell’s value. All we need is to access the Cell property.

W3Grid.Cell[ColumnIndex, RowIndex].Value := Value;

4. Example

Let’s conclude this article with a working example.

  1. Place a TW3Button component on a form.
  2. Double click the button to create an OnClick event.
  3. Edit code:
    • Add the W3Grid units to uses list.
    • Add the W3Grid components to the private section of the form class.
    • Add the W3Button1Click code.
  4. Execute the project.
unit Form1;

interface

uses 
  W3System, W3Graphics, W3Components, W3Forms, W3Fonts, W3Borders, W3Application, W3Button,

  { W3Grid }
  W3Grid, W3Columns;

type
  TForm1 = class(TW3form)
    procedure W3Button1Click(Sender: TObject);
  private
    {$I 'Form1:intf'}
    MyGrid: TW3Grid;
    MyTextColumn: TW3TextColumn;
    MyNumberColumn: TW3NumberColumn;
  protected
    procedure InitializeForm; override;
    procedure InitializeObject; override;
    procedure Resize; override;
  end;

implementation

{ TForm1}

procedure TForm1.W3Button1Click(Sender: TObject);
begin
  { Create and set Grid }
  MyGrid := TW3Grid.Create(Self);
  MyGrid.Visible := True;
  MyGrid.SetBounds(8, 40, 200, 100);
  MyGrid.HeaderHeight := 20;
  MyGrid.RowHeight := 18;

  { Create Text Column object
    & set Header's Caption}
  MyTextColumn := TW3TextColumn.Create(MyGrid as IW3ColumnsControl);
  MyTextColumn.Header.Caption := 'Text';

  { Create Number Column object
    & set Header's Caption }
  MyNumberColumn := TW3NumberColumn.Create(MyGrid as IW3ColumnsControl);
  MyNumberColumn.Header.Caption := 'Number';

  { Add both Columns into array }
  MyGrid.Columns.Add(MyTextColumn);
  MyGrid.Columns.Add(MyNumberColumn);

  { Add 1 new row & set 1st cell in newly added row }
  MyGrid.AddRow;
  MyGrid.Cell[0, MyGrid.LastAddedRow].Value := 'sample';
  MyGrid.Cell[1, MyGrid.LastAddedRow].Value := 240.5;

  { Set property specific for Number Column only }
  TW3NumberColumn(MyGrid.Columns[1]).Precision := 2;
end;

procedure TForm1.InitializeForm;
begin
  inherited;
  // this is a good place to initialize components
end;

procedure TForm1.InitializeObject;
begin
  inherited;
  {$I 'Form1:impl'}
end;

procedure TForm1.Resize;
begin
  inherited;
end;

end.

This simple demo will give you this 2×2 grid: Screenshot

Grid tutorial
« Smart Mobile Studio 2.0
Working with the grid component (part 2) »

2 thoughts on “Working with the grid component (part 1)”

  1. Mason Wheeler says:
    11.02.2014 at 16:58

    This post appears to be missing the WordPress MORE tag. 🙁

    • Jørn E. Angeltveit says:
      11.02.2014 at 18:24

      Sorry. Added now…

Comments are closed.

Pages

  • About
  • Feature Matrix
  • Forums
  • News
  • Release History
  • Download
  • Showcases
    • The Smart Contest 2013, Round 1 – Graphics
  • Store
  • Documentation
    • Creating your own controls
    • Debugging, exceptions and error handling
    • Differences between Delphi and Smart
    • Get the book
    • Getting started
      • Introduction
      • Local storage, session storage and global storage
      • Application architecture
      • The application object
      • Forms and navigation
      • Message dialogs
      • pmSmart Box Model
      • Themes and styles
    • Layout manager
    • Networking
      • Loading files
      • TW3HttpRequest
      • TW3JSONP
    • Prerequisites
    • Real data, talking to sqLite
    • System requirements
    • Project types
      • Visual project
      • Game project
      • Console project

Archives

  • December 2019
  • December 2018
  • November 2018
  • July 2018
  • June 2018
  • February 2018
  • September 2017
  • April 2017
  • November 2016
  • October 2016
  • September 2016
  • April 2016
  • March 2016
  • January 2016
  • October 2015
  • September 2015
  • July 2015
  • April 2015
  • January 2015
  • December 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014
  • March 2014
  • February 2014
  • January 2014
  • December 2013
  • November 2013
  • October 2013
  • August 2013
  • July 2013
  • June 2013
  • May 2013
  • April 2013
  • March 2013
  • February 2013
  • January 2013
  • December 2012
  • November 2012
  • August 2012
  • July 2012
  • June 2012
  • May 2012
  • April 2012
  • March 2012
  • February 2012
  • January 2012
  • November 2011
  • October 2011
  • September 2011

Categories

  • Announcements (25)
  • Developers log (119)
  • Documentation (26)
  • News (104)
  • News and articles (16)

WordPress

  • Register
  • Log in
  • WordPress

Subscribe

  • Entries (RSS)
  • Comments (RSS)
© Optimale Systemer AS