 |
ActiveDelphi .: O site do programador Delphi! :.
|
Exibir mensagem anterior :: Exibir próxima mensagem |
Autor |
Mensagem |
fabiodurgante Aprendiz

Registrado: Sexta-Feira, 8 de Novembro de 2013 Mensagens: 197
|
Enviada: Dom Ago 06, 2017 11:13 am Assunto: checkbox dentro dbgrid |
|
|
seguinte tenho ibquery1 com as colunas descricao status, quero escrever no dbgrid cluna descricao e ao lado na coluna status um checkbox e na estou conseguindo adicinar o checkbox, vi na internet que é no evento DBGrid1DrawColumnCell mas como poderia fazer ???? e verificar se foi selecionado ou nao o checkbox ??? |
|
Voltar ao Topo |
|
 |
leo_cj Colaborador

Registrado: Sábado, 26 de Março de 2011 Mensagens: 1335
|
Enviada: Seg Ago 07, 2017 8:58 am Assunto: |
|
|
Eu faço da seguinte maneira:
Evento OnDrawColumnCell:
Código: | procedure TfrmBaseFinder.grServersDrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
Check: Integer;
R: TRect;
begin
if Column.FieldName = 'Selected' then
begin
grServers.Canvas.FillRect(Rect);
if cdsServersSelected.AsString = 'X' then
Check := DFCS_CHECKED
else
Check := 0;
R := Rect;
InflateRect(R, -2, -2);
DrawFrameControl(grServers.Canvas.Handle, R, DFC_BUTTON,
DFCS_BUTTONCHECK or Check);
end;
end; |
e quanto a saber se foi clicado ou não, no evento OnClickCell eu tenho o seguinte:
Código: | procedure TfrmBaseFinder.grServersCellClick(Column: TColumn);
var
val, Server: String;
bases: TStringList;
tmp: TStringList;
Cont: Integer;
ContInt: Integer;
begin
val := IfThen(cdsServersSelected.AsString = 'X', EmptyStr, 'X');
Server := cdsServers.FieldByName('Servidor').AsString;
cdsServers.Edit;
cdsServersSelected.AsString := val;
cdsServers.Post;
if (val = 'X') then
begin
{Código quando o checkbox foi marcado}
end
else
begin
{Código quando o checkbox foi desmarcado}
end;
end; |
|
|
Voltar ao Topo |
|
 |
fabiodurgante Aprendiz

Registrado: Sexta-Feira, 8 de Novembro de 2013 Mensagens: 197
|
Enviada: Seg Ago 07, 2017 4:46 pm Assunto: |
|
|
o que e ssa linha aqui ??
if cdsServersSelected.AsString = 'X' then
Código: |
procedure TForm5.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
Check: Integer;
R: TRect;
begin
if Column.FieldName = 'Selected' then
begin
dbgrid1.Canvas.FillRect(Rect);
if Column.FieldName = 'SEL' then
Check := DFCS_CHECKED
else
Check := 0;
R := Rect;
InflateRect(R, -2, -2);
DrawFrameControl(dbgrid1.Canvas.Handle, R, DFC_BUTTON,
DFCS_BUTTONCHECK or Check);
end;
end;
|
coloquei assi e nao funcionou o SEL e minha coluna da pesquiza sel tem nome e SEL no dbgrid |
|
Voltar ao Topo |
|
 |
leo_cj Colaborador

Registrado: Sábado, 26 de Março de 2011 Mensagens: 1335
|
Enviada: Seg Ago 07, 2017 4:55 pm Assunto: |
|
|
No meu caso, a coluna se chama Selected, quando ela está marcada ela fica com o valor X('X') e quando ela não está marcada fica com valor vazio('' ou EmptyStr)
então na linha if cdsServersSelected.AsString = 'X' then eu estou verificando se a coluna está marcada |
|
Voltar ao Topo |
|
 |
fabiodurgante Aprendiz

Registrado: Sexta-Feira, 8 de Novembro de 2013 Mensagens: 197
|
Enviada: Seg Ago 07, 2017 5:05 pm Assunto: |
|
|
Meu dbgrid tem coluna nome é sel como faço para add os checkbox ele n adicionar os checkbox na coluna Sel |
|
Voltar ao Topo |
|
 |
