Fórum DBGrid #354494
27/02/2008
0
Srs.,
criei esse componente endando do TDbGrid para mostrar as linhas zebrada e se for lincado um ClientDataSet permite ordernar as colunas clicando no Titulo dos campos, se usar outros componentes tipo o ADOQuery, ADOTable, ADODataSet funciona mas não vai ordenar as clicando no Titulo da Coluna.
Se alguem tiver uma idéia para ordenar as colunas usando outros componentes de acesso a dados que não seja o ClientDataSet agradeço...
Att
criei esse componente endando do TDbGrid para mostrar as linhas zebrada e se for lincado um ClientDataSet permite ordernar as colunas clicando no Titulo dos campos, se usar outros componentes tipo o ADOQuery, ADOTable, ADODataSet funciona mas não vai ordenar as clicando no Titulo da Coluna.
Se alguem tiver uma idéia para ordenar as colunas usando outros componentes de acesso a dados que não seja o ClientDataSet agradeço...
Att
unit llDbGrid;
interface
uses
Windows, SysUtils, Classes, Controls, Grids, DbGrids, Graphics, DBClient,
Dialogs, Db;
type
{ TllDbGrid }
TllDbGrid = class(TDBGrid)
private
FCorLinhaImpar: TColor;
FCorLinhaPar: TColor;
FColunaSelecionada: String;
procedure SetCorLinhaImpar(Value: TColor);
procedure SetCorLinhaPar(Value: TColor);
protected
procedure Notification(AComponent: TComponent; Operation: TOperation); override;
procedure TitleClick(Column: TColumn); override;
procedure DrawColumnCell(const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState); override;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property CorLinhaImpar: TColor read FCorLinhaImpar write SetCorLinhaImpar;
property CorLinhaPar: TColor read FCorLinhaPar write SetCorLinhaPar;
end;
//procedure Register;
implementation
{procedure Register;
begin
RegisterComponents(´Samples´,[TllDbGrid]);
end;}
{ TllDbGrid }
constructor TllDbGrid.Create(AOwner: TComponent);
begin
inherited;
FCorLinhaImpar := $009DFFFD;
FCorLinhaPar := $00FFEFDF;
Options := Options - [dgEditing,dgColumnResize,dgConfirmDelete];
Options := Options + [dgAlwaysShowSelection];
end;
destructor TllDbGrid.Destroy;
begin
inherited;
end;
procedure TllDbGrid.Notification(AComponent: TComponent; Operation: TOperation);
begin
inherited;
case Operation of
opRemove:
begin
//if AComponent = FClientDataSet then
// FClientDataSet := nil;
end;
end;
end;
procedure TllDbGrid.KeyDown(var Key: Word; Shift: TShiftState);
begin
if ((Shift = [ssCtrl]) and (key = vk_delete)) then Abort;
inherited;
end;
procedure TllDbGrid.SetCorLinhaImpar(Value: TColor);
begin
if FCorLinhaImpar <> Value then
begin
FCorLinhaImpar := Value;
InvalidateGrid;
end;
end;
procedure TllDbGrid.SetCorLinhaPar(Value: TColor);
begin
if FCorLinhaPar <> Value then
begin
FCorLinhaPar := Value;
InvalidateGrid;
end;
end;
procedure TllDbGrid.TitleClick(Column: TColumn);
var
ClientDataSet: TClientDataSet;
Indice: String;
FlagDesc: Boolean;
I: Integer;
Index: Integer;
begin
if (DataLink.DataSource.DataSet is TClientDataSet) then
begin
ClientDataSet := TClientDataSet(DataLink.DataSource.DataSet);
Index := Column.Index;
if Index > Self.Columns.Count-1 then
Index := Self.Columns.Count-1;
if Index = 0 then
for I := 0 to Self.Columns.Count -1 do
if not Self.Columns[I].Visible then Inc(Index);
for I := 0 to Self.Columns.Count -1 do
Self.Columns[I].Title.Color := clBtnFace;
Self.Columns[Index].Title.Color := clSilver;
FColunaSelecionada := Self.Columns[Index].FieldName;
// Ordena a Coluna do DbGrid
Indice := ´Indice´+IntToStr(Random(9999));
FlagDesc := True;
if ClientDataSet.IndexDefs.Count > 0 then
begin
FlagDesc := not (ixDescending in ClientDataSet.IndexDefs[0].Options);
if FlagDesc and (ClientDataSet.IndexDefs[0].Fields <> FColunaSelecionada) then
FlagDesc := False;
end;
ClientDataSet.IndexDefs.Clear;
case FlagDesc of
True: ClientDataSet.IndexDefs.Add(Indice,FColunaSelecionada,[ixDescending]);
False: ClientDataSet.IndexDefs.Add(Indice,FColunaSelecionada,[]);
end;
ClientDataSet.IndexName := Indice;
end;
inherited;
end;
procedure TllDbGrid.DrawColumnCell(const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
R: TRect;
begin
inherited;
if not (gdSelected in State) then // Se a célula não está selecionada
begin
case Odd(DataSource.DataSet.RecNo) of // Se for ímpar
True: Self.Canvas.Brush.Color := FCorLinhaImpar;
False: Self.Canvas.Brush.Color := FCorLinhaPar; // Define uma cor de fundo
end;
end
else
begin
Self.Canvas.Brush.Color := clHighlight;
Self.Canvas.Font.Color := clHighlightText;
end;
Self.Canvas.FillRect(Rect); // Pinta a célula
Canvas.Font.Name := Column.Font.Name;
Canvas.Font.Size := Column.Font.Size;
R := Rect;
case Column.Alignment of
taRightJustify: R.Left := R.Right - Canvas.TextWidth(Column.Field.AsString)-5;
taCenter: R.Left := R.Left + ((R.Right-R.Left) div 2) - (Canvas.TextWidth(Column.Field.AsString) div 2) - 2;
end;
Self.DefaultDrawDataCell(R,Column.Field,State); // Pinta o texto padrão
inherited;
end;
end.
Luciano.lirio
Curtir tópico
+ 0
Responder
Clique aqui para fazer login e interagir na Comunidade :)