{ ------------------------------------------------------------- }
{ Purpose : NextDBGrid 6                                        }
{ By      : Bojan Nikolic                                       }
{ For     : BergSoft                                            }
{ ------------------------------------------------------------- }
{ Copyright (C) BergSoft.net 2014. All Rights Reserved.         }
{ ------------------------------------------------------------- }
unit NxDBColumns6;

interface

uses
  { Standard }
  Classes, Types, Windows, Graphics, SysUtils,
  { Data }
  DB,
  { Next Standard 6 }
  NxIntf6, NxEdit6,
  { NextGrid 6 }
  NxColumns6;

type
  INxDataBinding = interface;

  { TNxDataBinding }

  TNxDataBindingOptions = set of (dboAutoDeleteOnDeactivate,
    dboUseFieldProperties);

  TNxDataBinding = class(TPersistent)
  private
    FField: TField;
    FFieldName: string;
    FNullText: WideString;
    FOptions: TNxDataBindingOptions;
    FOwner: TNxColumn6;
    { Property Accessors }
    function GetDataSet: TDataSet;
    procedure SetFieldName(const Value: string);
    procedure SetField(const Value: TField);
    procedure SetNullText(const Value: WideString);
    procedure SetOptions(const Value: TNxDataBindingOptions);
  protected
    function GetOwner: TPersistent; override;
  public
    constructor Create(AOwner: TNxColumn6);
    procedure Assign(Source: TPersistent); override;
    procedure AssignDataBinding(Source: TObject);
    procedure AssignField;
    procedure AssignFieldProperties;
    { Properties }
    property DataSet: TDataSet read GetDataSet;
    property Field: TField read FField write SetField;
  published
    { Properties }
    property FieldName: string read FFieldName write SetFieldName;
    property NullText: WideString read FNullText write SetNullText;
    property Options: TNxDataBindingOptions read FOptions write SetOptions
      default [];
  end;

  INxDataBinding = interface
    ['{1127179A-2904-44EC-A289-11D8CB67AEBD}']
    function GetDataBinding: TNxDataBinding;
    property DataBinding: TNxDataBinding read GetDataBinding;
  end;

  INxDataOwner = interface
    ['{63BEBFEE-7DAC-4F98-851A-8B921DFD07B3}']
    function GetDataSet: TDataSet;
  end;

  { TNxTextDBColumn6 }

  TNxDBTextColumn6 = class(TNxTextColumn6, INxDataBinding)
  private
    FDataBinding: TNxDataBinding;
    function GetDataBinding: TNxDataBinding;
  public
    procedure Assign(Source: TPersistent); override;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    { Properties }
    property DataBinding: TNxDataBinding read GetDataBinding write FDataBinding;
  end;

  { TNxDBNumberColumn6 }

  TNxDBNumberColumn6 = class(TNxNumberColumn6, INxDataBinding)
  private
    FDataBinding: TNxDataBinding;
    function GetDataBinding: TNxDataBinding;
  public
    procedure Assign(Source: TPersistent); override;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    { Properties }
    property DataBinding: TNxDataBinding read GetDataBinding write FDataBinding;
  end;

  { TNxDBCheckBoxColumn6 }

  TNxValueSource = (vsCellValue, vsProperty);

  TNxDBCheckBoxColumn6 = class(TNxCheckBoxColumn6, INxDataBinding)
  private
    FDataBinding: TNxDataBinding;
    FValueChecked: WideString;
    FValueUnhecked: WideString;
    FValueSource: TNxValueSource;
    function GetDataBinding: TNxDataBinding;
    procedure SetValueChecked(const Value: WideString);
    procedure SetValueUnchecked(const Value: WideString);
    procedure SetValueSource(const Value: TNxValueSource);
  public
    procedure Assign(Source: TPersistent); override;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    { Methods }
    function GetValue(Cell: INxBase; ACol, ARow: Integer): Boolean; override;
    procedure SetValue(Cell: INxBase; ACol, ARow: Integer; Value: Boolean); override;
  published
    { Properties }
    property DataBinding: TNxDataBinding read GetDataBinding write FDataBinding;
    property ValueChecked: WideString read FValueChecked write SetValueChecked;
    property ValueSource: TNxValueSource read FValueSource write SetValueSource default vsCellValue;
    property ValueUnchecked: WideString read FValueUnhecked write SetValueUnchecked;
  end;

  { TNxDBDateColumn6 }

  TNxDBDateColumn6 = class(TNxDateColumn6, INxDataBinding)
  private
    FDataBinding: TNxDataBinding;
    function GetDataBinding: TNxDataBinding;
  public
    procedure Assign(Source: TPersistent); override;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    { Properties }
    property DataBinding: TNxDataBinding read GetDataBinding write FDataBinding;
  end;

  { TNxDBTimeColumn6 }

  TNxDBTimeColumn6 = class(TNxTimeColumn6, INxDataBinding)
  private
    FDataBinding: TNxDataBinding;
    function GetDataBinding: TNxDataBinding;
  public
    procedure Assign(Source: TPersistent); override;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    { Properties }
    property DataBinding: TNxDataBinding read GetDataBinding write FDataBinding;
  end;

  { TNxDBColorColumn6 }

  TNxDBColorColumn6 = class(TNxColorColumn6, INxDataBinding)
  private
    FDataBinding: TNxDataBinding;
    function GetDataBinding: TNxDataBinding;
  public
    procedure Assign(Source: TPersistent); override;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    { Properties }
    property DataBinding: TNxDataBinding read GetDataBinding write FDataBinding;
  end;

  { TNxDBMemoColumn6 }

  INxTextBlobContent = interface
    ['{328606AD-2F6E-4B1C-B3E7-73DBA447B727}']
    function GetExpandContent: Boolean;
    property ExpandContent: Boolean read GetExpandContent;
  end;

  TNxDBMemoColumn6 = class(TNxMemoColumn6, INxDataBinding, INxTextBlobContent)
  private
    FDataBinding: TNxDataBinding;
    FExpandContent: Boolean;
    function GetDataBinding: TNxDataBinding;
    function GetExpandContent: Boolean;
    procedure SetExpandContent(const Value: Boolean);
  public
    procedure Assign(Source: TPersistent); override;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    function CanEdit: Boolean; override;
  published
    { Properties }
    property DataBinding: TNxDataBinding read GetDataBinding write FDataBinding;
    property ExpandContent: Boolean read GetExpandContent write SetExpandContent default False;
  end;

  { TNxDBProgressColumn6 }

  TNxDBProgressColumn6 = class(TNxProgressColumn6, INxDataBinding)
  private
    FDataBinding: TNxDataBinding;
    function GetDataBinding: TNxDataBinding;
  public
    procedure Assign(Source: TPersistent); override;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    { Properties }
    property DataBinding: TNxDataBinding read GetDataBinding write FDataBinding;
  end;

  { TNxDBIncrementColumn6 }

  TNxDBIncrementColumn6 = class(TNxIncrementColumn6, INxDataBinding)
  private
    FDataBinding: TNxDataBinding;
    function GetDataBinding: TNxDataBinding;
  public
    procedure Assign(Source: TPersistent); override;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    { Properties }
    property DataBinding: TNxDataBinding read GetDataBinding write FDataBinding;
  end;

  { TNxDBButtonColumn6 }

  TNxDBButtonColumn6 = class(TNxButtonColumn6, INxDataBinding)
  private
    FDataBinding: TNxDataBinding;
    function GetDataBinding: TNxDataBinding;
  public
    procedure Assign(Source: TPersistent); override;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    { Properties }
    property DataBinding: TNxDataBinding read GetDataBinding write FDataBinding;
  end;

  { TNxDBIconColumn6 }

  TNxDBIconColumn6 = class(TNxIconColumn6, INxDataBinding)
  private
    FDataBinding: TNxDataBinding;
    function GetDataBinding: TNxDataBinding;
  public
    procedure Assign(Source: TPersistent); override;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    { Properties }
    property DataBinding: TNxDataBinding read GetDataBinding write FDataBinding;
  end;

  { TNxDBTrackbarColumn6 }

  TNxDBTrackbarColumn6 = class(TNxTrackbarColumn6, INxDataBinding)
  private
    FDataBinding: TNxDataBinding;
    function GetDataBinding: TNxDataBinding;
  public
    procedure Assign(Source: TPersistent); override;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    { Properties }
    property DataBinding: TNxDataBinding read GetDataBinding write FDataBinding;
  end;

  { TNxDBComboBoxColumn6 }

  TNxDBComboBoxColumn6 = class(TNxComboBoxColumn6, INxDataBinding)
  private
    FDataBinding: TNxDataBinding;
    function GetDataBinding: TNxDataBinding;
  public
    procedure Assign(Source: TPersistent); override;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    { Properties }
    property DataBinding: TNxDataBinding read GetDataBinding write FDataBinding;
  end;

  { TNxDBCheckComboBoxColumn6 }

  TNxDBCheckComboBoxColumn6 = class(TNxCheckBoxColumn6, INxDataBinding)
  private
    FDataBinding: TNxDataBinding;
    function GetDataBinding: TNxDataBinding;
  public
    procedure Assign(Source: TPersistent); override;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    { Properties }
    property DataBinding: TNxDataBinding read GetDataBinding write FDataBinding;
  end;

  { TNxDBGraphicColumn6 }

  INxGraphicBlobContent = interface
    ['{8D673639-64C5-4561-AD09-F5B53FA9ECBE}']
  end;

  TNxDBGraphicColumn6 = class(TNxGraphicColumn6, INxDataBinding, INxGraphicBlobContent)
  private
    FDataBinding: TNxDataBinding;
    function GetDataBinding: TNxDataBinding;
  public
    procedure Assign(Source: TPersistent); override;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    { Properties }
    property DataBinding: TNxDataBinding read GetDataBinding write FDataBinding;
  end;

  { TNxDBHTMLColumn6 }

  TNxDBHTMLColumn6 = class(TNxHTMLColumn6, INxDataBinding)
  private
    FDataBinding: TNxDataBinding;
    function GetDataBinding: TNxDataBinding;
  public
    procedure Assign(Source: TPersistent); override;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    { Properties }
    property DataBinding: TNxDataBinding read GetDataBinding write FDataBinding;
  end;

  { TNxDBListColumn6 }

  TNxDBListColumn6 = class(TNxListColumn6, INxDataBinding)
  private
    FDataBinding: TNxDataBinding;
    function GetDataBinding: TNxDataBinding;
  public
    procedure Assign(Source: TPersistent); override;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    { Properties }
    property DataBinding: TNxDataBinding read GetDataBinding write FDataBinding;
  end;

  { TNxDBToolbarColumn6 }

  TNxDBToolbarColumn6 = class(TNxToolbarColumn6, INxDataBinding)
  private
    FDataBinding: TNxDataBinding;
    function GetDataBinding: TNxDataBinding;
  public
    procedure Assign(Source: TPersistent); override;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    { Properties }
    property DataBinding: TNxDataBinding read GetDataBinding write FDataBinding;
  end;

  { ILookupContent }

  INxLookupContent = interface
    ['{D2334A45-0847-4E50-82CD-55E3B0B366D4}']
    procedure UpdateContent;
  end;

  { TNxLookupDataLink }

  TNxLookupDataLink = class(TDataLink)
  private
    FLookup: INxLookupContent;
  protected
  	procedure ActiveChanged; override;
    procedure UpdateData; override;
  public
    constructor Create(ALookup: INxLookupContent);
  end;

  { TNxLookupBinding }

  TNxLookupBinding = class(TPersistent)
  private
    FDataSource: TDataSource;
    FDetailFieldName: string;
    FKeyFieldName: string;
    FOnChange: TNotifyEvent;
    function GetActive: Boolean;
    procedure SetDataSource(const Value: TDataSource);
    procedure SetDetailFieldName(const Value: string);
    procedure SetKeyFieldName(const Value: string);
  protected
    procedure Changed;
    procedure BuildList(Strings: TStrings);
  public
    { Properties }
    property Active: Boolean read GetActive;
  published
    property DataSource: TDataSource read FDataSource write SetDataSource;
    property DetailFieldName: string read FDetailFieldName write SetDetailFieldName;
    property KeyFieldName: string read FKeyFieldName write SetKeyFieldName;
    property OnChange: TNotifyEvent read FOnChange write FOnChange;
  end;

  { TNxDBLookupColumn6 }

  TNxDBLookupColumn6 = class(TNxDBComboBoxColumn6, INxDataBinding, INxLookupContent)
  private
    { Property Fields }
    FDataLink: TNxLookupDataLink;
    FLookupBinding: TNxLookupBinding;
  private
{$IFDEF REGIONS}{$REGION 'INxLookupContent'}{$ENDIF}
    procedure UpdateContent;
{$IFDEF REGIONS}{$ENDREGION}{$ENDIF}
  protected
    procedure DoLookupBindingChange(Sender: TObject);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    { Methods }
    function GetDisplayText(Cell: INxBase; ACol, ARow: Integer): WideString; override;
    function GetEditText(Cell: INxBase; ACol, ARow: Integer): WideString; override;
    procedure Assign(Source: TPersistent); override;
    procedure AssignEditValue(Cell: INxBase; InplaceEdit: INxInplaceEdit); override;
  published
    { Properties }
    property LookupBinding: TNxLookupBinding read FLookupBinding write FLookupBinding;
  end;

  { TNxDBRateColumn6 }

  TNxDBRateColumn6 = class(TNxRateColumn6, INxDataBinding)
  private
    FDataBinding: TNxDataBinding;
    function GetDataBinding: TNxDataBinding;
  public
    procedure Assign(Source: TPersistent); override;
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    { Properties }
    property DataBinding: TNxDataBinding read GetDataBinding write FDataBinding;
  end;