leo_cj Colaborador

Registrado: Sábado, 26 de Março de 2011 Mensagens: 1335
|
Enviada: Seg Ago 07, 2017 5:10 pm Assunto: |
|
|
Sua coluna precisa estar adicionada na fonte de dados do seu Grid, seja ela um ClientDataSet ou uma Query.
nas linhas que tiver os códigos
Código: | Column.FieldName = 'Selected'
cdsServersSelected.AsString |
altere para a sua coluna, ex:
Código: | Column.FieldName = 'Sel'
cdsServersSel.AsString |
|
|
Voltar ao Topo |
|
 |
fabiodurgante Aprendiz

Registrado: Sexta-Feira, 8 de Novembro de 2013 Mensagens: 197
|
Enviada: Seg Ago 07, 2017 6:30 pm Assunto: |
|
|
isso daq erro nessa linha
cdsServersSel.AsString
unknow cdsServersSel o que seria isso ???
eu uso a ibquery1 para ter as colunas produto e sel seria assim
if IBQuery1.FieldByName('status').value = 'V' then
minha coluna sel possui o valor V colocar o checkbox no lugar do valor V, ams assim nao funcionou nao adicionou o checkbox nas linhas do dbgrid |
|
Voltar ao Topo |
|
 |
johnny-walker Moderador


Registrado: Sábado, 4 de Outubro de 2003 Mensagens: 10653 Localização: Contagem/MG - BRAZIL
|
Enviada: Seg Ago 07, 2017 9:24 pm Assunto: |
|
|
Eis aí:
Código: | unit uMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DBCtrls, DB, DBClient, Grids, DBGrids;
type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
DataSource1: TDataSource;
ClientDataSet1: TClientDataSet;
ClientDataSet1ID: TAutoIncField;
ClientDataSet1NOME: TStringField;
ClientDataSet1DIRETOR: TBooleanField;
DBCheckBox1: TDBCheckBox;
procedure FormCreate(Sender: TObject);
procedure DBGrid1ColExit(Sender: TObject);
procedure DBGrid1KeyPress(Sender: TObject; var Key: Char);
procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
begin
DBCheckBox1.DataSource := DataSource1;
DBCheckBox1.DataField := 'DIRETOR';
DBCheckBox1.Visible := False;
DBCheckBox1.Color := DBGrid1.Color;
DBCheckBox1.Caption := '';
DBCheckBox1.AllowGrayed:= False;
//explained later in the article
DBCheckBox1.ValueChecked := 'T';
DBCheckBox1.ValueUnChecked := 'F';
end;
procedure TForm1.DBGrid1ColExit(Sender: TObject);
begin
if DBGrid1.SelectedField <> nil then
if DBGrid1.SelectedField.FieldName = DBCheckBox1.DataField then
DBCheckBox1.Visible := False
end;
procedure TForm1.DBGrid1KeyPress(Sender: TObject; var Key: Char);
begin
if (Key = Chr(9)) then
Exit;
if (DBGrid1.SelectedField.FieldName = DBCheckBox1.DataField) then
begin
DBCheckBox1.SetFocus;
SendMessage(DBCheckBox1.Handle, WM_Char, word(Key), 0);
end;
end;
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
const IsChecked : array[Boolean] of Integer =
(DFCS_BUTTONCHECK, DFCS_BUTTONCHECK or DFCS_CHECKED);
var
DrawState: Integer;
DrawRect: TRect;
begin
if gdSelected in State then
begin
Canvas.FillRect(Rect);
Canvas.Brush.Color := clHighLight;//clBackground; // Cor de seleção
Canvas.Font.Color := clHighLightText;//clWhite;//$00FFF2D9;//clYellow;
end
else
inherited;
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
if (gdFocused in State) then
begin
if (Column.Field.FieldName = DBCheckBox1.DataField) then
begin
DBCheckBox1.Left := Rect.Left + DBGrid1.Left + 2;
DBCheckBox1.Top := Rect.Top + DBGrid1.top + 2;
DBCheckBox1.Width := Rect.Right - Rect.Left;
DBCheckBox1.Height := Rect.Bottom - Rect.Top;
DBCheckBox1.Visible := True;
end
end
else
begin
if Column.Field <> nil then
if (Column.Field.FieldName = DBCheckBox1.DataField) then
begin
DrawRect:=Rect;
InflateRect(DrawRect,-1,-1);
DrawState := ISChecked[Column.Field.AsBoolean];
DBGrid1.Canvas.FillRect(Rect);
DrawFrameControl(DBGrid1.Canvas.Handle, DrawRect,
DFC_BUTTON, DrawState);
end;
end;
end;
end.
|
bye _________________ P.O.W.E.R B.Y D.E.L.P.H.I |
|
Voltar ao Topo |
|
 |
