在 Delphi 中重新定位 DBGrid 中的列

发布于 2024-08-16 10:39:29 字数 240 浏览 8 评论 0原文

我需要动态更改 DBGRid 中某些列的位置。假设我需要将列号 21 放在位置 10 上。我使用:

DBGrid.Columns[21].Index:=10;

但是,这也会更改数组本身,这意味着下次我想访问此列时,我将需要编写 DBGrid.Columns[10],这使得它有点不干净,我需要记住所有列的位置等。有没有更简单的方法来重新定位列? 如果在此位置更改期间数组索引不发生更改,那就太好了。

I need to dynamically change position of certain column in DBGRid. Let's say I need to place column number 21 on position 10. I use:

DBGrid.Columns[21].Index:=10;

But, this also changes the array itself, that means, that next time I want to access this column, I will need to write DBGrid.Columns[10], this makes it a little unclean, I need to memorize positions of all columns etc. Is there an easier way to reposition a column?
It would also be good if array indexes do not change during this position change.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

樱娆 2024-08-23 10:39:29

处理该问题的一个简单方法是不按索引访问列,而是按字段名访问列。引入这样的方法:

function GetColumn(aGrid : TDBGrid; aFieldName : string) : TColumn;
var
  I : integer;
begin
  for I := 0 to DBGrid.Columns.Count-1 do
    if aDBGrid.Columns[I].FieldName = aFieldName then
    begin
      Result := aDBGrid.Columns[I];
      exit;
    end;
  Result := nil;
end;

缺点是每次需要访问网格时都必须运行循环,导致延迟,因此如果速度很重要,您可能会考虑其他选择。

A simple way to deal with the problem is to not access the columns by index but by fieldname. Introduce a method like this:

function GetColumn(aGrid : TDBGrid; aFieldName : string) : TColumn;
var
  I : integer;
begin
  for I := 0 to DBGrid.Columns.Count-1 do
    if aDBGrid.Columns[I].FieldName = aFieldName then
    begin
      Result := aDBGrid.Columns[I];
      exit;
    end;
  Result := nil;
end;

The drawback is that you have to run the loop every time you need to access the grid, causing a small delay, so if speed is essential you might consider other options.

猫九 2024-08-23 10:39:29

无论如何,对于那些到达此页面寻找重新排序网格中列的方法的人(像我一样):

type
  THackAccess = class(TCustomGrid);

procedure TCustomGrid_MoveColumn(grid: TCustomGrid; fromCol, toCol: integer);
begin
  THackAccess(grid).MoveColumn(fromCol+1, toCol+1);
end;

输入列是从零开始的。

Anyway, for those (like me) who reached this page looking for a way to reorder columns in a grid:

type
  THackAccess = class(TCustomGrid);

procedure TCustomGrid_MoveColumn(grid: TCustomGrid; fromCol, toCol: integer);
begin
  THackAccess(grid).MoveColumn(fromCol+1, toCol+1);
end;

Input columns are zero-based.

浅忆流年 2024-08-23 10:39:29

你是对的。您必须跟踪您的列所在的位置。也许在一个单独的结构中,或者作为从 TCustomGrid 派生的后代对象。

我保留一个容器对象,其中存储列的大小、它们包含的数据类型、排序顺序、格式选项以及网格中的位置等内容。然后我有一个引用容器的自定义网格。

type
  TpaGrid = class;
  TpaColumnType = (ctText,ctDateTime,ctNumber,ctSize,ctPic,ctFileName);

  TpaColumn = class(TCollectionItem)
   private
    FCaption: string;
    FTitleFont: TFont;
    FTitleAlignment: TAlignment;
    FDataType : TPaColumnType;
    FWidth: Integer;
    FFont: TFont;
    FColor: TColor;
    FBackColor: TColor;
    FAltBackColor: TColor;
    FAlignment: TAlignment;
    FPosition : integer;
    FSortOrder : integer;   // 0=no sort, 1=first, 2=second, etc...
    FSortAscending : boolean;
    // .... and many other interesting attributes 
   public
    // ... published properties
 end;

 TpaColumnClass = class of TPaColumn;

 TpaColumns = class(TCollection)
  private
   FGrid: TPaGrid;
   // ... Getters and Setters, exposing the items as columns
  public
   constructor Create(grid:TPaGrid; ColumnClass: TPaColumnClass);
   function  AddColumn: TPaColumn;
   // ... Load and Save procedures
   // ... published properties
 end;

 TpaGrid = class (TStringGrid)
  // ... overriden methods WMSize, DrawCell, ...
  // ... getters and setters
  private
   FColumns : TpaColumns;
  // ... 

结尾;

You are right. You have to keep track of where your columns are located. Maybe in a separate structure, or as a descendant object derived from TCustomGrid.

I keep a container object, where I store, among other things, the size of the columns, the type of the data they contain, the sort order, formatting options, and the position in the grid. And then I have a custom grid that references the container.

type
  TpaGrid = class;
  TpaColumnType = (ctText,ctDateTime,ctNumber,ctSize,ctPic,ctFileName);

  TpaColumn = class(TCollectionItem)
   private
    FCaption: string;
    FTitleFont: TFont;
    FTitleAlignment: TAlignment;
    FDataType : TPaColumnType;
    FWidth: Integer;
    FFont: TFont;
    FColor: TColor;
    FBackColor: TColor;
    FAltBackColor: TColor;
    FAlignment: TAlignment;
    FPosition : integer;
    FSortOrder : integer;   // 0=no sort, 1=first, 2=second, etc...
    FSortAscending : boolean;
    // .... and many other interesting attributes 
   public
    // ... published properties
 end;

 TpaColumnClass = class of TPaColumn;

 TpaColumns = class(TCollection)
  private
   FGrid: TPaGrid;
   // ... Getters and Setters, exposing the items as columns
  public
   constructor Create(grid:TPaGrid; ColumnClass: TPaColumnClass);
   function  AddColumn: TPaColumn;
   // ... Load and Save procedures
   // ... published properties
 end;

 TpaGrid = class (TStringGrid)
  // ... overriden methods WMSize, DrawCell, ...
  // ... getters and setters
  private
   FColumns : TpaColumns;
  // ... 

end;

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文