implementation

uses
  StrUtils, Dialogs, Variants;

{ TNxDataBinding }

procedure TNxDataBinding.Assign(Source: TPersistent);
begin
  if Source is TNxDataBinding then
  begin
    FieldName := TNxDataBinding(Source).FieldName;
    NullText := TNxDataBinding(Source).NullText;
    Options := TNxDataBinding(Source).Options;
  end;
end;

procedure TNxDataBinding.AssignDataBinding(Source: TObject);
var
  Instance: INxDataBinding;
begin
  if Supports(Source, INxDataBinding, Instance) then
  begin
    Assign(Instance.DataBinding);
  end;
end;

procedure TNxDataBinding.AssignField;
begin
  if Assigned(DataSet) then
  begin
    Field := DataSet.FindField(FFieldName);
  end;
end;

procedure TNxDataBinding.AssignFieldProperties;
begin
  if (dboUseFieldProperties in Options)
    and Assigned(FField) then
  begin
    FOwner.Alignment := FField.Alignment;
    FOwner.Editing := not FField.ReadOnly;
    FOwner.Header.Caption := FField.DisplayName; // FField.DisplayLabel?
    FOwner.Visible := FField.Visible;

    if FOwner is TNxDateColumn6 then
    begin
      if FField is TDateField then
      begin
