您现在的位置:首页 >> VCL >> VCL >> 内容

Delphi中使用cxGrid对数据集进行Sort和Locate操作

时间:2011/9/3 16:23:19 点击:

  核心提示:在编写某个系统时,由于使用了数据集类型无关技术(即数据集可能是ADOQuery,也有可能是TClientDataSet等等)。当需要对数据进行排序和查找时,只好利用cxGrid自身的功能来实现:fun...

在编写某个系统时,由于使用了数据集类型无关技术(即数据集可能是ADOQuery,也有可能是TClientDataSet等等)。当需要对数据进行排序和查找时,只好利用cxGrid自身的功能来实现:

function GridSortColumn(View : TcxGridDBTableView; FieldName : String) : Boolean;
var
  i : Integer;
begin
  {数据排序}
  Result := False;
  for i := 0 to View.ColumnCount -1 do
  begin
    if (UpperCase(View.Columns[i].DataBinding.FieldName) = FieldName) then
    begin
      View.Columns[i].SortIndex := 0;
      View.Columns[i].SortOrder := soAscending;
      Result := True;
    end
    else
    begin
      View.Columns[i].SortIndex := -1;
      View.Columns[i].SortOrder := soNone;
    end;
  end;
end;

function GridLocateRecord(View : TcxGridDBTableView; FieldName, LocateValue : String) : Boolean;
begin
  {数据查找}
  Result := False;
  if (View.GetColumnByFieldName(FieldName) <> nil) then
    Result := View.DataController.Search.Locate(View.GetColumnByFieldName(FieldName).Index, LocateValue);
end;

对于cxGrid排序后的数据集,不应该进行Next, Prior等操作,因为数据集的排序并没有变化,进行Next等操作时,会出现记录乱跳的情况,应该如下处理:

Prior:
DBView.DataController.FocusedRowIndex := DBView.DataController.FocusedRowIndex - 1;

Next:
DBView.DataController.FocusedRowIndex := DBView.DataController.FocusedRowIndex + 1;

作者:overblue 来源:转载
共有评论 0相关评论
发表我的评论
  • 大名:
  • 内容:
本类推荐
  • 没有
本类固顶
  • 没有
  • 盒子文章(www.2ccc.com) © 2024 版权所有 All Rights Reserved.
  • 沪ICP备05001939号