unit grid;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, DB, DBTables, Grids, DBGrids, StdCtrls, jpeg, ExtCtrls;

type
  TForm1 = class(TForm)
    Image1: TImage;
    Label1: TLabel;
    Label2: TLabel;
    Query1: TQuery;
    DataSource1: TDataSource;
    DBGrid1: TDBGrid;
    Button1: TButton;
    Query2: TQuery;
    DataSource2: TDataSource;
    DBGrid2: TDBGrid;
    Query1CODVENDA: TFloatField;
    Query1CODPROD: TFloatField;
    Query1QTDE: TFloatField;
    Query1UNITARIO: TCurrencyField;
    Query1TOTAL: TCurrencyField;
    Edit1: TEdit;
	function ConsultaNormal(ConsultaNormal:TQuery;Expressao:string):Boolean;
    procedure Button1Click(Sender: TObject);
    procedure DBGrid1TitleClick(Column: TColumn);
    procedure DBGrid1KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure DBGrid1KeyPress(Sender: TObject; var Key: Char);
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function Tform1.ConsultaNormal(ConsultaNormal:TQuery;Expressao:string):Boolean;
var resultado:boolean;
begin
  resultado := true;
  consultaNormal.active := false; consultaNormal.sql.clear; consultaNormal.sql.add(expressao);
  consultaNormal.active := true;
  if consultaNormal.recordcount = 0 then
     resultado := false;
  result := resultado;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
//   DBGRID1.Color := clinfobk;
//   dbgrid1.Columns.Clear;
//	  showmessage(inttostr(dbgrid1.Columns.Count));
end;

procedure TForm1.DBGrid1TitleClick(Column: TColumn);
var
   i:integer;
begin
   For i := 0 to DbGrid1.Columns.Count -1 do
      DBgrid1.Columns.Items[i].Color := ClWindow;
   DBGrid1.Columns.Items[Column.ID].Color := clInfoBk;
   ConsultaNormal(Query1,'select * from cliente order by '+column.FieldName);
end;

procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
	if key = 13 then
   	key := 9;
end;

procedure TForm1.DBGrid1KeyPress(Sender: TObject; var Key: Char);
begin
	Key := upcase(key);
   if (key = DecimalSeparator) then begin
   	showmessage(inttostr(Pos(DecimalSeparator,dbgrid1.Fields[3].Text)));
      showmessage(dbgrid1.Fields[3].Text);
	   if (Pos(DecimalSeparator,dbgrid1.Fields[3].Text) > 0) then key := #0;
   end;
end;
procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
begin
if (key = DecimalSeparator) then begin
	showmessage(inttostr(Pos(DecimalSeparator,Edit1.Text)));
   if (Pos(DecimalSeparator, Edit1.Text) > 0) then key := #0;
end;

if not ((key in ['0'..'9']) or (key = #13) or (key =#8) or (key=',')) then key := #0;

end;

end.