//        TNxDateColumn6(FOwner).FormatMask
      end;
    end;

    if FOwner is TNxFloatColumn6 then
    begin
      if FField is TNumericField then
      begin
        TNxFloatColumn6(FOwner).FormatMask := TNumericField(FField).DisplayFormat;
      end;
      if FField is TFloatField then
      begin
        TNxFloatColumn6(FOwner).Max := TFloatField(FField).MaxValue;
        TNxFloatColumn6(FOwner).Min := TFloatField(FField).MinValue;
        TNxFloatColumn6(FOwner).Precision := TFloatField(FField).Precision;
      end;
    end;
  end;
end;

constructor TNxDataBinding.Create(AOwner: TNxColumn6);
begin
  inherited Create;
  FField := nil;
  FOptions := [];
  FOwner := AOwner;
end;

function TNxDataBinding.GetDataSet: TDataSet;
var
  DataOwner: INxDataOwner;
begin
  Result := nil;

  { FOwner is a Column }
  if Assigned(FOwner.Columns) then
    if Supports(FOwner.Columns.Owner, INxDataOwner, DataOwner) then
    begin
      Result := DataOwner.GetDataSet;
    end;
end;

function TNxDataBinding.GetOwner: TPersistent;
begin
  Result := TPersistent(FOwner);
