So I have a TDBGrid that displays the content of a Query via SQL. I need to be able to only show the fields/columns that are selected in a TCheckListBox. How do I go about this problem?
Asked
Active
Viewed 58 times
2 Answers
1
The columns link back to the datasource so you can iterate over them until you find the one you want.
for cnt := 0 to DBGrid1.Columns.Count -1 do
if DBGrid1.Columns[cnt].FieldName = 'Lengte'
then DBGrid1.Columns[cnt].Visible := false;

Brian
- 6,717
- 2
- 23
- 31
0
Got it to work!
var
i, j: integer;
arrColsToHide: array[0..11] of string;
begin
// resets all columns to VISIBLE and clears array
for i := 0 to 11 do
begin
arrColsToHide[i]:= '';
dbGridExport.Columns[i+1].Visible:= true;
end;
// if item is not checked, add it to the array
for i := 0 to 11 do
if not(clbFieldsToExport.Checked[i])
then arrColsToHide[i]:= clbFieldsToExport.Items.Strings[i];
// compares value of array to fieldname, and hide if same
for j := 0 to 11 do
begin
for i := 1 to dbGridExport.Columns.Count-1 do
if dbGridExport.Columns[i].FieldName = arrColsToHide[j]
then dbGridExport.Columns[i].Visible := false;
end;
end;

Janrich
- 55
- 1
- 6