ClientDataSet 排序 筆記

ClientDataSet 排序 筆記

本來因該是用TkbmMemTable

但不知為何被改掉ClientDataSet

後來找到一些參考資料就照著修改

不過還是遇到一些小衝突expression of type 'TAction' found

原因是我的action內也有exit ,

就把該段判別field name 和 type拿掉..

不然笨一點就If xxx then 空白  else 繼續跑即可

if (Field = nil ) or (
 (Field is TObjectField) or (Field is TBlobField) or
 (Field is TAggregateField) or (Field is TVariantField)
  or (Field is TBinaryField) ) then  
else

不過還是偷懶一下,直接拿掉了。


 


procedure TQRY25.DBGrid13TitleClick(Column: TColumn);
begin
  inherited;

//TkbmMemTable 元件
  {
  TkbmMemTable(DBGrid13.DataSource.DataSet).SortFields := Column.FieldName;
  TkbmMemTable(DBGrid13.DataSource.DataSet).Sort([]);
  }
  //add by Joelee 20141008 純asc 排序
 { case Column.Index of
    0 : kbmShot.IndexFieldNames := ''; // no Index
  else
    kbmShot.IndexFieldNames := Column.FieldName; // Sort by Clicked Column
  end;
 }//end by joelee   
  
   //add by Joelee 20141009
   SortClientDataSet(kbmShot, Column.FieldName);
   //end by joelee
end;

 

function TQRY25.SortClientDataSet(ClientDataSet: TClientDataSet; const FieldName: string): Boolean;
var
  i: Integer;
  NewIndexName: string;
  IndexOptions: TIndexOptions;
  Field: TField;
begin
  Result := False;
  Field := ClientDataSet.Fields.FindField(FieldName);
  ClientDataSet.IndexDefs.Update;
  if ClientDataSet.IndexName = FieldName + '__IdxA'
    then
  begin
    NewIndexName := FieldName + '__IdxD';
    IndexOptions := [ixDescending];
  end
  else
  begin
    NewIndexName := FieldName + '__IdxA';
    IndexOptions := [];
  end;
//Look for existing index
  for i := 0 to Pred(ClientDataSet.IndexDefs.Count) do
  begin
    if ClientDataSet.IndexDefs[i].Name = NewIndexName then
    begin
      Result := True;
      Break
    end; //if
  end; // for
//If existing index not found, create one
  if not Result then
  begin
    ClientDataSet.AddIndex(NewIndexName,
      FieldName, IndexOptions);
    Result := True;
  end; // if not
//Set the index
  ClientDataSet.IndexName := NewIndexName;
end;

 

 

參考網址

http://edn.embarcadero.com/article/29056

 

 

 

 

大家一起加入blogads 賺零用錢!!