-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfGridCols.pas
More file actions
213 lines (187 loc) · 5.22 KB
/
Copy pathfGridCols.pas
File metadata and controls
213 lines (187 loc) · 5.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
unit fGridCols;
interface
uses
{$ifndef CLX}
Forms, Controls, StdCtrls, CheckLst, Classes, ExtCtrls, DBGrids, DB;
{$else}
QForms, QControls, QStdCtrls, QCheckLst, Classes, QExtCtrls, QDBGrids, DB;
{$endif}
type
TFormGridCols = class(TForm)
Panel1: TPanel;
btnOK: TButton;
btnCancel: TButton;
clbColumns: TCheckListBox;
btnAll: TButton;
btnNone: TButton;
procedure btnAllClick(Sender: TObject);
procedure btnNoneClick(Sender: TObject);
procedure clbColumnsClickCheck(Sender: TObject);
private
FGrid: TDBGrid;
{ Private declarations }
procedure SetAllColumns( Value : boolean );
procedure SetGrid(const Value: TDBGrid);
public
{ Public declarations }
property Grid : TDBGrid read FGrid write SetGrid;
procedure GetColumnList;
procedure SetColumnList;
function CountCheckState( State : TCheckBoxState ) : integer;
end;
procedure GridColumnToggler( const AGrid : TDBGrid );
function FindFieldColumn( const Grid : TDBGrid; const Field : TField ) : integer;
function SetAllChecklistBox( Box : TCheckListBox; const Value : boolean ) : integer;
function GetGridColumns( const Grid : TDBGrid; Box : TCheckListBox ) : integer;
function SetGridColumns( Grid : TDBGrid; const Box : TCheckListBox ) : integer;
function CountCheckListState( Box : TCheckListBox; State : TCheckBoxState ) : integer;
var
FormGridCols: TFormGridCols;
implementation
{$R *.dfm}
procedure GridColumnToggler( const AGrid : TDBGrid );
begin
with TFormGridCols.Create( Application ) do
try
Grid := AGrid;
if ShowModal = mrOk then
SetColumnList;
finally
Free;
end; { finally }
end; { GridColumnToggler() }
function FindFieldColumn( const Grid : TDBGrid; const Field : TField ) : integer;
var
i : integer;
begin
for i := 0 to Grid.Columns.Count - 1 do
if Grid.Columns[ i ].Field = Field then
begin
Result := i;
Exit;
end;
Result := -1;
end; { FindFieldColumn() }
function GetGridColumns( const Grid : TDBGrid; Box : TCheckListBox ) : integer;
var
i, j, k : integer;
Field : TField;
begin
Box.Items.Clear;
try
Box.Items.BeginUpdate;
if Assigned( Grid.DataSource.DataSet ) then
for i := 0 to Grid.DataSource.DataSet.Fields.Count - 1 do
begin
Field := Grid.DataSource.DataSet.Fields[ i ];
if Field.Visible then
begin
j := Box.Items.Add( Field.DisplayName );
k := FindFieldColumn( Grid, Field );
Box.Checked[ j ] := ( k >= 0 ) and Grid.Columns.Items[ k ].Visible;
Box.Items.Objects[ j ] := Field;
end; { Field should be listed }
end; { for }
finally
Box.Items.EndUpdate;
end; { finally }
Result := Box.Items.Count;
end; { GetGridColumns() }
function SetAllChecklistBox( Box : TCheckListBox; const Value : boolean ) : integer;
var
i : integer;
begin
for i := 0 to Box.Items.Count - 1 do
Box.Checked[ i ] := Value;
Result := Box.Items.Count;
end;
function SetGridColumns( Grid : TDBGrid; const Box : TCheckListBox ) : integer;
var
i, j : integer;
Field : TField;
procedure AddField(Field: TField; Depth: Integer );
var
I: Integer;
begin
Inc(Depth);
Grid.Columns.Add.Field := Field;
if Field.DataType in [ ftADT, ftArray ] then
for I := 0 to ( Field as TObjectField ).Fields.Count - 1 do
AddField( (Field as TObjectField).Fields[ I ], Depth);
end;
begin
Grid.Columns.BeginUpdate;
try
for i := 0 to Box.Items.Count - 1 do
begin
Field := TField( Box.Items.Objects[ i ] );
j := FindFieldColumn( Grid, Field );
if Box.Checked[ i ] then
begin
if j < 0 then
AddField( Field, 0 )
else
Grid.Columns.Items[ j ].Visible := True; { Add the field }
end
else if j > 0 then
Grid.Columns.Items[ j ].Visible := False;
end; { for }
finally
Grid.Columns.EndUpdate;
end; { finally }
Result := Grid.Columns.Count;
end;
function CountCheckListState(Box : TCheckListBox; State: TCheckBoxState): integer;
var
i : integer;
begin
Result := 0;
for i := 0 to Box.Items.Count - 1 do
if Box.State[ i ] = State then
Inc( Result );
end; { CountCheckState() }
{ TFormGridCols }
procedure TFormGridCols.btnAllClick(Sender: TObject);
begin
SetAllColumns( True );
end;
procedure TFormGridCols.SetAllColumns(Value: boolean);
begin
btnOk.Enabled := SetAllChecklistBox( clbColumns, Value ) > 0;
end;
procedure TFormGridCols.btnNoneClick(Sender: TObject);
begin
SetAllCheckListBox( clbColumns, False );
btnOk.Enabled := False;
end;
procedure TFormGridCols.GetColumnList;
begin
btnOk.Enabled := GetGridColumns( Grid, clbColumns ) > 0;
end;
procedure TFormGridCols.SetColumnList;
begin
SetGridColumns( Grid, clbColumns );
end;
procedure TFormGridCols.SetGrid(const Value: TDBGrid);
begin
if FGrid <> Value then
begin
FGrid := Value;
if Assigned( FGrid ) then
GetColumnList
else
begin
clbColumns.Items.Clear;
btnOk.Enabled := False;
end;
end;
end;
procedure TFormGridCols.clbColumnsClickCheck(Sender: TObject);
begin
btnOk.Enabled := CountCheckState( cbChecked ) > 0;
end;
function TFormGridCols.CountCheckState(State: TCheckBoxState): integer;
begin
Result := CountCheckListState( clbColumns, State );
end;
end.