end;

procedure TNxDataBinding.SetField(const Value: TField);
begin
  if FField <> Value then
  begin
    FField := Value;
    if FField <> nil then FFieldName := FField.FieldName;
    AssignFieldProperties;
  end;
end;

procedure TNxDataBinding.SetFieldName(const Value: string);
begin
  if Value <> FFieldName then
  begin
    FFieldName := Value;
    AssignField;
  end;
end;

procedure TNxDataBinding.SetNullText(const Value: WideString);
begin
  FNullText := Value;
end;

procedure TNxDataBinding.SetOptions(const Value: TNxDataBindingOptions);
var
  T: TNxDataBindingOptions;
begin
  if Value <> FOptions then
  begin
    T := FOptions;
    FOptions := Value;
    if (dboUseFieldProperties in FOptions) and not (dboUseFieldProperties in T) then
    begin
      AssignFieldProperties;
    end;
  end;
end;

{ TNxTextDBColumn6 }

procedure TNxDBTextColumn6.Assign(Source: TPersistent);
begin
  inherited;
  DataBinding.AssignDataBinding(Source);
end;

constructor TNxDBTextColumn6.Create(AOwner: TComponent);
begin
  inherited;
  FDataBinding := TNxDataBinding.Create(Self);
end;

destructor TNxDBTextColumn6.Destroy;
begin
  FreeAndNil(FDataBinding);
  inherited;
