My FAQ,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > Delphi > 临时文章
DBGrid应用全书(一)
作者:CYRTSOFT 时间:2002-09-28 11:42 出处:互联网 责编:MyFAQ
              摘要:DBGrid应用全书(一)
Delphi 语言的数据库编程中,DBGrid 是显示数据的主要手段之一。但是 DBGrid 缺省的外观未免显得单调和缺乏创意。其实,我们完全可以在我们的程序中通过编程来达到美化DBGrid 外观的目的。通过编程,我们可以改变 DBGrid 的表头、网格、网格线的前景色和背景色,以及相关的字体的大小和风格。
   
以下的示例程序演示了对 DBGrid 各属性的设置,使 Delphi 显示的表格就像网页中的表格一样漂亮美观。
   
示例程序的运行:
   
Form1 上放置 DBGrid1Query1DataSource1 三个数据库组件,设置相关的属性,使 DBGrid1 能显示表中的数据。然后,在 DBGrid1 onDrawColumnCell 事件中键入以下代码,然后运行程序,就可以看到神奇的结果了。本代码在 Windows98Delphi5.0 环境下调试通过。
procedure TMainForm.DBGrid1DrawColumnCell(Sender: TObject;
  const Rect: TRect; DataCol: Integer; Column: TColumn;State: TGridDrawState);
var i :integer;
begin
  if gdSelected in State then Exit;
//
定义表头的字体和背景颜色:
    for i :=0 to (Sender as TDBGrid).Columns.Count-1 do
    begin
      (Sender as TDBGrid).Columns[i].Title.Font.Name :='
宋体'; //字体
      (Sender as TDBGrid).Columns[i].Title.Font.Size :=9; //
字体大小
      (Sender as TDBGrid).Columns[i].Title.Font.Color :=$000000ff; //
字体颜色(红色)
      (Sender as TDBGrid).Columns[i].Title.Color :=$0000ff00; //
背景色(绿色)
    end;
//
隔行改变网格背景色:
  if Query1.RecNo mod 2 = 0 then
    (Sender as TDBGrid).Canvas.Brush.Color := clInfoBk //
定义背景颜色
  else
    (Sender as TDBGrid).Canvas.Brush.Color := RGB(191, 255, 223); //
定义背景颜色
//
定义网格线的颜色:
    DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
  with (Sender as TDBGrid).Canvas do //
cell 的边框
  begin
    Pen.Color := $00ff0000; //
定义画笔颜色(蓝色)
    MoveTo(Rect.Left, Rect.Bottom); //
画笔定位
    LineTo(Rect.Right, Rect.Bottom); //
画蓝色的横线
    Pen.Color := $0000ff00; //
定义画笔颜色(绿色)
    MoveTo(Rect.Right, Rect.Top); //
画笔定位
    LineTo(Rect.Right, Rect.Bottom); //
画绿色的竖线
  end;
end;

2.Delphi5 - 隔行改变DBGrid网格颜色

Form1 上放置 DBGrid1Query1DataSource1 三个数据库组件,设置相关的属性,使 DBGrid1 能显示表中的数据。然后,在 DBGrid1 onDrawColumnCell 事件中键入以下代码,然后运行程序
 procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
 DataCol: Integer; Column: TColumn; State: TGridDrawState);
var i:integer;
begin
  if gdSelected in State then Exit;  //
隔行改变网格背景色:
    if adoQuery1.RecNo mod 2 = 0 then
      (Sender as TDBGrid).Canvas.Brush.Color := clinfobk //
定义背景颜色
  else
    (Sender as TDBGrid).Canvas.Brush.Color := RGB(191, 255, 223);  //
定义背景颜色
 //
定义网格线的颜色:
  DBGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State);
  with (Sender as TDBGrid).Canvas do //
cell 的边框
  begin
    Pen.Color := $00ff0000; //
定义画笔颜色(蓝色)
    MoveTo(Rect.Left, Rect.Bottom); //
画笔定位
    LineTo(Rect.Right, Rect.Bottom); //
画蓝色的横线
    Pen.Color := clbtnface; //
定义画笔颜色(兰色)
    MoveTo(Rect.Right, Rect.Top); //
画笔定位
    LineTo(Rect.Right, Rect.Bottom); //
画绿色
  end;
end;

3. DBGrid指定列上显示DBComboBox

设置DBGrid1OnDrawDataCell事件如下:
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect; Field: TField; State: TGridDrawState);
begin
  if (gdFocused in State) then
  begin
    if (Field.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;
2)
DBGrid指定单元格未获得焦点时不显示DBComboBox,设置DBGrid1OnColExit事件如下:
procedure TForm1.DBGrid1ColExit(Sender: TObject);
begin
  If DBGrid1.SelectedField.FieldName = DBComboBox1.DataField then
    begin
      DBComboBox1.Visible := false;
    end;
end;
3)
、当DBGrid指定列获得焦点时DrawDataCell事件只是绘制单元格,并显示DBComboBox,但是DBComboBox并没有获得焦点,数据的输入还是在单元格上进行。在DBGrid1KeyPress事件中调用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_Charword(Key)0);
    end;
  end;
end;


关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 myfaq.com.cn All rights reserved. www.myfaq.com.cn 版权所有