Caros, hoje precisei colocar um checkbox no header de uma coluna da cxGrid, então criei uma unit para tal tarefa, segue código;
//Jucelio Moura, j03071986@gmail.com
unit uCxGridTools;
interface
uses System.SysUtils, System.Classes, Vcl.Graphics, Json,
cxGridDBTableView, Data.DB, System.Math, cxCheckBox;
type
TImageCollun = class
private
FSizeStr: Int64;
FImageStr: string;
function GetImageStr: TBitmap;
function StrToBitmap: TBitmap;
public
constructor Create(const ASize: Int64; AImageStr: string);
published
property Image: TBitmap read GetImageStr;
end;
THelperTcxGridDBColumn = class helper for TcxGridDBColumn
private
procedure TableViewFlagHeaderClick(Sender: TObject);
procedure ChangesBoolValue(const AValue: Boolean);
function GetImage(const AValue: Boolean): TBitmap;
procedure SetSettings;
public
procedure ChangesBool(const AValue: Boolean = False);
end;
implementation
{ TImageCollun }
constructor TImageCollun.Create(const ASize: Int64; AImageStr: string);
begin
FSizeStr:= ASize;
FImageStr:= AImageStr;
end;
function TImageCollun.GetImageStr: TBitmap;
begin
Result:= StrToBitmap;
end;
function TImageCollun.StrToBitmap: TBitmap;
var
loArqStream: TStringStream;
loFileString: String;
loFileSize, loi: Integer;
loArrayStringsFile: TStringList;
loFileBytes: Tbytes;
begin
try
try
loFileString := FImageStr;
loFileSize := FSizeStr;
loArrayStringsFile := TStringList.Create;
ExtractStrings([','], [' '], PChar(loFileString),
loArrayStringsFile);
SetLength(loFileBytes, loFileSize);
for loi := 0 to Pred(loFileSize) do
loFileBytes[loi] := StrToInt(loArrayStringsFile[loi]);
loArqStream := TStringStream.Create(loFileBytes);
Result:= TBitmap.Create;
Result.LoadFromStream(loArqStream);
finally
loArqStream.Free;
loArrayStringsFile.Free;
end;
except
FreeAndNil(Result);
raise;
end;
end;
{ THelperTcxGridDBColumn }
procedure THelperTcxGridDBColumn.ChangesBool(const AValue: Boolean = False);
begin
OnHeaderClick:= TableViewFlagHeaderClick;
HeaderGlyph:= GetImage(AValue);
SetSettings;
Tag:= IfThen(AValue, 1, 0);
end;
procedure THelperTcxGridDBColumn.ChangesBoolValue(const AValue: Boolean);
var
loBook: TBookmark;
begin
with TcxGridDBTableView(Self.GridView).DataController.DataSource.DataSet do
begin
loBook:= Bookmark;
DisableControls;
try
First;
while not Eof do
begin
Edit;
FieldByName(Self.DataBinding.FieldName).AsBoolean:= AValue;
Post;
Next;
end;
finally
if BookmarkValid(loBook) then
GotoBookmark(loBook);
EnableControls;
end;
end;
end;
function THelperTcxGridDBColumn.GetImage(const AValue: Boolean): TBitmap;
var
loImageCollun: TImageCollun;
loImgStr: string;
loSize: Int64;
begin
case AValue of
True: begin
loSize:= 822;
loImgStr:= '66, 77, 54, 3, 0, 0, 0, 0, 0, 0, 54, 0, 0, 0, 40, 0, 0, 0, 16, 0, 0, 0, 16, 0, 0, 0, 1, 0, 24, 0, 0, 0, 0, 0, 0, 3, 0, 0, 196, 14, 0, '+
'0, 196, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 254, 254, 254, 191, 191, 191, 167, 167, 167, 167, 167, 167, 167, '+
'167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, '+
'191, 191, 191, 254, 254, 254, 255, 255, 255, 254, 254, 254, 53, 53, 53, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '+
'0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 53, 53, 53, 254, 254, 254, 191, 191, 191, 0, 0, 0, 200, 200, 200, 255, '+
'255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, '+
'255, 255, 255, 255, 255, 255, 200, 200, 200, 0, 0, 0, 191, 191, 191, 167, 167, 167, 0, 0, 0, 255, 255, 255, 255, 255, '+
'255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, '+
'255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 167, 167, 167, 167, 167, 167, 0, 0, 0, 255, 255, 255, 255, 255, 255, '+
'255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, '+
'255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 167, 167, 167, 167, 167, 167, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, '+
'255, 255, 255, 255, 255, 247, 247, 247, 229, 229, 229, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, '+
'255, 255, 255, 255, 255, 255, 0, 0, 0, 167, 167, 167, 167, 167, 167, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, '+
'255, 243, 243, 243, 60, 60, 60, 1, 1, 1, 157, 157, 157, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, '+
'255, 255, 255, 0, 0, 0, 167, 167, 167, 167, 167, 167, 0, 0, 0, 255, 255, 255, 255, 255, 255, 251, 251, 251, 60, 60, 60, '+
'23, 23, 23, 107, 107, 107, 1, 1, 1, 157, 157, 157, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, '+
'167, 167, 167, 167, 167, 167, 0, 0, 0, 255, 255, 255, 255, 255, 255, 244, 244, 244, 74, 74, 74, 212, 212, 212, 255, '+
'255, 255, 127, 127, 127, 1, 1, 1, 157, 157, 157, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 167, 167, 167, '+
'167, 167, 167, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, '+
'255, 255, 255, 127, 127, 127, 1, 1, 1, 242, 242, 242, 255, 255, 255, 255, 255, 255, 0, 0, 0, 167, 167, 167, 167, 167, '+
'167, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, '+
'255, 255, 255, 255, 225, 225, 225, 253, 253, 253, 255, 255, 255, 255, 255, 255, 0, 0, 0, 167, 167, 167, 167, 167, '+
'167, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, '+
'255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 167, 167, 167, 167, 167, '+
'167, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, '+
'255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 167, 167, 167, 191, 191, '+
'191, 0, 0, 0, 200, 200, 200, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, '+
'255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 200, 200, 200, 0, 0, 0, 191, 191, 191, 254, 254, '+
'254, 53, 53, 53, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 53, 53, '+
'53, 254, 254, 254, 255, 255, 255, 254, 254, 254, 191, 191, 191, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, '+
'167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 191, 191, 191, '+
'254, 254, 254, 255, 255, 255';
end;
False: begin
loSize:= 822;
loImgStr:= '66, 77, 54, 3, 0, 0, 0, 0, 0, 0, 54, 0, 0, 0, 40, 0, 0, 0, 16, 0, 0, 0, 16, 0, 0, 0, 1, 0, 24, 0, 0, 0, 0, 0, 0, 3, 0, 0, 196, 14, 0, '+
'0, 196, 14, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 255, 255, 255, 254, 254, 254, 191, 191, 191, 167, 167, 167, 167, 167, 167, 167, '+
'167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, '+
'191, 191, 191, 254, 254, 254, 255, 255, 255, 254, 254, 254, 53, 53, 53, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '+
'0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 53, 53, 53, 254, 254, 254, 191, 191, 191, 0, 0, 0, 200, 200, 200, 255, '+
'255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, '+
'255, 255, 255, 255, 255, 255, 200, 200, 200, 0, 0, 0, 191, 191, 191, 167, 167, 167, 0, 0, 0, 255, 255, 255, 255, 255, '+
'255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, '+
'255, 255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 167, 167, 167, 167, 167, 167, 0, 0, 0, 255, 255, 255, 255, 255, 255, '+
'255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, '+
'255, 255, 255, 255, 255, 255, 255, 0, 0, 0, 167, 167, 167, 167, 167, 167, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, '+
'255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, '+
'255, 255, 255, 255, 255, 255, 0, 0, 0, 167, 167, 167, 167, 167, 167, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, '+
'255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, '+
'255, 255, 255, 255, 255, 0, 0, 0, 167, 167, 167, 167, 167, 167, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, '+
'255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, '+
'255, 255, 255, 255, 0, 0, 0, 167, 167, 167, 167, 167, 167, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, '+
'255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, '+
'255, 255, 255, 0, 0, 0, 167, 167, 167, 167, 167, 167, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, '+
'255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, '+
'255, 255, 0, 0, 0, 167, 167, 167, 167, 167, 167, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, '+
'255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, '+
'255, 0, 0, 0, 167, 167, 167, 167, 167, 167, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, '+
'255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, '+
'0, 0, 0, 167, 167, 167, 167, 167, 167, 0, 0, 0, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, '+
'255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 0, 0, '+
'0, 167, 167, 167, 191, 191, 191, 0, 0, 0, 200, 200, 200, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, '+
'255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 200, 200, 200, 0, 0, 0, '+
'191, 191, 191, 254, 254, 254, 53, 53, 53, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '+
'0, 0, 0, 0, 0, 0, 53, 53, 53, 254, 254, 254, 255, 255, 255, 254, 254, 254, 191, 191, 191, 167, 167, 167, 167, 167, 167, '+
'167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, 167, '+
'167, 191, 191, 191, 254, 254, 254, 255, 255, 255';
end;
end;
loImageCollun:= TImageCollun.Create(loSize, loImgStr);
try
Result:= loImageCollun.GetImageStr;
finally
loImageCollun.Free;
end;
end;
procedure THelperTcxGridDBColumn.SetSettings;
begin
HeaderGlyphAlignmentHorz:= taCenter;
with Options do
begin
FilteringPopup:= False;
Sorting:= False;
end;
PropertiesClassName:= 'TcxCheckBoxProperties';
with TcxCheckBoxProperties(Properties) do
begin
ImmediatePost:= True;
end;
end;
procedure THelperTcxGridDBColumn.TableViewFlagHeaderClick(Sender: TObject);
begin
TcxGridDBColumn(Sender).HeaderGlyph:= nil;
case TcxGridDBColumn(Sender).tag of
0: begin
TcxGridDBColumn(Sender).Tag:= 1;
TcxGridDBColumn(Sender).HeaderGlyph:= GetImage(True);
ChangesBoolValue(True);
end;
1: begin
TcxGridDBColumn(Sender).Tag:= 0;
TcxGridDBColumn(Sender).HeaderGlyph:= GetImage(false);
ChangesBoolValue(False);
end;
end;
end;
end.
- Implementação;
cxGridDBTableView.GetColumnByFieldName('SeuFieldBoolean').ChangesBool();
Vá com calma meu fi! kkk
ResponderExcluir