{------------------------------------------------------------------------------} { Unit Name: DbGridIni Purpose : A dbgrid that saves the settings Author : Vesa Lappalainen Date : 23.7.2000 Changed : ToDo : } {------------------------------------------------------------------------------} unit DbGridIni; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, Grids,DbGrids, kIniSave, ComCtrls; type TDBGridIni = class(TDBGrid) private { Private declarations } FIni: TIniSave; protected { Protected declarations } function GetAsString : string; virtual; procedure SetAsString(s:string); virtual; procedure LinkActive(Value: Boolean); override; procedure ColWidthsChanged; override; public { Public declarations } constructor Create(AOwner: TComponent); override; destructor Destroy; override; published { Published declarations } property Ini : TIniSave read FIni write FIni; property AsString : string read GetAsString write SetAsString stored false; end; procedure Register; implementation uses kDouble; procedure Register; begin RegisterComponents('Kave2000', [TDBGridIni]); end; //============================================================================= // TDBGridIni //============================================================================= constructor TDBGridIni.Create(AOwner: TComponent); begin inherited Create(AOwner); FIni := TIniSave.Create(self); Ini.AutoSave := True; Ini.PropName := 'AsString'; // Ini.IniSection:= 'Grid'; end; destructor TDBGridIni.Destroy; begin // Columns.SaveToFile('a.a'); if ( Columns.Count > 0 ) and ( Columns[0].FieldName > '' ) then Ini.Save; Ini.Free; inherited Destroy; end; procedure TDBGridIni.SetAsString(s:string); // virtual; var c,j,i: word; st : TStrings; cn,ns : string; begin if ( csDesigning in ComponentState ) then exit; st := TStringList.Create; st.CommaText := s; c := 0; for j:=0 to st.Count-1 do begin cn := st.Names[j]; ns := st.Values[cn]; if ( ns = '' ) then continue; for i := 0 to Columns.Count-1 do begin if ( cn = Columns[i].FieldName ) then begin Columns[i].Width := StrToIntDef(ns,10); if ( FGridState <> gsColMoving ) then // Columns[i].Index := c; MoveColumn(DataToRawColumn(i),DataToRawColumn(c)); ColWidths[DataToRawColumn(c)] := StrToIntDef(ns,10); Inc(c); break; end; end; end; st.Free; end; function TDBGridIni.GetAsString : string; // virtual; var i: word; begin Result := ''; if ( csDesigning in ComponentState ) then exit; for i := 0 to Columns.Count-1 do begin Result := Result + Columns[i].FieldName + '=' + IntToStr(Columns[i].Width) + ','; end; if (Length(result) > 0) then delete(result,Length(result),1); end; procedure TDBGridIni.ColWidthsChanged; begin inherited; // if ( FInMouseUp ) and ( not FInColumnMoved ) then Ini.DoAutoSave; if ( FGridState in [gsColSizing,gsColMoving] ) then Ini.DoAutoSave; end; procedure TDBGridIni.LinkActive(Value: Boolean); begin inherited; Ini.Read; end; end.