unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    DataSource1: TDataSource;
    StatusBar1: TStatusBar;
    PopupMenu1: TPopupMenu;
    MakeEditable1: TMenuItem;
    ImageList1: TImageList;
    radFromTable: TRadioButton;
    radFromQuery: TRadioButton;
    Memo1: TMemo;
    btnOpen: TButton;
    Button4: TButton;
    NextDBGrid1: TNextDBGrid;
    GroupBox1: TGroupBox;
    DBEdit1: TDBEdit;
    Label1: TLabel;
    Label2: TLabel;
    DBEdit2: TDBEdit;
    DBNavigator1: TDBNavigator;
    TNxDBNumberColumn1: TNxDBNumberColumn;
    TNxDBTextColumn1: TNxDBTextColumn;
    TNxDBNumberColumn2: TNxDBNumberColumn;
    TNxDBTextColumn2: TNxDBTextColumn;
    TNxDBTextColumn3: TNxDBTextColumn;
    TNxDBTextColumn4: TNxDBTextColumn;
    TNxDBTextColumn5: TNxDBTextColumn;
    TNxDBTextColumn6: TNxDBTextColumn;
    procedure NextDBGrid1SortColumn(Sender: TObject; ACol: Integer;
      Ascending: Boolean);
    procedure btnOpenClick(Sender: TObject);
    procedure radFromTableClick(Sender: TObject);
    procedure radFromQueryClick(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure NextDBGrid1InputAccept(Sender: TObject; var Accept: Boolean);
    procedure NextDBGrid1SelectCell(Sender: TObject; ACol, ARow: Integer);
    procedure NextDBGrid1FooterClick(Sender: TObject; ACol: Integer);
    procedure MakeEditable1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses Unit2;

{$R *.dfm}

procedure TForm1.NextDBGrid1SortColumn(Sender: TObject; ACol: Integer;
  Ascending: Boolean);
var
	DirectionStr: string;
begin
  if radFromQuery.Checked then { Query }
  begin
    if Ascending then DirectionStr := 'ASC'
      else DirectionStr := 'DESC';

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

    datDataSets.ADOQuery1.Active := True;
  end else
  begin
	  if Ascending then DirectionStr := 'ASC'
  	  else DirectionStr := 'DESC';

    datDataSets.ADOTable1.Sort := TNxDBCustomColumn(NextDBGrid1.Columns[ACol]).FieldName + ' ' + DirectionStr;
  end;
end;

procedure TForm1.btnOpenClick(Sender: TObject);
begin
  if radFromTable.Checked then
  begin
    DataSource1.DataSet := datDataSets.ADOTable1;
  end else
  begin
    datDataSets.ADOQuery1.SQL.Text := Memo1.Text;
    datDataSets.ADOQuery1.Open;
    DataSource1.DataSet := datDataSets.ADOQuery1;
  end;
  DataSource1.DataSet.Active := True;
  StatusBar1.Panels[0].Text := 'DataSet Active. Total ' + IntToStr(NextDBGrid1.RowCount) + ' rows.';
end;

procedure TForm1.radFromTableClick(Sender: TObject);
begin
  Memo1.Enabled := False;
  NextDBGrid1.DataSource.DataSet.Close;
end;

procedure TForm1.radFromQueryClick(Sender: TObject);
begin
  Memo1.Enabled := True;
  NextDBGrid1.DataSource.DataSet.Close;
end;

procedure TForm1.Button4Click(Sender: TObject);
begin
  DataSource1.DataSet.Active := False;
end;

procedure TForm1.NextDBGrid1InputAccept(Sender: TObject;
  var Accept: Boolean);
var
  i: Integer;
begin
  Accept := False;
  if DataSource1.DataSet is TADOQuery then
  begin
    ShowMessage('You can not add new record in read-only DataSet.');
    Exit;
  end;
  for i := 0 to NextDBGrid1.Columns.Count - 1 do
  begin
    if NextDBGrid1.Columns[i].InputValue <> '' then Accept := True;
  end;
end;

procedure TForm1.NextDBGrid1SelectCell(Sender: TObject; ACol,
  ARow: Integer);
begin
  StatusBar1.Panels[1].Text := 'Current Cell: ' + IntToStr(ACol) + ',' + IntToStr(ARow);
end;

procedure TForm1.NextDBGrid1FooterClick(Sender: TObject; ACol: Integer);
begin
  case NextDBGrid1.Columns[ACol].Field.DataType of
    ftInteger, ftSmallint, ftFloat: NextDBGrid1.CalculateFooter;
  end;
end;

procedure TForm1.MakeEditable1Click(Sender: TObject);
begin
	if radFromQuery.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;

end.