end;

function TNxDBTextColumn6.GetDataBinding: TNxDataBinding;
begin
  Result := FDataBinding;
end;

{ TNxDBNumberColumn6 }

procedure TNxDBNumberColumn6.Assign(Source: TPersistent);
begin
  inherited;
  DataBinding.AssignDataBinding(Source);
end;

constructor TNxDBNumberColumn6.Create(AOwner: TComponent);
begin
  inherited;
  FDataBinding := TNxDataBinding.Create(Self);
end;

destructor TNxDBNumberColumn6.Destroy;
begin
  FreeAndNil(FDataBinding);
  inherited;
end;

function TNxDBNumberColumn6.GetDataBinding: TNxDataBinding;
begin
  Result := FDataBinding;
end;

{ TNxDBCheckBoxColumn6 }

procedure TNxDBCheckBoxColumn6.Assign(Source: TPersistent);
begin
  inherited;
  DataBinding.AssignDataBinding(Source);
end;

constructor TNxDBCheckBoxColumn6.Create(AOwner: TComponent);
begin
  inherited;
  FDataBinding := TNxDataBinding.Create(Self);
  FValueChecked := '';
  FValueSource := vsCellValue;
  FValueUnhecked := '';
end;

destructor TNxDBCheckBoxColumn6.Destroy;
begin
  FreeAndNil(FDataBinding);
  inherited;
end;

function TNxDBCheckBoxColumn6.GetDataBinding: TNxDataBinding;
begin
  Result := FDataBinding;
end;

function TNxDBCheckBoxColumn6.GetValue(Cell: INxBase; ACol, ARow: Integer): Boolean;
begin
  case ValueSource of
    vsProperty: Result := CompareText(Cell.AsString, ValueChecked) = 0;
    else Result := inherited GetValue(Cell, ACol, ARow);
  end;
end;

procedure TNxDBCheckBoxColumn6.SetValue(Cell: INxBase; ACol, ARow: Integer; Value: Boolean);
begin
  case ValueSource of
    vsCellValue: inherited;
    vsProperty: Cell.AsString := IfThen(Value, ValueChecked, ValueUnchecked);
  end;
end;

procedure TNxDBCheckBoxColumn6.SetValueChecked(const Value: WideString);
begin
  if Value <> FValueChecked then
  begin
    FValueChecked := Value;
    Changed;
  end;
end;

procedure TNxDBCheckBoxColumn6.SetValueSource(const Value: TNxValueSource);
begin
  if Value <> FValueSource then
  begin
    FValueSource := Value;
    Changed;
  end;
end;

procedure TNxDBCheckBoxColumn6.SetValueUnchecked(const Value: WideString);
begin
  if Value <> FValueUnhecked then
  begin
    FValueUnhecked := Value;
    Changed;
  end;
end;

{ TNxDBDateColumn6 }

procedure TNxDBDateColumn6.Assign(Source: TPersistent);
begin
  inherited;
  DataBinding.AssignDataBinding(Source);
end;

constructor TNxDBDateColumn6.Create(AOwner: TComponent);
begin
  inherited;
  FDataBinding := TNxDataBinding.Create(Self);
end;

destructor TNxDBDateColumn6.Destroy;
begin
  FreeAndNil(FDataBinding);
  inherited;
end;

function TNxDBDateColumn6.GetDataBinding: TNxDataBinding;
begin
  Result := FDataBinding;
end;

{ TNxDBTimeColumn6 }

procedure TNxDBTimeColumn6.Assign(Source: TPersistent);
begin
  inherited;
  DataBinding.AssignDataBinding(Source);
end;

constructor TNxDBTimeColumn6.Create(AOwner: TComponent);
begin
  inherited;
  FDataBinding := TNxDataBinding.Create(Self);
end;

destructor TNxDBTimeColumn6.Destroy;
begin
  FreeAndNil(FDataBinding);
  inherited;
end;

function TNxDBTimeColumn6.GetDataBinding: TNxDataBinding;
begin
  Result := FDataBinding;
end;

{ TNxDBColorColumn6 }

