procedure LoadFromDataSet( Destination: TClientDataSet; Source: TDataSet; InitialSort:
String = '
'; CheckNotNull:
String = '
' );
var
fieldNo, indexNo: Integer;
field: TField;
fieldName:
String;
CopyData: Boolean;
begin
with Destination
do begin
Screen.Cursor := crHourGlass;
DisableControls;
//-- Vorhandene Indexes löschen --//
if ( FieldDefs.Count > 0 )
then begin
IndexDefs.Update;
Close;
Open;
while( IndexDefs.Count > 2 )
do begin
try
DeleteIndex(IndexDefs.Items[2].
Name);
except
Break;
end;
IndexDefs.Update;
end;
end;
//-------------------------------------//
//-- Struktur des DataSet übernehmen --//
//-------------------------------------//
Close;
try
Source.Open;
except
end;
if ( Source.FieldDefs.Count < 1 )
then
raise Exception.Create('
Fehler beim öffnen der Abfrage.');
//-- Clientdataset erstellen --//
FieldDefs.Assign(Source.FieldDefs);
CreateDataSet;
//-- Währungsfelder setzen --//
for fieldNo := 0
to Fields.Count - 1
do begin
field := Fields[fieldNo];
if ( field
is TFloatField )
then begin
fieldName := field.FieldName;
if ( Copy(fieldName,Length(fieldName)-3,4) = '
_EUR' )
then begin
(FieldByName(fieldName)
as TFloatField).Currency := True;
System.Delete(fieldName,Length(fieldName)-3,4);
(FieldByName(fieldName)
as TFloatField).Currency := True;
end;
end;
end;
CopyData := True;
//-- Alle Datensätze kopieren --//
if ( CheckNotNull <> '
' )
then
CopyData :=
not Source.FieldByName(CheckNotNull).IsNull;
if ( CopyData )
then begin
while (
not Source.Eof )
do begin
//-- Datensatz anlegen und alle Felder kopieren --//
Append;
for fieldNo := 0
to Fields.Count - 1
do
Fields[fieldNo].Value := Source.Fields[fieldNo].Value;
Post;
Source.Next;
end;
//-- Einen Index anlegen --//
if ( (InitialSort <> '
')
and (
not (Bof
and Eof)) )
then begin
AddIndex('
IDX_'+InitialSort,InitialSort,[]);
IndexName := '
IDX_'+InitialSort;
end;
end;
Source.Close;
First;
EnableControls;
Screen.Cursor := crDefault;
end;
end;