获取DataSet字段的列对象 [英] Get Column Object for DataSet Field

查看:144
本文介绍了获取DataSet字段的列对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说,我有一组 TField 对象,并且想要动态地改变它们在一些DBGrid中的可见性。我还希望允许用户在此DBGrid中更改列顺序,并且有一些固定的列。

Let's say, I have a set of TField objects and want to change their visibility in some DBGrid dynamically. I also want to allow users to change column order in this DBGrid, and have some fixed columns.

我知道适当的方式来隐藏/显示DBGrid的列,但在VCL有一个大的,大的设计缺陷:有Column对象,可以移动到它的Field对象,但不能找回方式。

I know proper way to hide/show columns on DBGrid, but there is a big, big design flaw in VCL: having Column object, one can travel to it's Field object, but cannot find way back.

我想要的只是Field对象中的一种Column属性,所以我可以输入类似这样:

All I want is just kind of Column property inside Field object, so I can type something like this:

Field.Column.Visible := False;

并隐藏DBGrid中具有指定列属性的任何列。

and hide any column inside DBGrid with assigned Columns property.

我知道我可以创建HashSet或者Collection with Columns并快速找到对应的列,但是还有更直接的方法来做这个?

I know I can just create HashSet or Collection with Columns and quickly find corresponding columns, but is there exist more straight way to do this?

推荐答案

如果列存储,可以使用类似:

If column are stored, may use something like:

function FindFieldColumn(Grid : TDBGrid; const FieldName: String):  TColumn;
var
  i: Integer;
begin
  Result := nil;
  for i := 0 to Grid.Columns.Count - 1 do
    if AnsiCompareText(Grid.Columns[i].FieldName, FieldName) = 0 then
      begin
         Result := Grid.Columns[i];
         Break;
      end;
end;

procedure SetVisibleColumn(Grid : TDBGrid; AFieldName : string; AVisible : boolean);
var
  Column : TColumn;
begin
  Column := FindFieldColumn(Grid,AFieldName);
  if Assigned(Column) then
     Column.Visible := AVisible;
end; 

拨打

SetVisibleColumn(MyGrid,MyField.Name, myField.Visible);

这篇关于获取DataSet字段的列对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