unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, NxScrollControl, NxCustomGridControl, NxCustomGrid, NxGrid,
  Menus, StdCtrls, ExtCtrls, ComCtrls, NxEdit, NxColumns,
  NxDisplays, NxGridPrint, NxColumnClasses;

type
  TForm1 = class(TForm)
    GridView1: TNextGrid;
    MainMenu1: TMainMenu;
    File1: TMenuItem;
    Exit1: TMenuItem;
    Edit1: TMenuItem;
    SelectAll1: TMenuItem;
    SelectNone1: TMenuItem;
    Panel1: TPanel;
    Button1: TButton;
    Button2: TButton;
    StatusBar1: TStatusBar;
    SpinEditor1: TNxSpinEdit;
    Label1: TLabel;
    Button3: TButton;
    Style1: TMenuItem;
    Automatic1: TMenuItem;
    OldStyleMSAccess1: TMenuItem;
    FlatStyle1: TMenuItem;
    N1: TMenuItem;
    DeleteSelected1: TMenuItem;
    DeleteCurrentRow1: TMenuItem;
    Selection1: TMenuItem;
    MultiSelect1: TMenuItem;
    IndicateSelectedCell1: TMenuItem;
    SelectFullRow1: TMenuItem;
    N2: TMenuItem;
    MakeEditable1: TMenuItem;
    HideFocus1: TMenuItem;
    MakeAutosizeable1: TMenuItem;
    N3: TMenuItem;
    EnableThemes1: TMenuItem;
    PrintDialog1: TPrintDialog;
    N4: TMenuItem;
    Print1: TMenuItem;
    GridPrint1: TNxGridPrint;
    N5: TMenuItem;
    ShowSelectedCount1: TMenuItem;
    ShowGrid1: TMenuItem;
    Misc1: TMenuItem;
    VisibleRowsCount1: TMenuItem;
    SelectLastRow1: TMenuItem;
    check: TNxCheckBoxColumn;
    title: TNxTextColumn;
    rating: TNxRateColumn;
    comment: TNxHtmlColumn;
    TextualColumn1: TNxTextColumn;
    IncrementColumn1: TNxIncrementColumn;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure SelectAll1Click(Sender: TObject);
    procedure SelectNone1Click(Sender: TObject);
    procedure Exit1Click(Sender: TObject);
    procedure SpinEditor1Change(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Automatic1Click(Sender: TObject);
    procedure OldStyleMSAccess1Click(Sender: TObject);
    procedure FlatStyle1Click(Sender: TObject);
    procedure DeleteSelected1Click(Sender: TObject);
    procedure GridView1CellFormating(Sender: TObject; ACol, ARow: Integer;
      var TextColor: TColor; var FontStyle: TFontStyles;
      CellState: TCellState);
    procedure DeleteCurrentRow1Click(Sender: TObject);
    procedure IndicateSelectedCell1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure SelectFullRow1Click(Sender: TObject);
    procedure MultiSelect1Click(Sender: TObject);
    procedure MakeEditable1Click(Sender: TObject);
    procedure HideFocus1Click(Sender: TObject);
    procedure MakeAutosizeable1Click(Sender: TObject);
    procedure EnableThemes1Click(Sender: TObject);
    procedure GridView1FooterClick(Sender: TObject; ACol: Integer);
    procedure Print1Click(Sender: TObject);
    procedure ShowSelectedCount1Click(Sender: TObject);
    procedure ShowGrid1Click(Sender: TObject);
    procedure VisibleRowsCount1Click(Sender: TObject);
    procedure GridView1SelectCell(Sender: TObject; ACol, ARow: Integer);
    procedure SelectLastRow1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  Movies: array[0..9] of WideString =
    ('Back To The Future', 'Kill Bill', 'Matrix',
      'Pretty Woman', 'Once Upon A Time In Mexico',
      'Undercover Bro', 'Dantes Peak', 'A Guy Think',
      'Alien', 'Osmozis'
  );
  Comments: array[0..9] of WideString =
    ('<font color="0000cc"><b>Funy</b> movie</font>', '', 'This is a <i>comment</i><br>in several<br><b>rows.</b>' ,
      'a <font color="#ff0000"><i>romantic movie</i></font>',
      '', '', '', '', '<font color="#009922">very <u>scary</u> </font><font color="#000000>movie</font>', '<i>Cartoon</i>');

implementation

uses Math;

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to 9 do
  begin
    GridView1.AddRow;
    GridView1.CellByName['title', 'Last'].AsString := Movies[i];
    GridView1.CellByName['rating', 'Last'].AsInteger := Random(6);
    GridView1.CellByName['comment', 'Last'].AsString := Comments[i];
    GridView1.CellByName['comment', 'Last'].Hint := Comments[i];
  end;
  StatusBar1.Panels[0].Text := '10 Rows Added';
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  GridView1.ClearRows;
  StatusBar1.Panels[0].Text := 'Grid is Empty';
end;

procedure TForm1.SelectAll1Click(Sender: TObject);
begin
  GridView1.SelectAll;
end;

procedure TForm1.SelectNone1Click(Sender: TObject);
begin
  GridView1.NoSelection;
end;

procedure TForm1.Exit1Click(Sender: TObject);
begin
  Close;
end;

procedure TForm1.SpinEditor1Change(Sender: TObject);
begin
  if (GridView1.SelectedRow >= 0) and (GridView1.SelectedRow < GridView1.RowCount) and (GridView1.RowCount > 0) then
    GridView1.RowHeight[GridView1.SelectedRow] := SpinEditor1.AsInteger;
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to GridView1.RowCount - 1 do
  begin
    GridView1.RowHeight[i] := 14 + Random(30);
  end;
end;

procedure TForm1.Automatic1Click(Sender: TObject);
begin
  GridView1.HeaderStyle := hsAuto;
end;

procedure TForm1.OldStyleMSAccess1Click(Sender: TObject);
begin
  GridView1.HeaderStyle := hsOldStyle;
end;

procedure TForm1.FlatStyle1Click(Sender: TObject);
begin
  GridView1.HeaderStyle := hsFlatBorders;
end;

procedure TForm1.DeleteSelected1Click(Sender: TObject);
var
  i: Integer;
begin
  i := 0;
  while i < GridView1.RowCount do
  begin
    if GridView1.CellByName['check', i].AsBoolean then
    begin
      GridView1.DeleteRow(i);
    end else Inc(i);
  end;
end;

procedure TForm1.GridView1CellFormating(Sender: TObject; ACol,
  ARow: Integer; var TextColor: TColor; var FontStyle: TFontStyles;
  CellState: TCellState);
begin
  if ARow = 4 then TextColor := clPurple else
  if ARow = 8 then TextColor := clGreen;
end;

procedure TForm1.DeleteCurrentRow1Click(Sender: TObject);
begin
  GridView1.DeleteRow(GridView1.SelectedRow);
end;

procedure TForm1.IndicateSelectedCell1Click(Sender: TObject);
begin
  case TMenuItem(Sender).Checked of
    True: GridView1.AppearanceOptions := GridView1.AppearanceOptions + [aoIndicateSelectedCell];
    False: GridView1.AppearanceOptions := GridView1.AppearanceOptions - [aoIndicateSelectedCell];
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  IndicateSelectedCell1.Enabled := goSelectFullRow in GridView1.Options;
end;

procedure TForm1.SelectFullRow1Click(Sender: TObject);
begin
  case TMenuItem(Sender).Checked of
    True: GridView1.Options := GridView1.Options + [goSelectFullRow];
    False: GridView1.Options := GridView1.Options - [goSelectFullRow];
  end;
  IndicateSelectedCell1.Enabled := goSelectFullRow in GridView1.Options;
end;

procedure TForm1.MultiSelect1Click(Sender: TObject);
begin
  case TMenuItem(Sender).Checked of
    True: GridView1.Options := GridView1.Options + [goMultiSelect];
    False: GridView1.Options := GridView1.Options - [goMultiSelect];
  end;
end;

procedure TForm1.MakeEditable1Click(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to GridView1.Columns.Count - 1 do
  begin
    GridView1.Columns[i].Options := GridView1.Columns[i].Options + [coEditing];
  end;
end;

procedure TForm1.HideFocus1Click(Sender: TObject);
begin
  case TMenuItem(Sender).Checked of
    True: GridView1.AppearanceOptions := GridView1.AppearanceOptions + [aoHideFocus];
    False: GridView1.AppearanceOptions := GridView1.AppearanceOptions - [aoHideFocus];
  end;
end;

procedure TForm1.MakeAutosizeable1Click(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to GridView1.Columns.Count - 1 do
  begin
    GridView1.Columns[i].Options := GridView1.Columns[i].Options + [coAutoSize];
  end;
end;

procedure TForm1.EnableThemes1Click(Sender: TObject);
begin
  GridView1.EnableVisualStyles := TMenuItem(Sender).Checked;
end;

procedure TForm1.GridView1FooterClick(Sender: TObject; ACol: Integer);
begin
  if (ACol = 2) or (ACol = 4) then GridView1.CalculateFooter;
end;

procedure TForm1.Print1Click(Sender: TObject);
begin
  if PrintDialog1.Execute then
  begin
    GridPrint1.Print;
  end;
end;

procedure TForm1.ShowSelectedCount1Click(Sender: TObject);
begin
  ShowMessage(IntToStr(GridView1.SelectedCount));
end;

procedure TForm1.ShowGrid1Click(Sender: TObject);
begin
  case TMenuItem(Sender).Checked of
    True: GridView1.Options := GridView1.Options + [goGrid];
    False: GridView1.Options := GridView1.Options - [goGrid];
  end;
end;

procedure TForm1.VisibleRowsCount1Click(Sender: TObject);
begin
  StatusBar1.Panels[1].Text := 'Visible Rows: ' + IntToStr(GridView1.GetVisibleCount);
end;

procedure TForm1.GridView1SelectCell(Sender: TObject; ACol, ARow: Integer);
begin
  SpinEditor1.AsInteger := GridView1.RowHeight[ARow];
end;

procedure TForm1.SelectLastRow1Click(Sender: TObject);
begin
  if GridView1.RowCount > 0 then GridView1.SelectLastRow;
end;

end.
