打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
dbgrid 使用小结
1、插入其他可视组件(注意:自动生成列和手动生成时的用法有所不同,手动时用DBGrid1DrawDataCell没有反应)

DrawDataCell事件是绘制单元格,当获得焦点网格所对应的字段与组合框所对应的字段一致时,移动组合框到获得焦点的网格上,并且使组合框可视,从而达到在DBGrid指定列上显示DBComboBox的功能。设置DBGrid1的OnDrawDataCell事件如下:

procedure TForm1.DBGrid1DrawColumnCell
(Sender: TObject; const Rect: TRect;
Column: TColumn; State: TGridDrawState);
begin
if (gdFocused in State) then
begin
if (Column.FieldName = DBComboBox1.DataField ) then
begin
DBComboBox1.Left := Rect.Left + DBGrid1.Left;
DBComboBox1.Top := Rect.Top + DBGrid1.top;
DBComboBox1.Width := Rect.Right - Rect.Left;
DBComboBox1.Height := Rect.Bottom - Rect.Top;
DBComboBox1.Visible := True;
end;
end;
end;

DBGrid指定单元格未获得焦点时不显示DBComboBox,设置DBGrid1的OnColExit事件如下: 
procedure TForm1.DBGrid1ColExit(Sender: TObject);
begin
If DBGrid1.SelectedField.FieldName 
= DBComboBox1.DataField then
begin
DBComboBox1.Visible := false;
end;
end;
当DBGrid指定列获得焦点时DrawDataCell事件只是绘制单元格,并显示DBComboBox,但是DBComboBox并没有获得焦点,数据的输入还是在单元格上进行。在DBGrid1的KeyPress事件中调用SendMessage这个 Windows API函数将数据输入传输到DBComboBox上,从而达到在DBComboBox上进行数据输入。因此还要设置KeyPress事件如下:

procedure TForm1.DBGrid1KeyPress
(Sender: TObject; var Key: Char);
begin
if (key < > chr(9)) then
begin
if (DBGrid1.SelectedField.FieldName
=DBComboBox1.DataField) then
begin
DBComboBox1.SetFocus;
SendMessage(DBComboBox1.Handle, 
WM_Char, word(Key), 0);
end;
end;
end;
****************************************************************************
2、针对某个条件改变颜色(和1有一样的问题)

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if (Column.FieldName='Area') then begin
dbgrid1.Canvas.Brush.Color:=clred;
(Sender as TDBGrid).Canvas.FillRect(Rect);
(Sender as TDBGrid).Canvas.TextRect(Rect, Rect.Left + 2, Rect.Top + 2,table1Area.asstring);
end;
end;


3.在最后一行按向下键头时不让增加数据:
先定义一个伪子类:
type
TMydbgrid=class(TDbgrid)
end;


procedure TkaoqinForm.DBGrid1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if key=40 then
if TMydbgrid(dbgrid1).Row>=TMydbgrid(dbgrid1).RowCount-1 then abort;
//if dm.ry3.recno=dm.ry3.RecordCount then abort;
end;


4。回车键移动光标:

procedure TTimeTableForm.wwDBGrid1KeyDown(Sender: TObject; var Key: Word;
Shift: TShiftState);
begin
if Key=13 then
with TwwDBGrid(Sender) do
if selectedindex < (fieldcount - 1) then
selectedindex := selectedindex + 1
else selectedindex := 0;

end;

5.控制每一行的颜色。
procedure TkaoqinForm.DBGrid1DrawDataCell(Sender: TObject;
const Rect: TRect; Field: TField; State: TGridDrawState);
begin
if (dbgrid1.DataSource.DataSet.RecNo mod 2)=0 then
begin
dbgrid1.Canvas.Font.Color:=clblue;
dbgrid1.Canvas.Brush.Color:=clMoneyGreen;
end;
dbgrid1.DefaultDrawDataCell(rect,field,state);
end;

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
DBGrid的应用举例 --逆水行舟
在Delphi的DBGrid中插入其他可视组件
转载DBGrid和DBGridEH
delphi中TDBGrid的使用
在DBGrid增加一列CheckBox(而非DBCheckBox)
Delphi的图形处理(一)
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服