SoloCodigo

Programación General => Delphi => Trucos => Mensaje iniciado por: lair en Lunes 10 de Agosto de 2009, 18:53

Título: Ordenar grid
Publicado por: lair en Lunes 10 de Agosto de 2009, 18:53
Hola a todos este codigo sirve para ordenar los registros contenidos en un dbgrid al dar click en el titulo de cada columna

Código: Delphi
  1. procedure TForm1.DBGrid1MouseMove
  2.   (Sender: TObject; Shift: TShiftState; X, Y: Integer);
  3. var
  4.   pt: TGridcoord;
  5. begin
  6.   pt:= DBGrid1.MouseCoord(x, y);
  7.  
  8.   if pt.y=0 then
  9.     DBGrid1.Cursor:=crHandPoint
  10.   else
  11.     DBGrid1.Cursor:=crDefault;
  12. end;
  13.  

Código: Delphi
  1. procedure TForm1.DBGrid1TitleClick(Column: TColumn);
  2. {$J+}
  3.  const PreviousColumnIndex : integer = -1;
  4. {$J-}
  5. begin
  6.   if DBGrid1.DataSource.DataSet is TCustomADODataSet then
  7.   with TCustomADODataSet(DBGrid1.DataSource.DataSet) do
  8.   begin
  9.     try
  10.       DBGrid1.Columns[PreviousColumnIndex].title.Font.Style :=
  11.       DBGrid1.Columns[PreviousColumnIndex].title.Font.Style - [fsBold];
  12.     except
  13.     end;
  14.  
  15.     Column.title.Font.Style :=
  16.     Column.title.Font.Style + [fsBold];
  17.     PreviousColumnIndex := Column.Index;
  18.  
  19.     if (Pos(Column.Field.FieldName, Sort) = 1) and (Pos(' DESC', Sort)= 0) then
  20.       Sort := Column.Field.FieldName + ' DESC'
  21.     else
  22.       Sort := Column.Field.FieldName + ' ASC';
  23.   end;
  24. end;
  25.  

espero les sea util  :beer: