unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    NextDBGrid1: TNextDBGrid;
    Table1: TTable;
    DataSource1: TDataSource;
    Button2: TButton;
    Memo1: TMemo;
    RadioButton1: TRadioButton;
    RadioButton2: TRadioButton;
    Query1: TQuery;
    Button3: TButton;
    Button4: TButton;
    ImageList1: TImageList;
    CheckBox2: TCheckBox;
    Button7: TButton;
    Button8: TButton;
    ListBox1: TListBox;
    CheckBox3: TCheckBox;
    MainMenu1: TMainMenu;
    Commands1: TMenuItem;
    ChangeFieldName1: TMenuItem;
    Convert1: TMenuItem;
    ClearRows1: TMenuItem;
    SetRecordFromCode1: TMenuItem;
    GroupBox1: TGroupBox;
    Label1: TLabel;
    Label2: TLabel;
    DBNavigator1: TDBNavigator;
    Label3: TLabel;
    DBText1: TDBText;
    Label4: TLabel;
    DBText2: TDBText;
    About1: TMenuItem;
    Label5: TLabel;
    DBText3: TDBText;
    DBEdit1: TDBEdit;
    DBEdit2: TDBEdit;
    DBCheckBox1: TDBCheckBox;
    Label6: TLabel;
    ConvertSalaryColumn1: TMenuItem;
    LastNameColumnasCoboboxColumn1: TMenuItem;
    ScrollToRow01: TMenuItem;
    CheckBox4: TCheckBox;
    PopupMenu1: TPopupMenu;
    MakeEditable1: TMenuItem;
    RowHeightofSelectedRow1: TMenuItem;
    Button1: TButton;
    Button9: TButton;
    Button5: TButton;
    SpeedButton1: TSpeedButton;
    SaveDialog1: TSaveDialog;
    XPManifest1: TXPManifest;
    PrintDialog1: TPrintDialog;
    procedure Button2Click(Sender: TObject);
    procedure RadioButton2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Label1MouseEnter(Sender: TObject);
    procedure Label1MouseLeave(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure NextDBGrid1CellColoring(Sender: TObject; ACol, ARow: Integer;
      var CellColor, GridColor: TColor; CellState: TCellState);
    procedure Button4Click(Sender: TObject);
    procedure NextDBGrid1SortColumn(Sender: TObject; ACol: Integer;
      Ascending: Boolean);
    procedure CheckBox2Click(Sender: TObject);
    procedure Button7Click(Sender: TObject);
    procedure Button8Click(Sender: TObject);
    procedure NextDBGrid1SelectCell(Sender: TObject; ACol, ARow: Integer);
    procedure CheckBox3Click(Sender: TObject);
    procedure NextDBGrid1Change(Sender: TObject; ACol, ARow: Integer);
    procedure ChangeFieldName1Click(Sender: TObject);
    procedure Convert1Click(Sender: TObject);
    procedure ClearRows1Click(Sender: TObject);
    procedure SetRecordFromCode1Click(Sender: TObject);
    procedure About1Click(Sender: TObject);
    procedure ConvertSalaryColumn1Click(Sender: TObject);
    procedure LastNameColumnasCoboboxColumn1Click(Sender: TObject);
    procedure NextDBGrid1ApplyCell(Sender: TObject; ACol, ARow: Integer;
      var Value: WideString);
    procedure ScrollToRow01Click(Sender: TObject);
    procedure NextDBGrid1CellFormating(Sender: TObject; ACol,
      ARow: Integer; Value: WideString; var TextColor: TColor;
      var FontStyle: TFontStyles; CellState: TCellState);
    procedure CheckBox4Click(Sender: TObject);
    procedure MakeEditable1Click(Sender: TObject);
    procedure NextDBGrid1MeasuringRowHeight(Sender: TObject;
      Index: Integer; var RowHeight: Integer);
    procedure RowHeightofSelectedRow1Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure Button9Click(Sender: TObject);
    procedure NextDBGrid1AfterEdit(Sender: TObject; ACol, ARow: Integer;
      Value: WideString);
    procedure NextDBGrid1EditAccept(Sender: TObject; ACol, ARow: Integer;
      Value: WideString; var Accept: Boolean);
    procedure NextDBGrid1Edit(Sender: TObject; ACol, ARow: Integer;
      Value: WideString);
    procedure Button5Click(Sender: TObject);
    procedure SpeedButton1Click(Sender: TObject);
    procedure NextDBGrid1ColumnCreate(Sender: TObject; Field: TField;
      var ColumnClass: TNxDBColumnClass; var AddColumn: Boolean);
    procedure NextDBGrid1ColumnAdded(Sender: TObject;
      Column: TNxDBCustomColumn);
  private
    { Private declarations }
  public

  end;

var
  Form1: TForm1;
  ColorOfRows: TColor = $00FFFAFA;
  Path: string;

implementation

uses
	NxColumnClasses;

{$R *.dfm}

procedure TForm1.Button2Click(Sender: TObject);
begin
  if RadioButton2.Checked	then Query1.SQL.Text := Memo1.Text;
	{ note: When you set Active to true in DataSet, Grid will be
				  automaticaly loaded with Data from DataSet. If Grid
          does not contain any column yet, columns will be added
          automaticaly. Each field in DataSet will get appropriate
          column type. }
  NextDBGrid1.Columns.Clear;
  DataSource1.DataSet.Active := True;
end;

procedure TForm1.RadioButton2Click(Sender: TObject);
begin
  NextDBGrid1.Columns.Clear;
	Memo1.Enabled := RadioButton2.Checked;
  if RadioButton2.Checked then DataSource1.DataSet := Query1 else DataSource1.DataSet := Table1;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
  DataSource1.DataSet.Active := False;
  NextDBGrid1.Columns.Clear;
end;

procedure TForm1.Label1MouseEnter(Sender: TObject);
begin
  TLabel(Sender).Font.Style := [fsUnderline];
end;

procedure TForm1.Label1MouseLeave(Sender: TObject);
begin
  TLabel(Sender).Font.Style := [];
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  C: HCURSOR;
begin
  C := LoadCursor(0, IDC_HAND);
  if C <> 0 then Screen.Cursors[crHandPoint] := C;
  Table1.DatabaseName := ExtractFilePath(ParamStr(0)) + '..\data';
  Table1.TableName := 'employee';

  Query1.DatabaseName := ExtractFilePath(ParamStr(0)) + '..\data';
  DataSource1.DataSet := Table1;

  if CheckBox3.Checked
    then NextDBGrid1.DataAwareOptions := NextDBGrid1.DataAwareOptions + [doBufferRecords]
    else NextDBGrid1.DataAwareOptions := NextDBGrid1.DataAwareOptions - [doBufferRecords];

  Path := ExtractFilePath(ParamStr(0));
end;

procedure TForm1.NextDBGrid1CellColoring(Sender: TObject; ACol,
  ARow: Integer; var CellColor, GridColor: TColor; CellState: TCellState);
begin
 	if not(csSelected in CellState) then
  begin
    if (ARow mod 2 = 0) and (CheckBox4.Checked) then CellColor := RGB(245, 245, 245);
    if (ACol = 2) and (NextDBGrid1.Columns[ACol].Field.AsString = 'Chris') then CellColor := clSkyBlue;
    //    if NextDBGrid1.Cells[2, ARow] = 'Chris' then CellColor := clSkyBlue;
  end;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
	if not DataSource1.DataSet.Active then Exit;
  if DataSource1.DataSet is TQuery then
  begin
    ShowMessage('Rows can not be added in "ReadOnly" DataSet such as TQuery. You can activate Table DataSet and to try again.');
    Exit;
  end;
  NextDBGrid1.Columns[0].DefaultValue := IntToStr(Random(1000));
  NextDBGrid1.Columns[1].DefaultValue := 'new Last Name';
  NextDBGrid1.Columns[2].DefaultValue := 'new First Name';
  NextDBGrid1.Columns[3].DefaultValue := IntToStr(Random(100));
  NextDBGrid1.Columns[4].DefaultValue := DateToStr(Today);
  NextDBGrid1.Columns[5].DefaultValue := IntToStr(Random(2));
  NextDBGrid1.Columns[6].DefaultValue := IntToStr(Random(100000));
  NextDBGrid1.AddRow;
end;

procedure TForm1.NextDBGrid1SortColumn(Sender: TObject; ACol: Integer;
  Ascending: Boolean);
var
	DirectionStr: string;
begin
	if TNxDBCustomColumn(NextDBGrid1.Columns[ACol]).FieldName = '' then Exit;

	if Ascending then DirectionStr := 'ASC'
  	else DirectionStr := 'DESC';

  Query1.SQL.Text := Memo1.Text + ' ORDER BY '
  	+ TNxDBCustomColumn(NextDBGrid1.Columns[ACol]).FieldName
    + ' ' + DirectionStr;

  Query1.Open;
end;

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

procedure TForm1.Button7Click(Sender: TObject);
begin
	if not DataSource1.DataSet.Active then Exit;
  NextDBGrid1.DeleteRow(NextDBGrid1.SelectedRow);
  if NextDBGrid1.SelectedRow >= NextDBGrid1.RowCount then NextDBGrid1.SelectedRow := NextDBGrid1.RowCount - 1;
end;

procedure TForm1.Button8Click(Sender: TObject);
begin
	if not DataSource1.DataSet.Active then Exit;
  DataSource1.DataSet.Edit;
  DataSource1.DataSet.Fields[1].AsString := 'test';
  DataSource1.DataSet.Post;
end;

procedure TForm1.NextDBGrid1SelectCell(Sender: TObject; ACol,
  ARow: Integer);
begin
  ListBox1.Items.Add('OnSelect ' + IntToStr(ACol) + ', ' + IntToStr(ARow));
end;

procedure TForm1.CheckBox3Click(Sender: TObject);
begin
  DataSource1.DataSet.Active := False;
	if CheckBox3.Checked then NextDBGrid1.DataAwareOptions := NextDBGrid1.DataAwareOptions + [doBufferRecords]
  	else NextDBGrid1.DataAwareOptions := NextDBGrid1.DataAwareOptions - [doBufferRecords];
  Button9.Enabled := CheckBox3.Checked; 
end;

procedure TForm1.NextDBGrid1Change(Sender: TObject; ACol, ARow: Integer);
begin
  ListBox1.Items.Add('OnChange ' + IntToStr(ACol) + ', ' + IntToStr(ARow));
end;

procedure TForm1.ChangeFieldName1Click(Sender: TObject);
begin
	{ note: Here we will change FieldName for second column,
  				by setting FieldName property. Each column have
          own FieldName property }
  NextDBGrid1.Columns[2].Header.Caption := 'LastName (Changed)';
  TNxDBCustomColumn(NextDBGrid1.Columns[2]).FieldName := 'LastName';
end;

procedure TForm1.Convert1Click(Sender: TObject);
begin
  NextDBGrid1.Columns.Delete(5);
  NextDBGrid1.Columns.Insert(TNxDBImageColumn, 5);
  with NextDBGrid1.Columns[5] as TNxDBImageColumn do
  begin
	  FieldName := 'ImageNo';
  	Position := 5;
  	Header.Caption := 'Image';
  	Alignment := taCenter;
  	Images := ImageList1;
  end;
end;

procedure TForm1.ClearRows1Click(Sender: TObject);
begin
	if MessageDlg('Are you sure that you want to delete all records?', mtWarning, [mbYes, mbNo], 0) = mrYes then NextDBGrid1.ClearRows;
end;

procedure TForm1.SetRecordFromCode1Click(Sender: TObject);
begin
  NextDBGrid1.Cells[1, 2] := 'text from code';
end;

procedure TForm1.About1Click(Sender: TObject);
begin
  ShellExecute(Handle, 'open', PChar('http://www.bergsoft.net'), '', '', SW_NORMAL);
end;

procedure TForm1.ConvertSalaryColumn1Click(Sender: TObject);
begin
  NextDBGrid1.Columns.Delete(6);
  NextDBGrid1.Columns.Insert(TNxDBProgressColumn, 6);
  with NextDBGrid1.Columns[6] as TNxDBProgressColumn do
  begin
	  FieldName := 'Salary';
  	Max := 50000;
  	Margin := 3;
	  Position := 6;
  	Header.Caption := 'Image';
  	Alignment := taCenter;
  end;
end;

procedure TForm1.LastNameColumnasCoboboxColumn1Click(Sender: TObject);
begin
  NextDBGrid1.Columns.Delete(1);
  NextDBGrid1.Columns.Insert(TNxDBComboBoxColumn, 1);
  with NextDBGrid1.Columns[1] as TNxDBComboBoxColumn do
  begin
    FieldName := 'LastName';
    Position := 1;
    Items.Add('Al');
    Items.Add('Andrew');
    Items.Add('Ben');
    Items.Add('Jim');
    Items.Add('Jack');
    Items.Add('Steve');
  end;
end;

procedure TForm1.NextDBGrid1ApplyCell(Sender: TObject; ACol, ARow: Integer;
  var Value: WideString);
begin
// Comment - This lines of code will show how to change cell display in last moment
// This code will flip checkboxes in InOffice column
{*	if ACol = 7 then
		if Value = 'True' then Value := 'False' else Value := 'True'; *}
end;

procedure TForm1.ScrollToRow01Click(Sender: TObject);
begin
  NextDBGrid1.ScrollToRow(0);
  NextDBGrid1.SelectedRow := 0;
end;

procedure TForm1.NextDBGrid1CellFormating(Sender: TObject; ACol,
  ARow: Integer; Value: WideString; var TextColor: TColor;
  var FontStyle: TFontStyles; CellState: TCellState);
begin
  if (Value = 'Bill') or (Value = 'Parker') or (Value = 'Ann') then
  begin
    TextColor := clBlue;
    FontStyle := [fsItalic];
  end;
  if (Value = 'Lambert') or (Value = 'Osborne') then
  begin
    TextColor := clRed;
    FontStyle := [fsStrikeOut];
  end;       
end;

procedure TForm1.CheckBox4Click(Sender: TObject);
begin
  NextDBGrid1.Refresh;
end;

procedure TForm1.MakeEditable1Click(Sender: TObject);
begin
	if RadioButton2.Checked then
  begin
    MessageDlg('You can not edit read-only DataSet. Select From Table radio button to edit TTable.', mtWarning, [mbOk], 0);
    Exit;
  end;
  if (NextDBGrid1.SelectedColumn >= 0) and (NextDBGrid1.SelectedColumn < NextDBGrid1.Columns.Count) then
  begin
    NextDBGrid1.Columns[NextDBGrid1.SelectedColumn].Options := NextDBGrid1.Columns[NextDBGrid1.SelectedColumn].Options + [coEditing];
  end;
end;

procedure TForm1.NextDBGrid1MeasuringRowHeight(Sender: TObject;
  Index: Integer; var RowHeight: Integer);
begin
//  if Index = 5 then RowHeight := 45;
end;

procedure TForm1.RowHeightofSelectedRow1Click(Sender: TObject);
begin
  ShowMessage(IntToStr(NextDBGrid1.RowHeight[NextDBGrid1.SelectedRow]));
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage('Total Rows: ' + IntToStr(NextDBGrid1.RowCount));
end;

procedure TForm1.Button9Click(Sender: TObject);
begin
  if NextDBGrid1.Columns.Count < 7 then
  begin
    ShowMessage('Column Not Exist.');
    Exit;
  end;
  NextDBGrid1.Columns[6].Footer.FormulaKind := fkSum;
  NextDBGrid1.CalculateFooter;
end;

procedure TForm1.NextDBGrid1AfterEdit(Sender: TObject; ACol, ARow: Integer;
  Value: WideString);
begin
  ListBox1.Items.Add('OnAfterEdit ' + IntToStr(ACol) + ', ' + IntToStr(ARow) + ', ' + Value);
end;

procedure TForm1.NextDBGrid1EditAccept(Sender: TObject; ACol,
  ARow: Integer; Value: WideString; var Accept: Boolean);
begin
  ListBox1.Items.Add('OnEditAccept ' + IntToStr(ACol) + ', ' + IntToStr(ARow) + ', ' + Value);
  if Value = '' then
  begin
    ShowMessage('OnEditAccept event - No empty values are accepted.');
    Accept := False;
  end;
end;

procedure TForm1.NextDBGrid1Edit(Sender: TObject; ACol, ARow: Integer;
  Value: WideString);
begin
  ListBox1.Items.Add('OnEdit ' + IntToStr(ACol) + ', ' + IntToStr(ARow));
end;

procedure TForm1.Button5Click(Sender: TObject);
begin
  DataSource1.DataSet.Locate('LastName', 'Brown', [loCaseInsensitive]);
end;

procedure TForm1.SpeedButton1Click(Sender: TObject);
begin
  if SaveDialog1.Execute then
  begin
    NextDBGrid1.SaveToTextFile(SaveDialog1.FileName);
  end;
end;

procedure TForm1.NextDBGrid1ColumnCreate(Sender: TObject; Field: TField;
  var ColumnClass: TNxDBColumnClass; var AddColumn: Boolean);
begin
  if Field.FieldName = 'Job' then ColumnClass := TNxDBListColumn;
  if Field.FieldName = 'Rating' then ColumnClass := TNxDBRateColumn;
  if Field.FieldName = 'Progress' then ColumnClass := TNxDBProgressColumn;
  if Field.FieldName = 'ImageNo' then ColumnClass := TNxDBImageColumn;
end;

procedure TForm1.NextDBGrid1ColumnAdded(Sender: TObject;
  Column: TNxDBCustomColumn);
begin
  if Column.Field.DisplayName = 'LastName' then
  begin
    Column.Header.MultiLine := True;
    Column.Header.Caption := 'Last' + #13#10 + 'Name';
  end;

  if Column is TNxDBIncrementColumn then
  begin
    Column.Header.Caption := 'Increment' + #13#10 + 'Column';
    Column.Header.MultiLine := True;
  end;

  if Column is TNxDBListColumn then
  begin
    with Column as TNxDBListColumn do
    begin
      Items.Add('<select job>');
      Items.Add('Sales');
      Items.Add('Marketing');
      Items.Add('Accounting');
      Items.Add('Production');
    end;
  end;

  if Column is TNxDBProgressColumn then
  begin
    with Column as TNxDBProgressColumn do
    begin
      HighValueBound := 75;
      HighValueColor := clGreen;
      LowValueBound := 25;
      LowValueColor := clRed;
      ProgressStyle := psBoxes;
      ShowText := True;
    end;
  end;

  if Column is TNxDBCheckBoxColumn then
  begin
    with Column as TNxDBCheckBoxColumn do
    begin
      Alignment := taCenter;
      ValueChecked := 'True';
      ValueUnchecked := 'False';
      Width := 50;
    end;
  end;

  if Column is TNxDBImageColumn then
  begin
    with Column as TNxDBImageColumn do
    begin
      Alignment := taCenter;
      Images := ImageList1;
      Width := 60;
    end;
  end;

  if Column is TNxDBNumberColumn then
  begin
    with Column as TNxDBNumberColumn do
    begin
      FormatMask := '#,##0.00';
      Width := 60;
    end;
  end;

  if Column is TNxDBRateColumn then
    with Column as TNxDBRateColumn do
    begin
      Alignment := taCenter;
      EmptyGlyph.LoadFromFile(Path + 'empty.bmp');
      EmptyGlyph.TransparentColor := clFuchsia;
      EmptyGlyph.Transparent := True;
      Glyph.LoadFromFile(Path + 'flag.bmp');
      Glyph.TransparentColor := clFuchsia;
      Glyph.Transparent := True;
    end;
end;

end.