procedure TNxDBColorColumn6.Assign(Source: TPersistent);
begin
  inherited;
  DataBinding.AssignDataBinding(Source);
end;

constructor TNxDBColorColumn6.Create(AOwner: TComponent);
begin
  inherited;
  FDataBinding := TNxDataBinding.Create(Self);
end;

destructor TNxDBColorColumn6.Destroy;
begin
  FreeAndNil(FDataBinding);
  inherited;
end;

function TNxDBColorColumn6.GetDataBinding: TNxDataBinding;
begin
  Result := FDataBinding;
end;

{ TNxDBMemoColumn }

procedure TNxDBMemoColumn6.Assign(Source: TPersistent);
begin
  inherited;
  DataBinding.AssignDataBinding(Source);
end;

function TNxDBMemoColumn6.CanEdit: Boolean;
begin
  Result := inherited CanEdit and ExpandContent;
end;

constructor TNxDBMemoColumn6.Create(AOwner: TComponent);
begin
  inherited;
  FDataBinding := TNxDataBinding.Create(Self);
  FExpandContent := False;
end;

destructor TNxDBMemoColumn6.Destroy;
begin
  FreeAndNil(FDataBinding);
  inherited;
end;

function TNxDBMemoColumn6.GetDataBinding: TNxDataBinding;
begin
  Result := FDataBinding;
end;

function TNxDBMemoColumn6.GetExpandContent: Boolean;
begin
  Result := FExpandContent;
end;

procedure TNxDBMemoColumn6.SetExpandContent(const Value: Boolean);
begin
  if Value <> FExpandContent then
  begin
    FExpandContent := Value;
    Changed;
  end;
end;

{ TNxDBProgressColumn6 }

procedure TNxDBProgressColumn6.Assign(Source: TPersistent);
begin
  inherited;
  DataBinding.AssignDataBinding(Source);
end;

constructor TNxDBProgressColumn6.Create(AOwner: TComponent);
begin
  inherited;
  FDataBinding := TNxDataBinding.Create(Self);
end;

destructor TNxDBProgressColumn6.Destroy;
begin
  FreeAndNil(FDataBinding);
  inherited;
end;

function TNxDBProgressColumn6.GetDataBinding: TNxDataBinding;
begin
  Result := FDataBinding;
end;

{ TNxDBIncrementColumn6 }

procedure TNxDBIncrementColumn6.Assign(Source: TPersistent);
begin
  inherited;
  DataBinding.AssignDataBinding(Source);
end;

constructor TNxDBIncrementColumn6.Create(AOwner: TComponent);
begin
  inherited;
  FDataBinding := TNxDataBinding.Create(Self);
end;

destructor TNxDBIncrementColumn6.Destroy;
begin
  FreeAndNil(FDataBinding);
  inherited;
end;

function TNxDBIncrementColumn6.GetDataBinding: TNxDataBinding;
begin
  Result := FDataBinding;
end;

{ TNxDBButtonColumn6 }

procedure TNxDBButtonColumn6.Assign(Source: TPersistent);
begin
  inherited;
  DataBinding.AssignDataBinding(Source);
end;

constructor TNxDBButtonColumn6.Create(AOwner: TComponent);
begin
  inherited;
  FDataBinding := TNxDataBinding.Create(Self);
end;

destructor TNxDBButtonColumn6.Destroy;
begin
  FreeAndNil(FDataBinding);
  inherited;
end;

function TNxDBButtonColumn6.GetDataBinding: TNxDataBinding;
begin
  Result := FDataBinding;
end;

{ TNxDBIconColumn6 }

procedure TNxDBIconColumn6.Assign(Source: TPersistent);
begin
  inherited;
  DataBinding.AssignDataBinding(Source);
end;

constructor TNxDBIconColumn6.Create(AOwner: TComponent);
begin
  inherited;
  FDataBinding := TNxDataBinding.Create(Self);
end;

destructor TNxDBIconColumn6.Destroy;
begin
  FreeAndNil(FDataBinding);
  inherited;
end;

function TNxDBIconColumn6.GetDataBinding: TNxDataBinding;
begin
  Result := FDataBinding;
end;

{ TNxDBTrackbarColumn6 }

procedure TNxDBTrackbarColumn6.Assign(Source: TPersistent);
begin
  inherited;
  DataBinding.AssignDataBinding(Source);
end;

constructor TNxDBTrackbarColumn6.Create(AOwner: TComponent);
begin
  inherited;
  FDataBinding := TNxDataBinding.Create(Self);