leo_cj Colaborador

Registrado: Sábado, 26 de Março de 2011 Mensagens: 1335
|
Enviada: Ter Ago 08, 2017 8:47 am Assunto: |
|
|
fabiodurgante escreveu: | isso daq erro nessa linha
cdsServersSel.AsString
unknow cdsServersSel o que seria isso ???
eu uso a ibquery1 para ter as colunas produto e sel seria assim
if IBQuery1.FieldByName('status').value = 'V' then
minha coluna sel possui o valor V colocar o checkbox no lugar do valor V, ams assim nao funcionou nao adicionou o checkbox nas linhas do dbgrid |
Amigo, o código que eu passei seria um exemplo de como funciona, cdsServers é o nome do meu componente TClientDataSet, cdsServersSel faz referência ao campo Sel dentro do cdsServers, pra fazer funcionar você vai ter que fazer adequações de acordo com os componentes que você está utilizando.
Sugiro também que verifique a alternativa apresentada pelo Johnny, está bem completa, mas lembre-se de configurar as propriedades do CheckBox no FormCreate de acordo com o seu cenário |
|
Voltar ao Topo |
|
 |
fabiodurgante Aprendiz

Registrado: Sexta-Feira, 8 de Novembro de 2013 Mensagens: 197
|
Enviada: Ter Ago 08, 2017 11:37 am Assunto: |
|
|
funcionou porem so seleciono 1 checkbox quando vou selecionar outro desmarca o que deveria estar marcado sempre desmarca 1 para marcar outro, como fazer para poder marcar todos ou somente 2 3 4 5 assim por diante ?? |
|
Voltar ao Topo |
|
 |
natanbh1 Colaborador

Registrado: Terça-Feira, 15 de Março de 2011 Mensagens: 3093 Localização: Belo Horizonte - MG
|
Enviada: Ter Ago 08, 2017 11:43 am Assunto: |
|
|
Se puder, poste seu código para que possamos analisar. _________________ ''A persistência é o caminho para o êxito.''
Charlie Chaplin |
|
Voltar ao Topo |
|
 |
johnny-walker Moderador


