Ver Mensaje Individual
  #1  
Antiguo 18-01-2008
Avatar de enecumene
[enecumene] enecumene is offline
Miembro de Oro
 
Registrado: may 2006
Ubicación: Santo Domingo, Rep. Dom.
Posts: 3.040
Reputación: 21
enecumene Va por buen camino
Ordenar datos en un DBGrid con click en cabecera

Este es otro truco tambien de Zarko Garjic de about.com, cuya funcion es ordenar los datos de un DBGrid al clickear en la cabecera del mismo, aqui el codigo:

1. Fijar las coordenadas del mouse en el DBGrid:

Código Delphi [-]
procedure TForm1.DBGrid1MouseMove
  (Sender: TObject; Shift: TShiftState; X, Y: Integer);
var
  pt: TGridcoord;
begin
  pt:= DBGrid1.MouseCoord(x, y);

  if pt.y=0 then
    DBGrid1.Cursor:=crHandPoint
  else
    DBGrid1.Cursor:=crDefault;
end;

2. Procedimiento para el click en el titulo del DBGrid en el evento OnTitleClick del DBGrid:

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;

Y eso es todo y que lo disfruten!!

Saludos.

Responder Con Cita