Ver Mensaje Individual
  #1  
Antiguo 02-05-2008
Avatar de totote
totote totote is offline
Miembro
 
Registrado: oct 2006
Posts: 150
Reputación: 18
totote Va por buen camino
Talking Ordenar datos en un DBGrid con click en cabecera

Bueno como vi que en la parte de trucos no funcionaba para componentes de dbExpress, le pregunte a google y me respondio XD.
Este código pertenece al siguiente sitio

Código que realiza el ordenamiento:
Código Delphi [-]
unit f_Varios;

interface

uses
      TypInfo, DBClient, DB;

function SortCustomClientDataSet(DataSet: TClientDataSet;const FieldName: String): Boolean;

implementation

function SortCustomClientDataSet(DataSet: TClientDataSet;const FieldName: String): Boolean;
var
  i: Integer;
  IndexDefs: TIndexDefs;
  IndexName: String;
  IndexOptions: TIndexOptions;
begin
  Result := False;
  if IsPublishedProp(DataSet, 'IndexDefs') then
    IndexDefs := GetObjectProp(DataSet, 'IndexDefs') as TIndexDefs
  else
    Exit;

  if IsPublishedProp(DataSet, 'IndexName') then
    IndexName := GetStrProp(DataSet, 'IndexName')
  else
    Exit;

  IndexDefs.Update;
  if DataSet.Fields.FindField(FieldName) = nil then Exit;
  if IndexName = FieldName + '__IdxA' then
  begin
    IndexName := FieldName + '__IdxD';
    IndexOptions := [ixDescending];
  end
  else
  begin
    IndexName := FieldName + '__IdxA';
    IndexOptions := [];
  end;
  for i := 0 to Pred(IndexDefs.Count) do
  begin
    if IndexDefs[i].Name = IndexName then
    begin
      Result := True;
      Break
    end; //if
  end; // for
  if not Result then
  begin
    DataSet.AddIndex(IndexName, FieldName,IndexOptions);
    Result := True;
  end; // if not
  SetStrProp(DataSet, 'IndexName', IndexName);
end;

Código para utilizar el ordenamiento:

Código Delphi [-]
procedure TForm1.DBGrid1TitleClick(Column: TColumn);
begin
  inherited;
  SortCustomClientDataset(ClientDataSet1,Column.FieldName);
end;


Saludos
__________________
¡Oh nooo! no compartas, compartir es pirateria, compartir te llevara a la carcel - Revolution OS
Responder Con Cita