end;

destructor TNxDBTrackbarColumn6.Destroy;
begin
  FreeAndNil(FDataBinding);
  inherited;
end;

function TNxDBTrackbarColumn6.GetDataBinding: TNxDataBinding;
begin
  Result := FDataBinding;
end;

{ TNxDBComboBoxColumn6 }

procedure TNxDBComboBoxColumn6.Assign(Source: TPersistent);
begin
  inherited;
  DataBinding.AssignDataBinding(Source);
end;

constructor TNxDBComboBoxColumn6.Create(AOwner: TComponent);
begin
  inherited;
  FDataBinding := TNxDataBinding.Create(Self);
end;

destructor TNxDBComboBoxColumn6.Destroy;
begin
  FreeAndNil(FDataBinding);
  inherited;
end;

function TNxDBComboBoxColumn6.GetDataBinding: TNxDataBinding;
begin
  Result := FDataBinding;
end;

{ TNxDBCheckComboBoxColumn6 }

procedure TNxDBCheckComboBoxColumn6.Assign(Source: TPersistent);
begin
  inherited;
  DataBinding.AssignDataBinding(Source);
end;

constructor TNxDBCheckComboBoxColumn6.Create(AOwner: TComponent);
begin
  inherited;
  FDataBinding := TNxDataBinding.Create(Self);
end;

destructor TNxDBCheckComboBoxColumn6.Destroy;
begin
  FreeAndNil(FDataBinding);
  inherited;
end;

function TNxDBCheckComboBoxColumn6.GetDataBinding: TNxDataBinding;
begin
  Result := FDataBinding;
end;

{ TNxDBGraphicColumn6 }

procedure TNxDBGraphicColumn6.Assign(Source: TPersistent);
begin
  inherited;
  DataBinding.AssignDataBinding(Source);
end;

constructor TNxDBGraphicColumn6.Create(AOwner: TComponent);
begin
  inherited;
  FDataBinding := TNxDataBinding.Create(Self);
end;

destructor TNxDBGraphicColumn6.Destroy;
begin
  FreeAndNil(FDataBinding);
  inherited;
end;

function TNxDBGraphicColumn6.GetDataBinding: TNxDataBinding;
begin
  Result := FDataBinding;
end;

{ TNxDBHTMLColumn6 }

procedure TNxDBHTMLColumn6.Assign(Source: TPersistent);
begin
  inherited;
  DataBinding.AssignDataBinding(Source);
end;

constructor TNxDBHTMLColumn6.Create(AOwner: TComponent);
begin
  inherited;
  FDataBinding := TNxDataBinding.Create(Self);
end;

destructor TNxDBHTMLColumn6.Destroy;
begin
  FreeAndNil(FDataBinding);
  inherited;
end;

function TNxDBHTMLColumn6.GetDataBinding: TNxDataBinding;
begin
  Result := FDataBinding;
end;

{ TNxDBListColumn6 }

procedure TNxDBListColumn6.Assign(Source: TPersistent);
begin
  inherited;
  DataBinding.AssignDataBinding(Source);
end;

constructor TNxDBListColumn6.Create(AOwner: TComponent);
begin
  inherited;
  FDataBinding := TNxDataBinding.Create(Self);
end;

destructor TNxDBListColumn6.Destroy;
begin
  FreeAndNil(FDataBinding);
  inherited;
end;

function TNxDBListColumn6.GetDataBinding: TNxDataBinding;
begin
  Result := FDataBinding;
end;

{ TNxDBToolbarColumn6 }

procedure TNxDBToolbarColumn6.Assign(Source: TPersistent);
begin
  inherited;
  DataBinding.AssignDataBinding(Source);
end;

constructor TNxDBToolbarColumn6.Create(AOwner: TComponent);
begin
  inherited;
  FDataBinding := TNxDataBinding.Create(Self);
end;

destructor TNxDBToolbarColumn6.Destroy;
begin
  FreeAndNil(FDataBinding);
  inherited;
end;

function TNxDBToolbarColumn6.GetDataBinding: TNxDataBinding;
begin
  Result := FDataBinding;
end;

{ TNxLookupDataLink }

procedure TNxLookupDataLink.ActiveChanged;
begin
  inherited;
  if Assigned(DataSet) then FLookup.UpdateContent;
end;

constructor TNxLookupDataLink.Create(ALookup: INxLookupContent);
begin
  FLookup := ALookup;
