DBGrid com componente que manipule datas

09/12/2003

1

Olá colegas,

preciso colocar em uma DbGrid algum componente que manupule datas. Tipo o DateTimePicker. Alguém pode me ajudar?

Grato pela atenção.


Responder

Posts

09/12/2003

Bampavao

Entra em contato comigo no e-mail bampavao@editorasaraiva.com.br que eu te mando um exemplo com fontes.


Responder

10/12/2003

Nigro

Olá amigo.
Porque não posta o fonte aqui no forum pois essa é a idéia de um forum o pode servir para mais pessoas sem a necessidade de ficarem te incomodando com um monte de e-mail´s, o que acha?


Responder

10/12/2003

Bampavao

Segue o fonte...

(*
ClubeDelphi Magazine
Segredos do DBGrid
Guinther Pauli
guinther@clubedelphi.com.br
Neste exemplo, aprenda como:
* Mostrar e editar campos do memo
* Mostrar e editar campos gráficos
* Mostrar um DateTimePicker em campos de data
* Simular um ´chexkbox´ em campos booleanos
* Ordenar dados (clicando no título da coluna)
* Formatar celulas
* Pintar linhas individuais
* Definir listas suspendidas
* Mostrar campos do memo usando LineBreak
* Colocar ícones pequenos nas celulas
* Todas as características estão disponiveis tbm numa versão para Kylix (CLX)
*)

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, DBClient, Grids, DBGrids, StdCtrls, ImgList, ComCtrls;

