上次"笑一笑"提供了一段代码如下:
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if gdfocused in state then
begin
if column.FieldName='Zh' then
begin
form1.ComboBox1.Left:=rect.Left+form1.DBGrid1.Left;
form1.ComboBox1.Top:=rect.Top+form1.DBGrid1.Top;
form1.ComboBox1.Width:=rect.Right-rect.Left;
form1.ComboBox1.Height:=rect.Bottom-rect.Top;
form1.ComboBox1.Visible:=true;
end;
end;
end; 我先试了一下checkbox,可以为每条记录生成checkbox,但初始状态还是原来字段的数据,要点选的时侯才能出来,我怎么把初始状态与checkbox进行关联(比如1为选中);还有事件的触发,因为一个Form上有两个DBGRID(数据相关),我想点击checkbox的时侯,把值传到自己数据表的同时,把值也传到另一个的DBGIRD相应字段进行更新.
谢谢!
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if gdfocused in state then
begin
if column.FieldName='Zh' then
begin
form1.ComboBox1.Left:=rect.Left+form1.DBGrid1.Left;
form1.ComboBox1.Top:=rect.Top+form1.DBGrid1.Top;
form1.ComboBox1.Width:=rect.Right-rect.Left;
form1.ComboBox1.Height:=rect.Bottom-rect.Top;
form1.ComboBox1.Visible:=true;
end;
end;
end; 我先试了一下checkbox,可以为每条记录生成checkbox,但初始状态还是原来字段的数据,要点选的时侯才能出来,我怎么把初始状态与checkbox进行关联(比如1为选中);还有事件的触发,因为一个Form上有两个DBGRID(数据相关),我想点击checkbox的时侯,把值传到自己数据表的同时,把值也传到另一个的DBGIRD相应字段进行更新.
谢谢!
解决方案 »
- 简单问题
- Delphi 如何调用Matlab神经网络工具箱?
- 类设计问题(静态方法、类方法、public与virtual/dynamic等,问题多多,555~~~),问题尖锐~~~
- 兑现诺言,给分1
- ????????这什么CSDN的论坛特别慢????
- 如何去掉字符串中间的空格? 谢谢.
- delphi7 安装 fastreports2.51出错,求大神解答
- 关于FlatSpeedButton的Caption换行问题
- 请高手帮我写一个有关字符串中汉字/英文字数的函数,谢谢,高分
- 有没有专门讲Delphi函数和类库的书啊?
- quickrep打印问题,很急很急,我三天没解决.谢谢啦,顶者有分
- 这样的过程怎么声明呢
效果很完美
分数太少
懒得动手
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DB, ADODB, Grids, DBGrids,SHELLAPI, StdCtrls;type
TForm1 = class(TForm)
DBGrid1: TDBGrid;
ADOQuery1: TADOQuery;
DataSource1: TDataSource;
procedure DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
procedure DBGrid1CellClick(Column: TColumn);
procedure DBGrid1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
procedure DBGrid1ColEnter(Sender: TObject);
procedure DBGrid1ColExit(Sender: TObject);
private
{ Private declarations }
PreOptions: TDBGridOptions; //保存Dbgrid option值
procedure SaveBoolean;
public
{ Public declarations }
end;var
Form1: TForm1;implementationuses Unit2;{$R *.dfm}procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
const// 这个整数值将按照布尔值返回,并送入数组
CtrlState : array[Boolean] of Integer = (DFCS_BUTTONCHECK,DFCS_BUTTONCHECK or DFCS_CHECKED);
begin
//确保只有在逻辑字段才能插入组件
if Column.Field.DataType = ftBoolean then
begin
DBGrid1.Canvas.FillRect(Rect);
DrawFrameControl(DBGrid1.Canvas.Handle, Rect, DFC_BUTTON, CtrlState[Column.Field.AsBoolean]);
end;
end;procedure TForm1.DBGrid1CellClick(Column: TColumn);
begin
if DBGrid1.SelectedField.DataType = ftBoolean then
SaveBoolean();
end;procedure TForm1.DBGrid1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
//确保该栏是逻辑字段和空格键在键盘中被敲击
if ( Key = VK_SPACE ) and
( DBGrid1.SelectedField.DataType = ftBoolean ) then
SaveBoolean();end;
procedure TForm1.SaveBoolean;
begin
//保存结果
DBGrid1.SelectedField.Dataset.Edit;
DBGrid1.SelectedField.AsBoolean := not DBGrid1.SelectedField.AsBoolean;
DBGrid1.SelectedField.Dataset.Post;
end;procedure TForm1.DBGrid1ColEnter(Sender: TObject);
beginif DBGrid1.SelectedField.DataType = ftBoolean then
begin
PreOptions := DBGrid1.Options;
DBGrid1.Options := DBGrid1.Options - [dgEditing];
end;
end;procedure TForm1.DBGrid1ColExit(Sender: TObject);
begin
if DBGrid1.SelectedField.DataType = ftBoolean then
DBGrid1.Options := PreOptions;
end;end.