end;

procedure TNxLookupDataLink.UpdateData;
begin
  inherited;
  if Assigned(DataSet) then FLookup.UpdateContent;
end;

{ TNxLookupBinding }

procedure TNxLookupBinding.BuildList(Strings: TStrings);
begin
  Strings.Clear;
  DataSource.DataSet.DisableControls;
  try
    DataSource.DataSet.First;
    repeat
      Strings.Add(FDataSource.DataSet.FieldByName(DetailFieldName).AsString);
      DataSource.DataSet.Next;
    until FDataSource.DataSet.Eof;
  finally
    DataSource.DataSet.EnableControls;
  end;
end;

procedure TNxLookupBinding.Changed;
begin
  if Assigned(FOnChange) then FOnChange(Self);
end;

function TNxLookupBinding.GetActive: Boolean;
begin
  Result := Assigned(FDataSource) and Assigned(FDataSource.DataSet)
    and (FDataSource.DataSet.Active)
    and (FKeyFieldName <> '')
    and (FDetailFieldName <> '');
end;

procedure TNxLookupBinding.SetDataSource(const Value: TDataSource);
begin
  if Value <> FDataSource then
  begin
    FDataSource := Value;
    Changed;
  end;
end;

procedure TNxLookupBinding.SetDetailFieldName(const Value: string);
begin
  if Value <> FDetailFieldName then
  begin
    FDetailFieldName := Value;
    Changed;
  end;
end;

procedure TNxLookupBinding.SetKeyFieldName(const Value: string);
begin
  if Value <> FKeyFieldName then
  begin
    FKeyFieldName := Value;
    Changed;
  end;
end;

{ TNxDBLookupColumn6 }

procedure TNxDBLookupColumn6.Assign(Source: TPersistent);
begin
  inherited;

end;

procedure TNxDBLookupColumn6.AssignEditValue(Cell: INxBase; InplaceEdit: INxInplaceEdit);
var
  Value: Variant;
begin
  if FLookupBinding.Active then
  begin
    Value := FLookupBinding.DataSource.DataSet.Lookup(
      FLookupBinding.DetailFieldName, InplaceEdit.Text, FLookupBinding.KeyFieldName);;

    if not VarIsNull(Value) then
    begin
      Cell.AsString := Value;
    end;
  end;
end;

constructor TNxDBLookupColumn6.Create(AOwner: TComponent);
begin
  inherited;
  FDataLink := TNxLookupDataLink.Create(Self as INxLookupContent);
  FLookupBinding := TNxLookupBinding.Create;
  FLookupBinding.OnChange := DoLookupBindingChange;
end;

destructor TNxDBLookupColumn6.Destroy;
begin
  FDataLink.Free;
  FLookupBinding.Free;
  inherited;
end;

procedure TNxDBLookupColumn6.DoLookupBindingChange(Sender: TObject);
begin
  FDataLink.DataSource := FLookupBinding.DataSource;
end;

function TNxDBLookupColumn6.GetDisplayText(Cell: INxBase; ACol, ARow: Integer): WideString;
var
  Value: Variant;
begin
  if FLookupBinding.Active then
  begin
    Value := FLookupBinding.DataSource.DataSet.Lookup(
      FLookupBinding.KeyFieldName, Cell.AsString, FLookupBinding.DetailFieldName);;

    if not VarIsNull(Value) then Result := Value;
  end;
end;

function TNxDBLookupColumn6.GetEditText(Cell: INxBase; ACol, ARow: Integer): WideString;
begin
  Result := GetDisplayText(Cell, ACol, ARow);
end;

///<summary>Called on TDataLink changed</summary>
procedure TNxDBLookupColumn6.UpdateContent;
begin
  if FLookupBinding.Active then FLookupBinding.BuildList(Items);
end;

{ TNxDBRateColumn6 }

procedure TNxDBRateColumn6.Assign(Source: TPersistent);
begin
  inherited;
  DataBinding.AssignDataBinding(Source);
end;

constructor TNxDBRateColumn6.Create(AOwner: TComponent);
begin
  inherited;
  FDataBinding := TNxDataBinding.Create(Self);
end;

destructor TNxDBRateColumn6.Destroy;
begin
  FDataBinding.Free;
  inherited;
end;

function TNxDBRateColumn6.GetDataBinding: TNxDataBinding;
begin
  Result := FDataBinding;
end;

end.
