unit uMitarbeiterDS;
{$I cxVer.inc}
interface
uses
Variants, Classes,
cxCustomData, cxGridCustomTableView,
uMitarbeiter;
const
IndexOfID = 0;
IndexOfPersNr = 1;
IndexOfNachName = 2;
IndexOfVorname = 3;
IndexOfUpdated_at = 4;
IndexOfCreated_at = 5;
type
TDemoMitarbeiterDataSource =
class(TcxCustomDataSource)
private
FMaList: TMitarbeiterList;
FModified: boolean;
protected
function GetRecordCount: Integer;
override;
function GetValue(ARecordHandle: TcxDataRecordHandle; AItemHandle: TcxDataItemHandle): Variant;
override;
function AppendRecord: TcxDataRecordHandle;
override;
procedure DeleteRecord(ARecordHandle: TcxDataRecordHandle);
override;
function InsertRecord(ARecordHandle: TcxDataRecordHandle): TcxDataRecordHandle;
override;
procedure SetValue(ARecordHandle: TcxDataRecordHandle; AItemHandle: TcxDataItemHandle;
const AValue: Variant);
override;
public
constructor Create(AMaList: TMitarbeiterList);
property Modified: boolean
read FModified;
property MitarbeiterList : TMitarbeiterList
read FMaList;
function GetDataObjectByHandle(ARecordHandle: TcxDataRecordHandle): TMitarbeiter;
end;
implementation
{ TCustomDataSource }
constructor TDemoMitarbeiterDataSource.Create(AMaList: TMitarbeiterList);
begin
inherited Create;
FMaList := AMaList;
end;
function TDemoMitarbeiterDataSource.AppendRecord: TcxDataRecordHandle;
var
aMa: TMitarbeiter;
begin
aMa := TMitarbeiter.Create(FMaList.Connection);
Result := TcxDataRecordHandle(FMaList.Add(aMa));
DataChanged;
if not Modified
then
FModified := True;
end;
procedure TDemoMitarbeiterDataSource.DeleteRecord(ARecordHandle: TcxDataRecordHandle);
begin
FMaList.Delete(Integer(ARecordHandle));
DataChanged;
if not Modified
then
FModified := True;
end;
function TDemoMitarbeiterDataSource.GetDataObjectByHandle(
ARecordHandle: TcxDataRecordHandle): TMitarbeiter;
begin
try
Result := FMaList[Integer(ARecordHandle)];
except
result :=
nil;
end;
end;
function TDemoMitarbeiterDataSource.GetRecordCount: Integer;
begin
Result := FMaList.Count;
end;
function TDemoMitarbeiterDataSource.GetValue(ARecordHandle: TcxDataRecordHandle;
AItemHandle: TcxDataItemHandle): Variant;
var
AColumnId: Integer;
aMa: TMitarbeiter;
begin
aMa := FMaList[Integer(ARecordHandle)];
AColumnId := GetDefaultItemID(Integer(AItemHandle));
case AColumnId
of
IndexOfID : Result := aMa.ID;
IndexOfPersNr : Result := aMa.PersNr;
IndexOfNachName : Result := aMa.Nachname;
IndexOfVorname : Result := aMa.Vorname;
IndexOfUpdated_at : Result := aMa.Updated_at;
IndexOfCreated_at : Result := aMa.Created_at;
end;
end;
function TDemoMitarbeiterDataSource.InsertRecord(
ARecordHandle: TcxDataRecordHandle): TcxDataRecordHandle;
var
aMa: TMitarbeiter;
begin
aMa := TMitarbeiter.Create;
FMaList.Insert(Integer(ARecordHandle), aMa);
Result := TcxDataRecordHandle(ARecordHandle);
DataChanged;
if not Modified
then
FModified := True;
end;
procedure TDemoMitarbeiterDataSource.SetValue(ARecordHandle: TcxDataRecordHandle;
AItemHandle: TcxDataItemHandle;
const AValue: Variant);
var
aMa: TMitarbeiter;
AColumnId: Integer;
begin
AColumnId := GetDefaultItemID(Integer(AItemHandle));
aMa := FMaList[Integer(ARecordHandle)];
case AColumnId
of
IndexOfID :
begin
if VarIsNull(AValue)
then
aMa.ID := 0
else
aMa.ID := AValue;
end;
IndexOfPersNr : aMa.PersNr := VarToStr(AValue) ;
IndexOfNachName : aMa.Nachname := VarToStr(AValue) ;
IndexOfVorname : aMa.Vorname := VarToStr(AValue) ;
IndexOfUpdated_at :
begin
// Read-Only
(*
if VarIsNull(AValue) then
aMa.Updated_at := 0
else
aMa.Updated_at := VarToDateTime(AValue);
*)
end;
IndexOfCreated_at :
begin
// Read-Only
end;
end;
if not Modified
then
FModified := True;
end;
end.