unit NxDBFieldsEditor6;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics,
  Controls, Forms, Dialogs, StdCtrls, DB, TypInfo,
  NxColumns6, NxGridView6, NxControls6, NxCustomGrid6, NxVirtualGrid6, NxGrid6, NxDBGrid6;

type
  TBeforeAddFieldEvent = procedure (Sender: TObject; FieldName: string;
    var Checked: Boolean) of object;

  TFieldsDialog = class(TForm)
    Button1: TButton;
    Button2: TButton;
    FieldsGrid: TNextGrid6;
    NxReportGridView61: TNxReportGridView6;
    NxCheckBoxColumn61: TNxCheckBoxColumn6;
    NxTextColumn61: TNxTextColumn6;
    NxListColumn61: TNxListColumn6;
    Button3: TButton;
    Button4: TButton;
    Button5: TButton;
    NxTextColumn62: TNxTextColumn6;
    Button6: TButton;
    procedure FormShow(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
    procedure Button5Click(Sender: TObject);
    procedure Button6Click(Sender: TObject);
  private
    { Prop. Fields }
    FDataSet: TDataSet;
    FOnBeforeAddField: TBeforeAddFieldEvent;
    function GetClassAt(Index: Integer): TNxColumnClass;
  protected
    procedure DoBeforeAddField(FieldName: string; var Checked: Boolean); dynamic;
  public
    { Properties }
    property DataSet: TDataSet read FDataSet write FDataSet;
    property ClassAt[Index: Integer]: TNxColumnClass read GetClassAt;
    { Events }
    property OnBeforeAddField: TBeforeAddFieldEvent read FOnBeforeAddField write FOnBeforeAddField;
  end;

var
  FieldsDialog: TFieldsDialog;

implementation

{$R *.dfm}

procedure TFieldsDialog.Button3Click(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to Pred(FieldsGrid.RowCount) do
  begin
    FieldsGrid.Cell[NxCheckBoxColumn61.Index, i].AsBoolean := True;
  end;
end;

procedure TFieldsDialog.Button4Click(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to Pred(FieldsGrid.RowCount) do
  begin
    FieldsGrid.Cell[NxCheckBoxColumn61.Index, i].AsBoolean := False;
  end;
end;

procedure TFieldsDialog.Button5Click(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to Pred(FieldsGrid.RowCount) do
  begin
    FieldsGrid.Cell[NxCheckBoxColumn61.Index, i].InvertValue;
  end;
end;

procedure TFieldsDialog.Button6Click(Sender: TObject);
var
  i: Integer;
begin
  for i := 0 to Pred(FieldsGrid.RowCount) do
  begin
    if FieldsGrid.Selected[i] then
      FieldsGrid.Cell[NxCheckBoxColumn61.Index, i].AsBoolean := not FieldsGrid.Cell[NxCheckBoxColumn61.Index, i].AsBoolean;
  end;
end;

procedure TFieldsDialog.DoBeforeAddField(FieldName: string; var Checked: Boolean);
begin
  if Assigned(FOnBeforeAddField) then FOnBeforeAddField(Self, FieldName, Checked);
end;

procedure TFieldsDialog.FormShow(Sender: TObject);
var
  i: Integer;
  ColClass: TNxColumnClass;
  DataType: TFieldType;
  FieldName, DataTypeStr: string;
  Checked: Boolean;
begin

  for i := 0 to Pred(DataSet.FieldDefs.Count) do
  begin
    FieldName := DataSet.FieldDefs[i].Name;

    DataType := DataSet.FieldDefs[i].DataType;
    DataTypeStr := GetEnumName(TypeInfo(TFieldType), Ord(DataType));

    ColClass := GetColumnClass(DataType);

    Checked := True;

    { Trigger Event, if set }
    DoBeforeAddField(FieldName, Checked);

    with FieldsGrid.AddRow do
    begin
      Cells[0].AsBoolean := Checked;
      Cells[1].AsString := FieldName;
      Cells[2].AsString := DataTypeStr;
      Cells[3].AsInteger := NxListColumn61.Items.IndexOf(ColClass.ClassName);
    end;

  end;
end;

function TFieldsDialog.GetClassAt(Index: Integer): TNxColumnClass;
var
  Str: String;
begin
  Str := NxListColumn61.Items[FieldsGrid.Cell[NxListColumn61.Index, Index].AsInteger];
  Result := TNxColumnClass(GetClass(Str));
end;


end.
