unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, NxColumns, NxDBColumns, NxScrollControl, NxCustomGridControl,
  NxCustomGrid, NxDBGrid, DB, StdCtrls, ComCtrls, ExtCtrls, DBCtrls, Mask,
  Grids, DBGrids, Buttons, ImgList;

type
  TForm1 = class(TForm)
    NextDBGrid1: TNextDBGrid;
    DataSource1: TDataSource;
    TNxDBTextColumn1: TNxDBTextColumn;
    TNxDBCheckBoxColumn1: TNxDBCheckBoxColumn;
    NxDBProgressColumn1: TNxDBProgressColumn;
    NxDBTextColumn1: TNxDBTextColumn;
    Button1: TButton;
    StatusBar1: TStatusBar;
    GroupBox1: TGroupBox;
    DBEdit1: TDBEdit;
    Label1: TLabel;
    DBNavigator1: TDBNavigator;
    Button3: TButton;
    NxDBIncrementColumn1: TNxDBIncrementColumn;
    Label2: TLabel;
    DBEdit2: TDBEdit;
    CheckBox1: TCheckBox;
    SpeedButton1: TSpeedButton;
    SpeedButton2: TSpeedButton;
    NxDBNumberColumn1: TNxDBNumberColumn;
    SpeedButton3: TSpeedButton;
    PrintDialog1: TPrintDialog;
    CheckBox2: TCheckBox;
    DBEdit3: TDBEdit;
    Label3: TLabel;
    ImageList1: TImageList;
    NxDBImageColumn1: TNxDBImageColumn;
    DBEdit4: TDBEdit;
    Label4: TLabel;
    ColorDialog1: TColorDialog;
    SpeedButton4: TSpeedButton;
    CheckBox3: TCheckBox;
    Button4: TButton;
    Button2: TButton;
    Button5: TButton;
    Button6: TButton;
    Button7: TButton;
    Button8: TButton;
    Button9: TButton;
    procedure NextDBGrid1CustomDrawCell(Sender: TObject; ACol,
      ARow: Integer; CellRect: TRect; CellState: TCellState);
    procedure Button1Click(Sender: TObject);
    procedure NextDBGrid1SelectCell(Sender: TObject; ACol, ARow: Integer);
    procedure NextDBGrid1InputAccept(Sender: TObject; var Accept: Boolean);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure CheckBox1Click(Sender: TObject);
    procedure GroupBox1Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure SpeedButton2Click(Sender: TObject);
    procedure NextDBGrid1FooterClick(Sender: TObject; ACol: Integer);
    procedure CheckBox2Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure NextDBGrid1ApplyCell(Sender: TObject; ACol, ARow: Integer; var Value: WideString);
    procedure SpeedButton4Click(Sender: TObject);
    procedure CheckBox3Click(Sender: TObject);
    procedure NextDBGrid1SortColumn(Sender: TObject; ACol: Integer; Ascending: Boolean);
    procedure Button2Click(Sender: TObject);
    procedure NextDBGrid1MouseWheelDown(Sender: TObject;
      Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);
    procedure NextDBGrid1CellColoring(Sender: TObject; ACol, ARow: Integer;
      var CellColor, GridColor: TColor; CellState: TCellState);
    procedure NextDBGrid1CellHint(Sender: TObject; ACol, ARow: Integer;
      var Value: WideString);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses Unit2;

var MessageShown: Boolean = False;

{$R *.dfm}

procedure TForm1.NextDBGrid1ApplyCell(Sender: TObject; ACol, ARow: Integer; var Value: WideString);
begin
  if ACol = 6 then
  begin
    Value := '2';

    if NextDBGrid1.Columns[5].Field.AsFloat > 10000 then
    begin
      Value := '2';
    end else
    if NextDBGrid1.Columns[5].Field.AsFloat < 10000 then
    begin
      Value := '1';
    end else Value := '0';
  end;
end;

procedure TForm1.NextDBGrid1CustomDrawCell(Sender: TObject; ACol,
  ARow: Integer; CellRect: TRect; CellState: TCellState);
var
  R: TRect;
  Field: TField;
begin
  if ACol = 1 then
  begin
    Field := NextDBGrid1.Columns[ACol].Field;
    if Assigned(Field) and not Field.IsNull then
    begin
      R := CellRect;
      InflateRect(R, -3, -2);
      NextDBGrid1.Canvas.Pen.Color := clGrayText;
      NextDBGrid1.Canvas.Brush.Color := Field.AsInteger;
      NextDBGrid1.Canvas.Rectangle(R);
    end;
  end; 
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  DataSource1.DataSet.Open;
  DBEdit1.DataField := 'progress';
  DBEdit2.DataField := 'project';
end;

procedure TForm1.NextDBGrid1SelectCell(Sender: TObject; ACol,
  ARow: Integer);
begin
  StatusBar1.Panels[0].Text := 'Selected count: ' + IntToStr(NextDBGrid1.SelectedCount);
end;

procedure TForm1.NextDBGrid1SortColumn(Sender: TObject; ACol: Integer; Ascending: Boolean);
var
  Field: TField;
	DirectionStr: string;
begin
  Field := NextDBGrid1.Columns[ACol].Field;
  if Field <> nil then
  begin
	  if Ascending then DirectionStr := 'ASC'
  	  else DirectionStr := 'DESC';

    DataModule1.ADOTable1.Sort := Field.FieldName + ' ' + DirectionStr;
    if not MessageShown then
    begin
      ShowMessage('TADOTable.Sort method and OnSortColumn event are used for sorting grid.');
      MessageShown := True;
    end;
  end;
