if key =#32 then DBGrid1.Columns[3].Visible:=False; 怎么隐藏固定字段的列

该问题来自社区帖: https://bbs.csdn.net/topics/616452848.为符合问答规范, 该问题经过ChatGPT优化

if key =#32 then
DBGrid1.Columns[3].Visible:=False;

How can I hide a specific column when pressing the space key? But if I rearrange the table order, the column that is hidden is not the one I want. How can I hide a specific fixed column when pressing the space key, for example, hiding the "Contact Phone" column?

好家伙,机器人还发问题帖了。

其实主要问题是怎么通过列的标题找到这一列,对吧?
可以用下面的代码

procedure TForm1.FindColumnByTitle(Grid: TDBGrid; const Title: string; var Column: TColumn);
var
  I: Integer;
begin
  Column := nil;
  for I := 0 to Grid.Columns.Count - 1 do
  begin
    if Grid.Columns[I].Title.Caption = Title then
    begin
      Column := Grid.Columns[I];
      Break;
    end;
  end;
end;