type
TForm1 = class(TForm)
ClientDataSet1: TClientDataSet;
ClientDataSet1NOME_CLIENTE: TStringField;
ClientDataSet1SEXO: TStringField;
ClientDataSet1ID_EMPRESA: TIntegerField;
ClientDataSet1DATA_NASC: TDateField;
ClientDataSet1FOTO: TBlobField;
ClientDataSet1OBS: TBlobField;
ClientDataSet1ATIVO: TBooleanField;
DataSource1: TDataSource;
DBGrid1: TDBGrid;
ClientDataSet1SALDO: TCurrencyField;
ClientDataSet2: TClientDataSet;
ClientDataSet1NOME_EMPRESA: TStringField;
ImageList1: TImageList;
ClientDataSet1ID_CLIENTE: TIntegerField;
DateTimePicker1: TDateTimePicker;
procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
procedure DBGrid1TitleClick(Column: TColumn);
procedure DBGrid1EditButtonClick(Sender: TObject);
procedure DBGrid1CellClick(Column: TColumn);
procedure DBGrid1ColEnter(Sender: TObject);
procedure DateTimePicker1Change(Sender: TObject);
procedure FormShow(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

uses DBCtrls, ExtCtrls, ExtDlgs;

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
R : TRect;
begin
(*DBGrid zebrado*)
if not odd(ClientDataSet1.RecNo) then // se for ímpar
if not (gdSelected in State) then // se a célula não está selecionada
begin
DBGrid1.Canvas.Brush.Color:=$00FFEFDF; // define uma cor de fundo
DBGrid1.Canvas.FillRect(Rect); // pinta a célula
DBGrid1.DefaultDrawDataCell(rect,Column.Field,State); // pinta o texto padrão
end;
(*DateTimePicker*)
DateTimePicker1.Visible :=
DBGrid1.SelectedField = ClientDataSet1DATA_NASC;
if DateTimePicker1.Visible then
if gdFocused in State then
begin
DateTimePicker1.Date :=
ClientDataSet1DATA_NASC.AsDateTime;
DateTimePicker1.SetBounds(
Rect.Left,Rect.Top,
Rect.Right-Rect.Left,
Rect.Bottom-Rect.Top);
end;
(*destaca campo SALDO*)
if Column.Field = ClientDataSet1SALDO then
if ClientDataSet1SALDO.AsCurrency < 200 then
begin
DBGrid1.Canvas.Font.Style :=
DBGrid1.Canvas.Font.Style + [fsBold];
DBGrid1.Canvas.Font.Color := clRed;
DBGrid1.DefaultDrawDataCell(rect,Column.Field,State); // pinta o texto padrão
end;
(*pinta checkbox*)
if Column.Field=ClientDataSet1ATIVO then
begin
DBGrid1.Canvas.FillRect(Rect);
ImageList1.Draw(DBGrid1.Canvas,Rect.Left+10,Rect.Top+1,0);
if ClientDataSet1ATIVO.AsBoolean then
ImageList1.Draw(DBGrid1.Canvas,Rect.Left+10,Rect.Top+1,2)
else
ImageList1.Draw(DBGrid1.Canvas,Rect.Left+10,Rect.Top+1,1);
end;
(*Pinta ícone para o campo sexo*)
if Column.Field=ClientDataSet1SEXO then
begin
DBGrid1.Canvas.FillRect(Rect);
DBGrid1.DefaultDrawDataCell(rect,Column.Field,State); // pinta o texto padrão
if ClientDataSet1SEXO.AsString=´M´ then
ImageList1.Draw(DBGrid1.Canvas,Rect.Left+16,Rect.Top+1,3);
if ClientDataSet1SEXO.AsString=´F´ then
ImageList1.Draw(DBGrid1.Canvas,Rect.Left+16,Rect.Top+1,4);
end;
(*pinta o campo foto*)
if Column.Field = ClientDataSet1FOTO then
begin
if not (gdSelected in State) then // se não for a célula selecionada
DBGrid1.Canvas.FillRect(Rect); // limpa a célula
with TPicture.Create do
begin
Assign(ClientDataSet1FOTO);
DBGrid1.Canvas.StretchDraw(Rect,Bitmap); // desenha imagem
Free;
end;
end;
(*exibe campo memo*)
R:=Rect; // declara uma variável local R : TRect
Dec(R.Bottom,2);
if Column.Field=ClientDataSet1OBS then
begin
if not (gdSelected in State) then
DBGrid1.Canvas.FillRect(Rect);
DrawText(DBGrid1.Canvas.Handle,
pchar(ClientDataSet1OBS.AsString),
length(ClientDataSet1OBS.AsString),
R,DT_WORDBREAK);
end;
end;

procedure TForm1.DBGrid1TitleClick(Column: TColumn);
begin
(*recupera a cor original da coluna*)
if ClientDataSet1.IndexFieldNames <> ´´ then
DBGrid1.Columns[ClientDataSet1.FieldByName
(ClientDataSet1.IndexFieldNames).Index].Title.Color :=
DBGrid1.FixedColor;
(*se campo não for blob ou memo e se for do tipo data*)
if not (Column.Field.DataType in [ftBlob,ftMemo]) then
if Column.Field.FieldKind = fkData then
begin
ClientDataSet1.IndexFieldNames := Column.FieldName; // indexa
Column.Title.Color := $008A8AFF; // coloca cor na coluna
end;
end;

procedure TForm1.DBGrid1EditButtonClick(Sender: TObject);
var
OD : TOpenPictureDialog; // declare ExtDlgs no uses
Frm : TForm;
MM : TDBMemo; // declare DBCtrls no uses
begin
(*se o campo selecionado for FOTO*)
if DBGrid1.SelectedField = ClientDataSet1FOTO then
begin
OD := TOpenPictureDialog.create(nil);
try
if OD.Execute then
begin
(*edita e atribui campo*)
ClientDataSet1.Edit;
ClientDataSet1FOTO.LoadFromFile(OD.FileName);
ClientDataSet1.Post;
end;
finally
OD.free;
end;
end;
(*se o campo selecionado for OBS*)
if DBGrid1.SelectedField=ClientDataSet1OBS then
begin
Frm := TForm.Create(nil);
try
Frm.Width := 240;
Frm.Height := 120;
Frm.Top := Mouse.CursorPos.Y;
Frm.Left := Mouse.CursorPos.X;
Frm.BorderStyle := bsToolWindow;
Frm.Caption := ´Editando Observação´;
MM := TDBMemo.Create(nil);
try
MM.Parent := Frm;
MM.Align := alClient;
MM.DataSource := DataSource1;
MM.DataField := ´OBS´;
Frm.ShowModal;
if ClientDataSet1.State in [dsedit,dsinsert] then
ClientDataSet1.Post;
finally
MM.Free;
end;
finally
Frm.Free;
end;
end;
end;

procedure TForm1.DBGrid1CellClick(Column: TColumn);
begin
if Column.Field=ClientDataSet1ATIVO then
begin
ClientDataSet1.Edit;
ClientDataSet1ATIVO.AsBoolean:=not
ClientDataSet1ATIVO.AsBoolean;
end;
end;

procedure TForm1.DBGrid1ColEnter(Sender: TObject);
begin
if DBGrid1.SelectedField=ClientDataSet1ATIVO then
DBGrid1.Options := DBGrid1.Options - [dgEditing]
else
DBGrid1.Options := DBGrid1.Options + [dgEditing];
end;

procedure TForm1.DateTimePicker1Change(Sender: TObject);
begin
ClientDataSet1.Edit;
ClientDataSet1DATA_NASC.AsDateTime :=
DateTimePicker1.Date;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
DBGrid1TitleClick(DBGrid1.Columns[1]);
end;

end.


Responder

10/12/2003

Bampavao

Segue a versão CLX...

unit Unit1;

interface

uses
SysUtils, Types, Classes, Variants, QTypes, QGraphics, QControls, QForms,
QDialogs, QStdCtrls, QImgList, DB, DBClient, QGrids, QDBGrids;

type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
ClientDataSet1: TClientDataSet;
ClientDataSet1ID_CLIENTE: TIntegerField;
ClientDataSet1NOME_CLIENTE: TStringField;
ClientDataSet1SEXO: TStringField;
ClientDataSet1ID_EMPRESA: TIntegerField;
ClientDataSet1DATA_NASC: TDateField;
ClientDataSet1SALDO: TCurrencyField;
ClientDataSet1FOTO: TBlobField;
ClientDataSet1OBS: TBlobField;
ClientDataSet1ATIVO: TBooleanField;
ClientDataSet1NOME_EMPRESA: TStringField;
DataSource1: TDataSource;
ClientDataSet2: TClientDataSet;
ImageList1: TImageList;
procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
procedure DBGrid1CellClick(Column: TColumn);
procedure DBGrid1ColEnter(Sender: TObject);
procedure DBGrid1EditButtonClick(Sender: TObject);
procedure DBGrid1TitleClick(Column: TColumn);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.xfm}