Registrado: Sábado, 4 de Outubro de 2003 Mensagens: 10653 Localização: Contagem/MG - BRAZIL
|
Enviada: Qua Ago 09, 2017 7:49 pm Assunto: |
|
|
O problema é que você está fazendo errado, visto que há de ter mais de um checkbox para cada field, veja:
Código: | unit uMain;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DBCtrls, DB, DBClient, Grids, DBGrids;
type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
DataSource1: TDataSource;
ClientDataSet1: TClientDataSet;
ClientDataSet1ID: TAutoIncField;
ClientDataSet1NOME: TStringField;
ClientDataSet1DIRETOR: TBooleanField;
ClientDataSet1PRESIDENTE: TBooleanField;
procedure FormCreate(Sender: TObject);
procedure DBGrid1ColExit(Sender: TObject);
procedure DBGrid1KeyPress(Sender: TObject; var Key: Char);
procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
procedure FormDestroy(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
DBCheckbox: Array[1..2] of TDBCheckBox;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
I: Byte;
begin
for I := Low(DBCheckbox) to High(DBCheckbox) do
begin
DBCheckBox[I] := TDBCheckBox.Create(Self);
DBCheckBox[I].Parent := Self;
DBCheckBox[I].DataSource := DataSource1;
DBCheckBox[I].Visible := False;
DBCheckBox[I].Color := DBGrid1.Color;
DBCheckBox[I].Caption := '';
end;
DBCheckBox[1].DataField := 'DIRETOR';
DBCheckBox[2].DataField := 'PRESIDENTE';
{
DBCheckBox1.DataSource := DataSource1;
DBCheckBox1.DataField := 'DIRETOR';
DBCheckBox1.Visible := False;
DBCheckBox1.Color := DBGrid1.Color;
DBCheckBox1.Caption := '';
}
//explained later in the article
//DBCheckBox1.ValueChecked := 'S';
//DBCheckBox1.ValueUnChecked := 'N';
end;
procedure TForm1.DBGrid1ColExit(Sender: TObject);
begin
if DBGrid1.SelectedField.FieldName = DBCheckBox[1].DataField then
DBCheckBox[1].Visible := False;
if DBGrid1.SelectedField.FieldName = DBCheckBox[2].DataField then
DBCheckBox[2].Visible := False;
end;
procedure TForm1.DBGrid1KeyPress(Sender: TObject; var Key: Char);
begin
if (Key = Chr(9)) then
Exit;
if (DBGrid1.SelectedField.FieldName = DBCheckBox[1].DataField) then
begin
DBCheckBox[1].SetFocus;
SendMessage(DBCheckBox[1].Handle, WM_Char, word(Key), 0);
end;
if (DBGrid1.SelectedField.FieldName = DBCheckBox[2].DataField) then
begin
DBCheckBox[2].SetFocus;
SendMessage(DBCheckBox[2].Handle, WM_Char, word(Key), 0);
end;
end;
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
const IsChecked : array[Boolean] of Integer =
(DFCS_BUTTONCHECK, DFCS_BUTTONCHECK or DFCS_CHECKED);
var
DrawState: Integer;
DrawRect: TRect;
begin
if (gdFocused in State) then
begin
if (Column.Field.FieldName = DBCheckBox[1].DataField) then
begin
DBCheckBox[1].Left := Rect.Left + DBGrid1.Left + 2;
DBCheckBox[1].Top := Rect.Top + DBGrid1.top + 2;
DBCheckBox[1].Width := Rect.Right - Rect.Left;
DBCheckBox[1].Height := Rect.Bottom - Rect.Top;
DBCheckBox[1].Visible := True;
end;
if (Column.Field.FieldName = DBCheckBox[2].DataField) then
begin
DBCheckBox[2].Left := Rect.Left + DBGrid1.Left + 2;
DBCheckBox[2].Top := Rect.Top + DBGrid1.top + 2;
DBCheckBox[2].Width := Rect.Right - Rect.Left;
DBCheckBox[2].Height := Rect.Bottom - Rect.Top;
DBCheckBox[2].Visible := True;
end;
end
else
begin
if (Column.Field.FieldName = DBCheckBox[1].DataField) then
begin
DrawRect:=Rect;
InflateRect(DrawRect,-1,-1);
DrawState := ISChecked[Column.Field.AsBoolean];
DBGrid1.Canvas.FillRect(Rect);
DrawFrameControl(DBGrid1.Canvas.Handle, DrawRect,
DFC_BUTTON, DrawState);
end;
if (Column.Field.FieldName = DBCheckBox[2].DataField) then
begin
DrawRect:=Rect;
InflateRect(DrawRect,-1,-1);
DrawState := ISChecked[Column.Field.AsBoolean];
DBGrid1.Canvas.FillRect(Rect);
DrawFrameControl(DBGrid1.Canvas.Handle, DrawRect,
DFC_BUTTON, DrawState);
end;
end;
end;
procedure TForm1.FormDestroy(Sender: TObject);
var
I: Byte;
begin
for I := Low(DBCheckbox) to High(DBCheckbox) do
begin
FreeAndNil(DBCheckBox[I]);
end;
end;
end.
|
bye _________________ P.O.W.E.R B.Y D.E.L.P.H.I |
|
Voltar ao Topo |
|
 |
|
|
Enviar Mensagens Novas: Proibido. Responder Tópicos Proibido Editar Mensagens: Proibido. Excluir Mensagens: Proibido. Votar em Enquetes: Proibido.
|
|