Ver Mensaje Individual
  #1  
Antiguo 05-10-2015
Avatar de jeremiselxi
jeremiselxi jeremiselxi is offline
Miembro
 
Registrado: ago 2008
Posts: 199
Reputación: 16
jeremiselxi Va por buen camino
Ordenar DBGRID en Delphi 10 Seattle.

Buen día.

Muchos me imagino que han utilizado este trozo de código para ordenar campos en delphi 7:
Código Delphi [-]
procedure TForm1.DBGrid1TitleClick(Column: TColumn);
{$J+}
 const PreviousColumnIndex : integer = -1;
{$J-}
begin
  if DBGrid1.DataSource.DataSet is TCustomADODataSet then
  with TCustomADODataSet(DBGrid1.DataSource.DataSet) do
  begin
    try
      DBGrid1.Columns[PreviousColumnIndex].title.Font.Style :=
      DBGrid1.Columns[PreviousColumnIndex].title.Font.Style - [fsBold];
    except
    end;

    Column.title.Font.Style := 
    Column.title.Font.Style + [fsBold];
    PreviousColumnIndex := Column.Index;

    if (Pos(Column.Field.FieldName, Sort) = 1)
    and (Pos(' DESC', Sort)= 0) then
      Sort := Column.Field.FieldName + ' DESC'
    else
      Sort := Column.Field.FieldName + ' ASC';
  end;
end;

pero al utilizar una de las versiones XE en adelante ya no funciona.

En este sentido, luego de investigar y realizar pruebas, adjunto la forma de como ordenarlo inclusive para la ultima versión Delphi 10 seattle.

Código Delphi [-]
procedure TForm1.DBGrid1TitleClick(Column: TColumn);
{$J+}
 const PreviousColumnIndex : integer = -1;
{$J-}
begin
  if DBGrid1.DataSource.DataSet is TCustomADODataSet then
  with TCustomADODataSet(DBGrid1.DataSource.DataSet) do
  begin
    try
      DBGrid1.Columns[PreviousColumnIndex].title.Font.Style :=
      DBGrid1.Columns[PreviousColumnIndex].title.Font.Style - [fsBold];
    except
    end;

    Column.title.Font.Style :=
    Column.title.Font.Style + [fsBold];
    PreviousColumnIndex := Column.Index;

if  (ANSIPos(ANSIString(Column.Field.FieldName), Sort) = 1) and
    (ANSIPos(ANSIString(' DESC'), Sort)= 0) then
//antes
//    if (Pos(Column.Field.FieldName, Sort) = 1) and (Pos(' DESC', Sort)= 0) then
      Sort := Column.Field.FieldName + ' DESC'
    else
      Sort := Column.Field.FieldName + ' ASC';
  end;
end;

Espero sea de utilidad ya que me han ayudado bastante y con esto aporto mi pedazo de arena.

Muchos saludos desde República Dominicana.
__________________
Cristo te ama, ven a d él, ya k te espera con los brazos abiertos. Dios te bendiga mucho
Responder Con Cita