uses QDBCtrls, Qt;

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
R : TRect;
begin
(*DBGrid zebrado*)
if not odd(ClientDataSet1.RecNo) then // se for ímpar
if not (gdSelected in State) then // se a célula não está selecionada
begin
DBGrid1.Canvas.Brush.Color:=$00FFEFDF; // define uma cor de fundo
DBGrid1.Canvas.FillRect(Rect); // pinta a célula
DBGrid1.DefaultDrawDataCell(rect,Column.Field,State); // pinta o texto padrão
end;
(*destaca campo SALDO*)
if Column.Field = ClientDataSet1SALDO then
if ClientDataSet1SALDO.AsCurrency < 200 then
begin
DBGrid1.Canvas.Font.Style :=
DBGrid1.Canvas.Font.Style + [fsBold];
DBGrid1.Canvas.Font.Color := clRed;
DBGrid1.DefaultDrawDataCell(rect,Column.Field,State); // pinta o texto padrão
end;
(*pinta checkbox*)
if Column.Field=ClientDataSet1ATIVO then
begin
DBGrid1.Canvas.FillRect(Rect);
ImageList1.Draw(DBGrid1.Canvas,Rect.Left+10,Rect.Top+1,0);
if ClientDataSet1ATIVO.AsBoolean then
ImageList1.Draw(DBGrid1.Canvas,Rect.Left+10,Rect.Top+1,2)
else
ImageList1.Draw(DBGrid1.Canvas,Rect.Left+10,Rect.Top+1,1);
end;
(*Pinta ícone para o campo sexo*)
if Column.Field=ClientDataSet1SEXO then
begin
DBGrid1.Canvas.FillRect(Rect);
DBGrid1.DefaultDrawDataCell(rect,Column.Field,State); // pinta o texto padrão
if ClientDataSet1SEXO.AsString=´M´ then
ImageList1.Draw(DBGrid1.Canvas,Rect.Left+16,Rect.Top+1,3);
if ClientDataSet1SEXO.AsString=´F´ then
ImageList1.Draw(DBGrid1.Canvas,Rect.Left+16,Rect.Top+1,4);
end;
(*pinta o campo foto*)
if Column.Field = ClientDataSet1FOTO then
begin
if not (gdSelected in State) then // se não for a célula selecionada
DBGrid1.Canvas.FillRect(Rect); // limpa a célula
with TPicture.Create do
begin
Assign(ClientDataSet1FOTO);
DBGrid1.Canvas.StretchDraw(Rect,Bitmap); // desenha imagem
Free;
end;
end;
(*exibe campo memo*)
R:=Rect; // declara uma variável local R : TRect
Dec(R.Bottom,2);
if Column.Field=ClientDataSet1OBS then
begin
if not (gdSelected in State) then
DBGrid1.Canvas.FillRect(Rect);
DBGrid1.Canvas.TextRect(R,R.Left,R.Top,
ClientDataSet1OBS.AsString,Integer(AlignmentFlags_WordBreak));
//QPainter_drawText
end;
end;

