ImageVerifierCode 换一换
格式:DOCX , 页数:19 ,大小:25.42KB ,
资源ID:5996315      下载积分:3 金币
快捷下载
登录下载
邮箱/手机:
温馨提示:
快捷下载时,用户名和密码都是您填写的邮箱或者手机号,方便查询和重复下载(系统自动生成)。 如填写123,账号就是123,密码也是123。
特别说明:
请自助下载,系统不会自动发送文件的哦; 如果您已付费,想二次下载,请登录后访问:我的下载记录
支付方式: 支付宝    微信支付   
验证码:   换一换

加入VIP,免费下载
 

温馨提示:由于个人手机设置不同,如果发现不能下载,请复制以下地址【https://www.bdocx.com/down/5996315.html】到电脑端继续下载(重复下载不扣费)。

已注册用户请登录:
账号:
密码:
验证码:   换一换
  忘记密码?
三方登录: 微信登录   QQ登录  

下载须知

1: 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。
2: 试题试卷类文档,如果标题没有明确说明有答案则都视为没有答案,请知晓。
3: 文件的所有权益归上传用户所有。
4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
5. 本站仅提供交流平台,并不能对任何下载内容负责。
6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

版权提示 | 免责声明

本文(Delphi中DBGrid 应用全书.docx)为本站会员(b****6)主动上传,冰豆网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。 若此文所含内容侵犯了您的版权或隐私,请立即通知冰豆网(发送邮件至service@bdocx.com或直接QQ联系客服),我们立即给予删除!

Delphi中DBGrid 应用全书.docx

1、Delphi中DBGrid 应用全书Delphi中DBGrid 应用全书(一)在 Delphi 语言的数据库编程中,DBGrid 是显示数据的主要手段之一。但是 DBGrid 缺省的外观未免显得单调和缺乏创意。其实,我们完全可以在我们的程序中通过编程来达到美化DBGrid 外观的目的。通过编程,我们可以改变 DBGrid 的表头、网格、网格线的前景色和背景色,以及相关的字体的大小和风格。 以下的示例程序演示了对 DBGrid 各属性的设置,使 Delphi 显示的表格就像网页中的表格一样漂亮美观。 示例程序的运行: 在 Form1 上放置 DBGrid1、Query1、DataSource1

2、三个数据库组件,设置相关的属性,使 DBGrid1 能显示表中的数据。然后,在 DBGrid1 的 onDrawColumnCell 事件中键入以下代码,然后运行程序,就可以看到神奇的结果了。本代码在 Windows98、Delphi5.0 环境下调试通过。procedure TMainForm.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn;State: TGridDrawState);var i :integer;begin if gdSelected i

3、n State then Exit;/定义表头的字体和背景颜色: for i :=0 to (Sender as TDBGrid).Columns.Count-1 do begin (Sender as TDBGrid).Columnsi.Title.Font.Name :=宋体; /字体 (Sender as TDBGrid).Columnsi.Title.Font.Size :=9; /字体大小 (Sender as TDBGrid).Columnsi.Title.Font.Color :=$000000ff; /字体颜色(红色) (Sender as TDBGrid).Columnsi.

4、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 T

5、DBGrid).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网格颜色 :

6、 在 Form1 上放置 DBGrid1、Query1、DataSource1 三个数据库组件,设置相关的属性,使 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 t

7、hen 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 /画 cel

8、l 的边框 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设置DBGrid1的OnDrawDataCell事件如下:p

9、rocedure 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; DBC

10、omboBox1.Width := Rect.Right - Rect.Left; DBComboBox1.Height := Rect.Bottom - Rect.Top; DBComboBox1.Visible := True; end; end;end;2)、DBGrid指定单元格未获得焦点时不显示DBComboBox,设置DBGrid1的OnColExit事件如下:procedure TForm1.DBGrid1ColExit(Sender: TObject);begin If DBGrid1.SelectedField.FieldName = DBComboBox1.DataFiel

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

12、 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;打印 TDBGrid内容-方法1procedure PrintDbGrid(DataSet:TDataSet;DbGrid:TDbGrid;Title:String);varPointX

13、,PointY:integer;ScreenX:integer;i,lx,ly:integer;px1,py1,px2,py2:integer;RowPerPage,RowPrinted:integer;ScaleX:Real;THeight:integer;TitleWidth:integer;SumWidth:integer;PageCount:integer;SpaceX,SpaceY:integer;RowCount:integer;beginPointX:=Round(GetDeviceCaps(printer.Handle,LOGPIXELSX)/2.54);PointY:=Rou

14、nd(GetDeviceCaps(printer.Handle,LOGPIXELSY)/2.54);ScreenX:=Round(Screen.PixelsPerInch/2.54);ScaleX:=PointX/ScreenX;RowPrinted:=0;SumWidth:=0;printer.BeginDoc;With Printer.Canvas dobegin DataSet.DisableControls; DataSet.First ; THeight:=Round(TextHeight(我)*1.5);/设定每行高度为字符高的1.5倍 SpaceY:= Round(TextHei

15、ght(我)/4); SpaceX:=Round(TextWidth(我)/4); RowPerpage:=Round(printer.PageHeight-5*PointY)/THeight); /上下边缘各2厘米 ly:=2*PointY; PageCount:=0; while not DataSet.Eof do begin if (RowPrinted=RowPerPage) or (RowPrinted=0) then begin if RowPrinted0 then Printer.NewPage; RowPrinted:=0; PageCount:=PageCount+1;

