仅显示通过SQL在DBGrid中的TCheckListBox中选择的字段

仅显示通过SQL在DBGrid中的TCheckListBox中选择的字段,sql,database,delphi,ms-access,tadoquery,Sql,Database,Delphi,Ms Access,Tadoquery,因此,我有一个TDBGrid,它通过SQL显示查询的内容。 我需要能够只显示在TCheckListBox中选择的字段/列。我该如何处理这个问题 这些列链接回数据源,因此您可以对它们进行迭代,直到找到所需的列 for cnt := 0 to DBGrid1.Columns.Count -1 do if DBGrid1.Columns[cnt].FieldName = 'Lengte' then DBGrid1.Columns[cnt].Visible := false;

因此,我有一个TDBGrid,它通过SQL显示查询的内容。 我需要能够只显示在TCheckListBox中选择的字段/列。我该如何处理这个问题


这些列链接回数据源,因此您可以对它们进行迭代,直到找到所需的列

  for cnt := 0 to DBGrid1.Columns.Count -1 do
    if DBGrid1.Columns[cnt].FieldName = 'Lengte'
      then DBGrid1.Columns[cnt].Visible := false;
成功了

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;