end;

procedure TForm1.NextDBGrid1InputAccept(Sender: TObject;
  var Accept: Boolean);
var
  i: Integer;
begin
  { note: we will not accept empty record }
  for I := 0 to NextDBGrid1.Columns.Count - 1 do
  begin
    if NextDBGrid1.Columns[i].InputValue <> '' then
    begin
      Accept := True;
      Exit;
    end;
  end;
  Accept := False;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  if DataSource1.DataSet.Active then DataSource1.DataSet.Append;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  NextDBGrid1.SelectLastRow;
end;

procedure TForm1.CheckBox1Click(Sender: TObject);
begin
  if CheckBox1.Checked
    then NextDBGrid1.DataAwareOptions := NextDBGrid1.DataAwareOptions + [doBufferRecords]
      else NextDBGrid1.DataAwareOptions := NextDBGrid1.DataAwareOptions - [doBufferRecords];
end;

procedure TForm1.CheckBox2Click(Sender: TObject);
begin
  if (Sender as TCheckBox).Checked then
  begin
    NextDBGrid1.Options := NextDBGrid1.Options + [goSecondClickEdit];
  end else
  begin
    NextDBGrid1.Options := NextDBGrid1.Options - [goSecondClickEdit];
  end;
end;

procedure TForm1.CheckBox3Click(Sender: TObject);
begin
  if (Sender as TCheckBox).Checked
    then NextDBGrid1.Options := NextDBGrid1.Options + [goLockFixedCols]
      else NextDBGrid1.Options := NextDBGrid1.Options - [goLockFixedCols];
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  CheckBox1.Hint := 'When enabled all records will be downloaded from DataSet.' + sLineBreak +
                    '(DatawareOptions.doBufferRecords)';

  CheckBox2.Hint := 'When enabled user will first need to select cell and then click once again to edit it' + sLineBreak +
                    '(Options.goSecondClickEdit)';

  CheckBox3.Hint := 'When enabled user will not be able to change fixed cols and made them not-fixed (and vice-versa).' + sLineBreak +
                    '(Options.goLockFixedCols)';

  NextDBGrid1.Columns[6].Header.Hint := 'Icon is set manualy from event, without setting FieldName' + sLineBreak +
                                        '(OnApplyCell)';

  NextDBGrid1.Columns[5].Footer.Hint := 'In buffered mode, only downloaded records from server are calculated' + sLineBreak +
                                        '(CalculateFooter)';
end;

procedure TForm1.GroupBox1Click(Sender: TObject);
begin
  Height := Height - 10;
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  NextDBGrid1.GridStyle := gsSlides;
  NextDBGrid1.Options := NextDBGrid1.Options - [goSelectFullRow]
end;

procedure TForm1.SpeedButton2Click(Sender: TObject);
begin
  NextDBGrid1.GridStyle := gsReport;
  NextDBGrid1.Options := NextDBGrid1.Options + [goSelectFullRow]
end;

procedure TForm1.NextDBGrid1FooterClick(Sender: TObject; ACol: Integer);
begin
  NextDBGrid1.CalculateFooter;
end;

procedure TForm1.SpeedButton4Click(Sender: TObject);
begin
  if DataSource1.DataSet.Active and ColorDialog1.Execute then
  begin
    DataSource1.DataSet.Edit;
    DBEdit4.Field.AsInteger := ColorDialog1.Color;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  NextDBGrid1.ScrollToRow(20);
end;

procedure TForm1.NextDBGrid1MouseWheelDown(Sender: TObject;
  Shift: TShiftState; MousePos: TPoint; var Handled: Boolean);
begin
  Handled := True;
end;

procedure TForm1.NextDBGrid1CellColoring(Sender: TObject; ACol,
  ARow: Integer; var CellColor, GridColor: TColor; CellState: TCellState);
//var
//  Field: TField;
begin
{  if csSelected in CellState then Exit;
  Field := NextDBGrid1.Columns[1].Field;
  if Assigned(Field) and not Field.IsNull then
  begin
    if ARow < NextDBGrid1.RowCount then
    begin
      CellColor := StringToColor(Field.AsString);
    end else CellColor := clWindow;
  end;}
end;

procedure TForm1.NextDBGrid1CellHint(Sender: TObject; ACol, ARow: Integer;
  var Value: WideString);
begin
  if ACol = 5 then
  begin
    if ARow = 5 then Value := 'OnCellHint' + #13#10 + 'This cell have own hint from OnCellHint event.';
  end;
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
  NextDBGrid1.SelectFirstRow();
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
  NextDBGrid1.SaveToHtml('test.htm', [hsSaveHeaders, hsCreateStyleSheet]);
end;

procedure TForm1.Button7Click(Sender: TObject);
begin
  NextDBGrid1.DataSource.DataSet.Edit;
  NextDBGrid1.DataSource.DataSet.FieldByName('Project').Clear;
  NextDBGrid1.DataSource.DataSet.Post;
end;

procedure TForm1.Button8Click(Sender: TObject);
begin
  DataModule1.ADOTable1.Locate('Project', 'New Project 14', []);
end;

procedure TForm1.Button9Click(Sender: TObject);
begin
  DataSource1.DataSet.Locate('Project', 'New Project 9', [loCaseInsensitive]);
end;

end.
