unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, DBClient, NxScrollControl, NxCustomGridControl,
  NxCustomGrid, NxDBGrid, Provider, NxColumns, NxDBColumns, ExtCtrls,
  DBCtrls, Grids, DBGrids, StdCtrls, Menus;

type
  TForm1 = class(TForm)
    NextDBGrid1: TNextDBGrid;
    DataSource1: TDataSource;
    DBNavigator1: TDBNavigator;
    Button1: TButton;
    ClientDataSet1: TClientDataSet;
    Button2: TButton;
    btnLoad: TButton;
    Button3: TButton;
    Button4: TButton;
    PopupMenu1: TPopupMenu;
    Delete1: TMenuItem;
    Button5: TButton;
    Button6: TButton;
    procedure Button1Click(Sender: TObject);
    procedure NextDBGrid1ColumnAdded(Sender: TObject;
      Column: TNxDBCustomColumn);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure btnLoadClick(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure NextDBGrid1EditAccept(Sender: TObject; ACol, ARow: Integer;
      Value: WideString; var Accept: Boolean);
    procedure NextDBGrid1CellColoring(Sender: TObject; ACol, ARow: Integer;
      var CellColor, GridColor: TColor; CellState: TCellState);
    procedure NextDBGrid1MouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
    procedure NextDBGrid1DeleteRecord(Sender: TObject;
      var Accept: Boolean);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
    procedure NextDBGrid1ApplyCell(Sender: TObject; ACol, ARow: Integer;
      var Value: WideString);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  NewHoverRow, OldHoverRow: Integer;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
  with ClientDataSet1 do
  begin
    FieldDefs.Add('Id', ftInteger);
    FieldDefs.Add('Project', ftString, 32);
    FieldDefs.Add('Budget', ftFloat);
    FieldDefs.Add('Status', ftInteger);
    IndexDefs.Add('IdIndex', 'Id', [ixPrimary, ixUnique]);
    CreateDataSet;
    Open;
  end;
end;

procedure TForm1.NextDBGrid1ColumnAdded(Sender: TObject;
  Column: TNxDBCustomColumn);
begin
  Column.Options := Column.Options + [coEditing];
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  NextDBGrid1.Options := NextDBGrid1.Options + [goMultiSelect, goSelectFullRow];
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  ClientDataSet1.SaveToFile('data.xml');
end;

procedure TForm1.btnLoadClick(Sender: TObject);
begin
  ClientDataSet1.LoadFromFile('data.xml');
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  with ClientDataSet1 do
  begin
    Insert;
    Fields[0].AsInteger := RecordCount + 1;
    Fields[1].AsString := 'New Project';
    Fields[2].AsFloat := 10000;
    Fields[3].AsInteger := 50;
    Post;
  end;
end;

procedure TForm1.NextDBGrid1EditAccept(Sender: TObject; ACol,
  ARow: Integer; Value: WideString; var Accept: Boolean);
begin
  if Value = 'none' then Accept := False;
end;

procedure TForm1.NextDBGrid1CellColoring(Sender: TObject; ACol,
  ARow: Integer; var CellColor, GridColor: TColor; CellState: TCellState);
var
  CursorPoint: TPoint;
begin
  if csSelected in CellState then Exit;
  GetCursorPos(CursorPoint);
  CursorPoint := NextDBGrid1.ScreenToClient(CursorPoint);
  if NextDBGrid1.GetRowAtPos(CursorPoint.X, CursorPoint.Y) = ARow then
  begin
    CellColor := clSkyBlue;
  end;
end;

procedure TForm1.NextDBGrid1MouseMove(Sender: TObject; Shift: TShiftState;
  X, Y: Integer);
begin
  NewHoverRow := NextDBGrid1.GetRowAtPos(X, Y);
  if NewHoverRow <> OldHoverRow then
  begin
    NextDBGrid1.RefreshRow(OldHoverRow);
    OldHoverRow := NewHoverRow;
    NextDBGrid1.RefreshRow(NewHoverRow);
  end;
end;

procedure TForm1.NextDBGrid1DeleteRecord(Sender: TObject;
  var Accept: Boolean);
begin
  Accept := MessageDlg('Are you sure?', mtConfirmation, [mbYes, mbNo], 0) = mrYes;
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
  ClientDataSet1.Delete;
end;

procedure TForm1.Button6Click(Sender: TObject);
begin
  if ClientDataSet1.State = dsEdit then
  begin
    ClientDataSet1.Fields[NextDBGrid1.SelectedColumn].Clear;
    NextDBGrid1.Columns[NextDBGrid1.SelectedColumn].NullText := 'NULL';
  end;
end;

procedure TForm1.NextDBGrid1ApplyCell(Sender: TObject; ACol, ARow: Integer;
  var Value: WideString);
begin
  if Value = 'NULL' then Value := '<NULL>';
end;

end.
