unit TestDBG1;
interface
uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
Grids, DBGrids,
Db, DBTables, ExtCtrls, ComCtrls, Buttons, DBCtrls,
StdCtrls;
type
TForm1 =
class(TForm)
pc1: TPageControl;
tsListe: TTabSheet;
panTop: TPanel;
Tab1: TTable;
DS1: TDataSource;
dbg1: TDBGrid;
sbClose: TSpeedButton;
DBNavigator1: TDBNavigator;
cbStID: TComboBox;
Label1: TLabel;
sbFilterOff: TSpeedButton;
sbNew: TSpeedButton;
cbSort: TComboBox;
Label2: TLabel;
procedure sbCloseClick(Sender: TObject);
procedure cbStIDChange(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure sbFilterOffClick(Sender: TObject);
procedure dbg1DblClick(Sender: TObject);
procedure sbNewClick(Sender: TObject);
procedure cbSortChange(Sender: TObject);
procedure panTopResize(Sender: TObject);
private
{ Private-Deklarationen }
public
{ Public-Deklarationen }
end;
var
Form1: TForm1;
implementation
{$R *.DFM}
const
fnMW :
string = '
Messwerte.DB';
ixStID :
string = '
IdxStID';
ixDatum :
string = '
IdxDatumDesc';
c_keineSortierung = '
keine Sortierung';
var
s, vfilt :
string;
// ------ allg. Funktionen -----------------------------------------------------
//UP hängt an einen Pfadstring nötigenfalls ein \ an
function AppBkSp(s:
string):
string;
begin
AppBkSp := IncludeTrailingBackslash(s);
end;
//Öffnet eine Tabelle, falls nötig, und setzt vFilt als akt. Filter
// Rückgabewert 0 --> erfolgreich Filter gesetzt
function SetFilter(Tab : TTable ; vFilt :
string) : integer;
begin
if not fileexists(AppBkSp(Tab.DataBaseName)+Tab.TableName)
then begin
Result := 101;
exit;
end;
Tab.DisableControls;
if not Tab.Active
then Tab.Active := true;
Tab.Filtered := false;
if vFilt = '
'
then Tab.Filter := '
'
else begin
Tab.Filter := vFilt;
try
Tab.Filtered := true;
Result := 0;
except
Result := 1;
Tab.Filtered := false;
ShowMessage('
SetFilter: '+ Tab.TableName +'
konnte nicht mit '+ vFilt +'
gefiltert werden');
end;
end;
Tab.EnableControls;
end;
procedure DBGEditEinAus(dbg: TObject);
begin
if (dbg
is TDBGrid)
then begin
if (dgEditing
in (dbg
as TDBGrid).Options)
then
(dbg
as TDBGrid).Options := (dbg
as TDBGrid).Options - [dgEditing, dgAlwaysShowEditor]
+ [dgRowSelect, dgAlwaysShowSelection]
else
(dbg
as TDBGrid).Options := (dbg
as TDBGrid).Options + [dgEditing, dgAlwaysShowEditor]
- [dgRowSelect, dgAlwaysShowSelection];
(dbg
as TDBGrid).refresh;
end;
end;
// ------end of allg. Funktionen -----------------------------------------------
procedure TForm1.FormActivate(Sender: TObject);
var
old, new : integer;
i : integer;
begin
// Tabelle öffnen, falls nicht im OI definiert
if not Tab1.Active
then begin
tab1.DatabaseName := AppBkSp(ExtractFilePath(Application.ExeName)) + '
daten\';
tab1.TableName := fnMW;
tab1.Open;
end;
// Stations-Nummern in Combobox laden, falls das noch nicht erfolgt ist
if cbStID.Items.Count = 0
then begin
tab1.IndexName := ixStID;
tab1.First;
old := 0;
new := 0;
while not tab1.Eof
do begin
new := tab1.FieldByName('
StID').AsInteger;
if new <> old
then begin
s := IntToStr(new);
cbStID.Items.Add(s);
old := new;
end;
tab1.Next;
end;
cbStID.Sorted := true;
end;
s := cbStID.Items.Strings[0];
cbStID.Text := s;
cbStIDChange(Sender);
// Tabelle gestalten
if dbg1.Columns.Count = tab1.FieldCount
then begin
dbg1.Columns.Clear;
i := 0;
dbg1.Columns.Add;
// dbg1.Columns.Items[i].Title.Caption := 'Datum'; //Überschrift ändern
dbg1.Columns.Items[i].FieldName := '
Datum';
dbg1.Columns.Items[i].Width := 120;
inc(i);
dbg1.Columns.Add;
dbg1.Columns.Items[i].Title.Caption := '
Windstärke';
dbg1.Columns.Items[i].FieldName := '
Wind';
dbg1.Columns.Items[i].Width := 70;
inc(i);
dbg1.Columns.Add;
dbg1.Columns.Items[i].FieldName := '
Temp';
dbg1.Columns.Items[i].Width := 50;
inc(i);
dbg1.Columns.Add;
dbg1.Columns.Items[i].FieldName := '
Feuchtigkeit';
dbg1.Columns.Items[i].Width := 80;
inc(i);
dbg1.Columns.Add;
dbg1.Columns.Items[i].FieldName := '
Luftdruck';
dbg1.Columns.Items[i].Width := 70;
inc(i);
end;
//Sortierordnung einlesen
if cbSort.Items.Count = 0
then begin
cbSort.Clear;
Tab1.IndexDefs.Update;
if Tab1.IndexDefs.Count > 0
then begin
for i := 0
to Tab1.IndexDefs.Count - 1
do begin
s := Tab1.IndexDefs.Items[i].Fields;
s := Tab1.IndexDefs.Items[i].
Name;
if trim(s) = '
'
then s := c_keineSortierung;
cbSort.Items.Add(s);
end;
end;
end;
cbSort.Text := ixDatum;
tab1.IndexName := ixDatum;
tab1.First;
pc1.Align := alClient;
end;
procedure TForm1.sbCloseClick(Sender: TObject);
begin
close;
end;
procedure TForm1.cbStIDChange(Sender: TObject);
begin
s := cbStID.Text;
vfilt := '
StID=' + s;
SetFilter(tab1, vfilt);
end;
procedure TForm1.sbFilterOffClick(Sender: TObject);
begin
SetFilter(tab1, '
');
end;
procedure TForm1.dbg1DblClick(Sender: TObject);
begin
DBGEditEinAus(dbg1);
end;
procedure TForm1.sbNewClick(Sender: TObject);
begin
tab1.Append;
tab1.FieldByName('
Datum').AsDateTime := now;
tab1.FieldByName('
StID').AsInteger := StrToInt(cbStID.Text);
tab1.Post;
// EditModus einschalten
if not (dgEditing
in dbg1.Options)
then DBGEditEinAus(dbg1);
end;
procedure TForm1.cbSortChange(Sender: TObject);
begin
if cbSort.Text = c_keineSortierung
then s := '
'
else s := cbSort.Text;
tab1.IndexName := s;
tab1.First;
end;
procedure TForm1.panTopResize(Sender: TObject);
begin
sbClose.Left := panTop.Width - sbClose.Width - 24;
end;
end.