16、Font.Name:=宋体; Font.size:=16; Font.Style:=Font.Style+fsBold; lx:=Round(Printer.PageWidth-TextWidth(Title)/2); ly:=2*PointY; TextOut(lx,ly,Title); Font.Size:=11; Font.Style:=Font.Style-fsBold; lx:=Printer.PageWidth-5*PointX; ly:=Round(2*PointY+0.2*PointY); if RowPerPage*PageCountDataSet.RecordCount t

17、hen RowCount:=DataSet.RecordCount else RowCount:=RowPerPage*PageCount; TextOut(lx,ly,第+IntToStr(RowPerPage*(PageCount-1)+1)+-+IntToStr(RowCount)+条,共+IntToStr(DataSet.RecordCount)+条); lx:=2*PointX; ly:=ly+THeight*2; py1:=ly-SpaceY; if RowCount=DataSet.RecordCount then py2:=py1+THeight*(RowCount-RowPe

18、rPage*(PageCount-1)+1) else py2:=py1+THeight*(RowPerPage+1); SumWidth:=lx; for i:=0 to DBGrid.Columns.Count-1 do begin px1:=SumWidth-SpaceX; px2:=SumWidth; MoveTo(px1,py1); LineTo(px2,py2); TitleWidth:=TextWidth(DBGrid.Columnsi.Title.Caption); lx:=Round(SumWidth+(DBGrid.Columnsi.width*scaleX-titleWi

19、dth)/2); TextOut(lx,ly,DBGrid.Columnsi.Title.Caption); SumWidth:=Round(SumWidth+DBGrid.Columnsi.width*scaleX)+SpaceX*2; end; px1:=SumWidth; /画最后一条竖线 px2:=SumWidth; MoveTo(px1,py1); LineTo(px2,py2); px1:=2*PointX; /画第一条横线 px2:=SumWidth; py1:=ly-SpaceY; py2:=ly-SpaceY; MoveTo(px1,py1); LineTo(px2,py2)

20、; py1:=py1+THeight; py2:=py2+THeight; MoveTo(px1,py1); LineTo(px2,py2); end; lx:=2*PointX; ly:=ly+THeight; px1:=lx; px2:=SumWidth; py1:=ly-SpaceY+THeight; py2:=ly-SpaceY+THeight; MoveTo(px1,py1); LineTo(px2,py2); for i:=0 to DBGrid.Columns.Count-1 do begin TextOut(lx,ly,DataSet.FieldByname(DBGrid.Co

21、lumnsi.Fieldname).AsString); lx:=Round(lx+DBGrid.Columnsi.width*ScaleX+SpaceX*2); end; RowPrinted:=RowPrinted+1; DataSet.next; end; DataSet.first; DataSet.EnableControls;end;printer.EndDoc;end;把DBGrid输出到Excel表格方法一(支持多Sheet)功能描述:把DBGrid输出到Excel表格(支持多Sheet)调用格式:CopyDbDataToExcel(DBGrid1, DBGrid2);proc

22、edure CopyDbDataToExcel(Args: array of const);var iCount, jCount: Integer; XLApp: Variant; Sheet: Variant; I: Integer;begin Screen.Cursor := crHourGlass; if not VarIsEmpty(XLApp) then begin XLApp.DisplayAlerts := False; XLApp.Quit; VarClear(XLApp); end; try XLApp := CreateOleObject(Excel.Application

23、); except Screen.Cursor := crDefault; Exit; end; XLApp.WorkBooks.Add; XLApp.SheetsInNewWorkbook := High(Args) + 1; for I := Low(Args) to High(Args) do begin XLApp.WorkBooks1.WorkSheetsI+1.Name := TDBGrid(ArgsI.VObject).Name; Sheet := XLApp.Workbooks1.WorkSheetsTDBGrid(ArgsI.VObject).Name; if not TDB

24、Grid(ArgsI.VObject).DataSource.DataSet.Active then begin Screen.Cursor := crDefault; Exit; end; TDBGrid(ArgsI.VObject).DataSource.DataSet.first; for iCount := 0 to TDBGrid(ArgsI.VObject).Columns.Count - 1 do Sheet.Cells1, iCount + 1 := TDBGrid(ArgsI.VObject).Columns.ItemsiCount.Title.Caption; jCount

25、 := 1; while not TDBGrid(ArgsI.VObject).DataSource.DataSet.Eof do begin for iCount := 0 to TDBGrid(ArgsI.VObject).Columns.Count - 1 do Sheet.CellsjCount + 1, iCount + 1 := TDBGrid(ArgsI.VObject).Columns.ItemsiCount.Field.AsString; Inc(jCount); TDBGrid(ArgsI.VObject).DataSource.DataSet.Next; end; XlA

26、pp.Visible := True; end; Screen.Cursor := crDefault;end; dbgrid的數據導入到excel中方法二unit Unit1;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Grids, DBGrids, Db, DBTables, Excel97, OleServer, Word97;type TForm1 = class(TForm) ExcelApplication1: TExcelAppl

27、ication; ExcelWorkbook1: TExcelWorkbook; ExcelWorksheet1: TExcelWorksheet; Table1: TTable; DataSource1: TDataSource; DBGrid1: TDBGrid; Button1: TButton; Button4: TButton; WordApplication1: TWordApplication; WordDocument1: TWordDocument; procedure Button1Click(Sender: TObject); procedure Button4Click

28、(Sender: TObject); private Private declarations public Public declarations end;var Form1: TForm1;implementation$R *.DFMprocedure TForm1.Button1Click(Sender: TObject);var i, row, column: integer;begin Try ExcelApplication1.Connect; Except MessageDlg(Excel may not be installed, mtError, mbOk, 0); Abort; End; ExcelAp

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1