procedure TForm1.DBGrid1CellClick(Column: TColumn);
begin
if Column.Field=ClientDataSet1ATIVO then
begin
ClientDataSet1.Edit;
ClientDataSet1ATIVO.AsBoolean:=not
ClientDataSet1ATIVO.AsBoolean;
ClientDataSet1.Post
end;
end;

procedure TForm1.DBGrid1ColEnter(Sender: TObject);
begin
if DBGrid1.SelectedField=ClientDataSet1ATIVO then
DBGrid1.Options := DBGrid1.Options - [dgEditing]
else
DBGrid1.Options := DBGrid1.Options + [dgEditing];
end;

procedure TForm1.DBGrid1EditButtonClick(Sender: TObject);
var
OD : TOpenDialog; // declare ExtDlgs no uses
Frm : TForm;
MM : TDBMemo; // declare DBCtrls no uses
begin
(*se o campo selecionado for FOTO*)
if DBGrid1.SelectedField = ClientDataSet1FOTO then
begin
OD := TOpenDialog.create(nil);
try
if OD.Execute then
begin
(*edita e atribui campo*)
ClientDataSet1.Edit;
ClientDataSet1FOTO.LoadFromFile(OD.FileName);
ClientDataSet1.Post;
end;
finally
OD.free;
end;
end;
(*se o campo selecionado for OBS*)
if DBGrid1.SelectedField=ClientDataSet1OBS then
begin
Frm := TForm.Create(nil);
try
Frm.Width:=240;
Frm.Height:=120;
Frm.Top:=Mouse.CursorPos.Y;
Frm.Left:=Mouse.CursorPos.X;
// Frm.BorderStyle := bsToolWindow;
Frm.Caption := ´Editando Observação´;
MM:=TDBMemo.Create(nil);
try
MM.Parent := Frm;
MM.Align := alClient;
MM.DataSource := DataSource1;
MM.DataField := ´OBS´;
Frm.ShowModal;
if ClientDataSet1.State in [dsedit,dsinsert] then
ClientDataSet1.Post;
finally
MM.Free;
end;
finally
Frm.Free;
end;
end;
end;

procedure TForm1.DBGrid1TitleClick(Column: TColumn);
begin
(*recupera a cor original da coluna*)
if ClientDataSet1.IndexFieldNames <> ´´ then
DBGrid1.Columns[ClientDataSet1.FieldByName
(ClientDataSet1.IndexFieldNames).Index].Title.Color :=
DBGrid1.FixedColor;
(*se campo não for blob ou memo e se for do tipo data*)
if not (Column.Field.DataType in [ftBlob,ftMemo]) then
if Column.Field.FieldKind = fkData then
begin
ClientDataSet1.IndexFieldNames := Column.FieldName; // indexa
Column.Title.Color := $00FFBB77; // coloca cor na coluna
end;
end;

end.


Responder