DBGrid应用全书.doc_第1页
DBGrid应用全书.doc_第2页
DBGrid应用全书.doc_第3页
DBGrid应用全书.doc_第4页
DBGrid应用全书.doc_第5页
已阅读5页,还剩5页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

DBGrid 应用全书 Posted on 2010-02-05 09:07 大椰网吧 阅读(96) 评论(0) 编辑 收藏 * *在Delphi语言的数据库编程中,DBGrid是显示数据的主要手段之一。 *但是DBGrid缺省的外观未免显得单调和缺乏创意。其实,我们完全可 *以在我们的程序中通过编程来达到美化DBGrid外观的目的。通过编程, *我们可以改变DBGrid的表头、网格、网格线的前景色和背景色,以及 *相关的字体的大小和风格。 *转自:jinjazz落寞刺客 *DBGrid应用全书感谢archonwang *airii的blog上看到的文章,动了动手 *原文/keylife/iblog_show.asp?xid=4091* 1、外观 = 表头、隔行、网格 = procedureTForm1.DBGridDrawColumnCell_A(Sender:TObject;constRect:TRect; DataCol:Integer;Column:TColumn;State:TGridDrawState); vari:integer; begin ifgdSelectedinStatethenExit; /定义表头的字体和背景颜色: fori:=0to(SenderasTDBGrid).Columns.Count-1do begin (SenderasTDBGrid).Columnsi.Title.Font.Name:=宋体;/字体 (SenderasTDBGrid).Columnsi.Title.Font.Size:=9;/字体大小 (SenderasTDBGrid).Columnsi.Title.Font.Color:=$000000ff;/字体颜色(红色) (SenderasTDBGrid).Columnsi.Title.Color:=$0000ff00;/背景色(绿色) end; /隔行改变网格背景色: if(SenderasTDBGrid).DataSource.DataSet.RecNomod2=0then (SenderasTDBGrid).Canvas.Brush.Color:=clInfoBk/定义背景颜色 else (SenderasTDBGrid).Canvas.Brush.Color:=RGB(191,255,223);/定义背景颜色 /定义网格线的颜色: TDBGrid(sender).DefaultDrawColumnCell(Rect,DataCol,Column,State); with(SenderasTDBGrid).Canvasdo/画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; = 焦点单元变色 = procedureTForm1.DBGridDrawColumnCell_B(Sender:TObject;constRect:TRect; DataCol:Integer;Column:TColumn;State:TGridDrawState); begin if(State=gdSelected)or(State=gdSelected,gdFocused)then TDBGrid(sender).Canvas.Brush.color:=clRed;/当前行以红色显示,其它行使用背景的浅绿色 TDBGrid(sender).Canvas.pen.mode:=pmmask; TDBGrid(sender).DefaultDrawColumnCell(Rect,DataCol,Column,State); end; = 单元字体变色 = procedureTForm1.DBGridDrawColumnCell_C(Sender:TObject;constRect:TRect; DataCol:Integer;Column:TColumn;State:TGridDrawState); begin ifcopy(TDbgrid(sender).DataSource.DataSet.fieldbyname(column.Title.Caption).AsString,1,1)=Athen TDBGrid(sender).Canvas.Font.Color:=clRed else if(State=gdSelected,gdFocused)then TDBGrid(sender).Canvas.Font.Color:=clWhite else TDBGrid(sender).Canvas.Font.Color:=clBlack; TDBGrid(sender).DefaultDrawColumnCell(Rect,DataCol,Column,State); end; = 纵向斑马线 = procedureTForm1.DBGridDrawColumnCell_D(Sender:TObject;constRect:TRect; DataCol:Integer;Column:TColumn;State:TGridDrawState); begin CaseDataColMod2=0of True:DbGrid1.Canvas.Brush.Color:=clinfobk;/偶数列用蓝色 False:DbGrid1.Canvas.Brush.Color:=clMoneygreen;/奇数列用浅绿色 End; if(State=gdSelected,gdFocused)then TDBGrid(sender).Canvas.Font.Color:=clblue; TDBGrid(sender).Canvas.pen.mode:=pmmask; DbGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State); end; = 突出行显示 = procedureTForm1.DBGridDrawColumnCell_E(Sender:TObject;constRect:TRect; DataCol:Integer;Column:TColumn;State:TGridDrawState); begin Tdbgrid(sender).Color:=clAqua; Tdbgrid(sender).Options:=Tdbgrid(sender).Options+dgRowSelect; if(State=gdSelected)or(State=gdSelected,gdFocused)then DbGrid1.Canvas.Brush.color:=clRed;/当前行以红色显示,其它行使用背景的浅绿色 DbGrid1.Canvas.pen.mode:=pmmask; DbGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State); end; = 突出行列显示 = procedureTForm1.DBGridDrawColumnCell_F(Sender:TObject;constRect:TRect; DataCol:Integer;Column:TColumn;State:TGridDrawState); begin Tdbgrid(sender).Color:=clAqua; Tdbgrid(sender).Options:=Tdbgrid(sender).Options+dgRowSelect; if(State=gdSelected)or(State=gdSelected,gdFocused)then begin CaseDataColMod2=0of True:DbGrid1.Canvas.Brush.color:=clRed;/当前选中行的偶数列显示红色 False:DbGrid1.Canvas.Brush.color:=clblue;/当前选中行的奇数列显示蓝色 end; DbGrid1.Canvas.pen.mode:=pmmask; DbGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State); end; end; = 眼花缭乱_ = procedureTForm1.DBGridDrawColumnCell_G(Sender:TObject;constRect:TRect; DataCol:Integer;Column:TColumn;State:TGridDrawState); begin CaseTable1.RecNomod2=0of/根据数据集的记录号进行判断 True:DbGrid1.Canvas.Brush.color:=Clinfobk;/偶数行用浅绿色显示 False:DbGrid1.Canvas.Brush.color:=clmoneygreen;/奇数行用蓝色表示 end; If(State=gdSelected)or(State=gdSelected,gdFocused)then CaseDataColmod2=0of True:DbGrid1.Canvas.Brush.color:=clRed;/当前选中行的偶数列用红色 False:DbGrid1.Canvas.Brush.color:=clGreen;/当前选中行的奇数列用绿色表示 end; DbGrid1.Canvas.pen.mode:=pmMask; DbGrid1.DefaultDrawColumnCell(Rect,DataCol,Column,State); end; 图像 procedureTForm1.DBGridDrawColumnCell_H(Sender:TObject;constRect:TRect; DataCol:Integer;Column:TColumn;State:TGridDrawState); var Bmp:TBitmap; begin if(Column.Field.DataType=ftBLOB)or(Column.Field.DataType=ftGraphic)then begin Bmp:=TBitmap.Create; try Bmp.Assign(Column.Field); DBGrid1.Canvas.StretchDraw(Rect,Bmp); Bmp.Free; Except Bmp.Free; end; end; end; = 自动调整列宽 = functionDBGridRecordSize(mColumn:TColumn):Boolean; 返回记录数据网格列显示最大宽度是否成功 begin Result:=False; ifnotAssigned(mColumn.Field)thenExit; mColumn.Field.Tag:=Max(mColumn.Field.Tag, TDBGrid(mColumn.Grid).Canvas.TextWidth(mColumn.Field.DisplayText); Result:=True; end;DBGridRecordSize functionDBGridAutoSize(mDBGrid:TDBGrid;mOffset:Integer=5):Boolean; 返回数据网格自动适应宽度是否成功 var I:Integer; begin Result:=False; ifnotAssigned(mDBGrid)thenExit; ifnotAssigned(mDBGrid.DataSource)thenExit; ifnotAssigned(mDBGrid.DataSource.DataSet)thenExit; ifnotmDBGrid.DataSource.DataSet.ActivethenExit; forI:=0tomDBGrid.Columns.Count-1dobegin ifnotmDBGrid.ColumnsI.VisiblethenContinue; ifAssigned(mDBGrid.ColumnsI.Field)then mDBGrid.ColumnsI.Width:=Max(mDBGrid.ColumnsI.Field.Tag, mDBGrid.Canvas.TextWidth(mDBGrid.ColumnsI.Title.Caption)+mOffset elsemDBGrid.ColumnsI.Width:= mDBGrid.Canvas.TextWidth(mDBGrid.ColumnsI.Title.Caption)+mOffset; mDBGrid.Refresh; end; Result:=True; end;DBGridAutoSize /源代码结束 列宽 procedureTForm1.DBGridDrawColumnCell_I(Sender:TObject;constRect:TRect; DataCol:Integer;Column:TColumn;State:TGridDrawState); begin DBGridRecordSize(Column); end; 增加右键菜单 procedureTForm1.DBGridDrawColumnCell_J(Sender:TObject;constRect:TRect; DataCol:Integer;Column:TColumn;State:TGridDrawState); begin vCurRect:=Rect;/vCurRect在实现部分定义 end; procedureTForm1.DBGridMouseDown(Sender:TObject;Button:TMouseButton; Shift:TShiftState;X,Y:Integer); var CurPost:TPoint; begin GetCursorPos(CurPost);/获得鼠标当前坐标 if(y=17)and(x=vCurRect.Right)then begin ifbutton=mbrightthen begin PmTitle.Popup(CurPost.x,CurPost.y); end; end; end; 2、其他技巧 = 文字也可以托放 = procedureTForm1.DBGridDragOver(Sender,Source:TObject;X,Y:Integer; State:TDragState;varAccept:Boolean); begin accept:=true; end; procedureTForm1.DBGridDragDrop(Sender,Source:TObject;X,Y:Integer); begin ifSourceEdit1thenexit; withSenderasTDbGriddobegin Perform(wm_LButtonDown,0,MakeLong(x,y); PerForm(WM_LButtonUp,0,MakeLong(x,y); ifSelectedField.DataType=ftStringthen begin SelectedField.Dataset.edit; SelectedField.AsString:=Edit1.text; end; end; end; /指针控制 procedureTForm1.Button1Click(Sender:TObject); begin Button1.Enabled:=false; withDbgrid1.DataSource.DataSetdo try ifnotcheckbox1.CheckedthenDisableControls; first; whilenoteofdo begin sleep(50); application.ProcessMessages; button1.Caption:=inttostr(RecNo); next; end; first; finally ifnotcheckbox1.CheckedthenEnableControls; end; Button1.Enabled:=True; button1.Caption:=Go; end; /定制下拉框 procedureTForm1.Button2Click(Sender:TObject); vari:integer; begin fori:=0todbgrid1.Columns.Count-1do ifdbgrid1.Columnsi.FieldName=combobox1.Textthen begin dbgrid1.Columns1.PickList:=memo1.Lines; TDrawGrid(dbgrid1).col:=i; dbgrid1.SetFocus; end; end; Excel /导出到excel procedureTform1.ExportDBGrid(toExcel:Boolean); var bm:TBookmark; col,row:Integer; sline:String; mem:TMemo; ExcelApp:Variant; begin Screen.Cursor:=crHourglass; DBGrid1.DataSource.DataSet.DisableControls; bm:=DBGrid1.DataSource.DataSet.GetBookmark; DBGrid1.DataSource.DataSet.First; /createtheExcelobject iftoExcelthen begin ExcelApp:=CreateOleObject(Excel.Application); ExcelApp.WorkBooks.Add(xlWBatWorkSheet); ExcelApp.WorkBooks1.WorkSheets1.Name:=GridData; end; /Firstwesendthedatatoamemo /worksfasterthandoingitdirectlytoExcel mem:=TMemo.Create(Self); mem.Visible:=false; mem.Parent:=self; mem.Clear; sline:=; /addtheinfoforthecolumnnames forcol:=0toDBGrid1.FieldCount-1do sline:=sline+DBGrid1.Fieldscol.DisplayLabel+#9; mem.Lines.Add(sline); /getthedataintothememo forrow:=0toDBGrid1.DataSource.DataSet.RecordCount-1do begin sline:=; forcol:=0toDBGrid1.FieldCount-1do sline:=sline+DBGrid1.Fieldscol.AsString+#9; mem.Lines.Add(sline); DBGrid1.DataSource.DataSet.Next; end; /wecopythedatatotheclipboard mem.SelectAll; mem.CopyToClipboard; /ifneeded,sendittoExcel /ifnot,wealreadyhaveitintheclipboard iftoExcelthen begin ExcelApp.Workbooks1.WorkSheetsGridData.Paste; ExcelApp.Visible:=true; end; FreeAndNil(mem); /FreeAndNil(ExcelApp); DBGrid1.DataSource.DataSet.GotoBookmark(bm); DBGrid1.DataSource.DataSet.FreeBookmark(bm); DBGrid1.DataSource.DataSet.EnableControls; Screen.Cursor:=crDefault; end; procedureTForm1.N4Click(Sender:TObject); begin AboutBox.ShowModal; end; 功能描述:把DBGrid输出到Excel表格(支持多Sheet) 设计:CoolSlob 日期:2002-10-23 支持:CoolS 调用格式:CopyDbDataToExcel(DBGrid1,DBGrid2); procedureCopyDbDataToExcel(Args:arrayofconst); var iCount,jCount:Integer; XLApp:Variant; Sheet:Variant; I:Integer; begin Screen.Cursor:=crHourGlass; ifnotVarIsEmpty(XLApp)then begin XLApp.DisplayAlerts:=False; XLApp.Quit; VarClear(XLApp); end; try XLApp

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论