Delphi中DBGrid 应用全书.docx

上传人:b****6 文档编号:5996315 上传时间:2023-01-02 格式:DOCX 页数:19 大小:25.42KB
下载 相关 举报
Delphi中DBGrid 应用全书.docx_第1页
第1页 / 共19页
Delphi中DBGrid 应用全书.docx_第2页
第2页 / 共19页
Delphi中DBGrid 应用全书.docx_第3页
第3页 / 共19页
Delphi中DBGrid 应用全书.docx_第4页
第4页 / 共19页
Delphi中DBGrid 应用全书.docx_第5页
第5页 / 共19页
点击查看更多>>
下载资源
资源描述

Delphi中DBGrid 应用全书.docx

《Delphi中DBGrid 应用全书.docx》由会员分享,可在线阅读,更多相关《Delphi中DBGrid 应用全书.docx(19页珍藏版)》请在冰豆网上搜索。

Delphi中DBGrid 应用全书.docx

Delphi中DBGrid应用全书

Delphi中DBGrid应用全书

(一)

在Delphi语言的数据库编程中,DBGrid是显示数据的主要手段之一。

但是DBGrid缺省的外观未免显得单调和缺乏创意。

其实,我们完全可以在我们的程序中通过编程来达到美化DBGrid外观的目的。

通过编程,我们可以改变DBGrid的表头、网格、网格线的前景色和背景色,以及相关的字体的大小和风格。

  以下的示例程序演示了对DBGrid各属性的设置,使Delphi显示的表格就像网页中的表格一样漂亮美观。

  示例程序的运行:

  在Form1上放置DBGrid1、Query1、DataSource1三个数据库组件,设置相关的属性,使DBGrid1能显示表中的数据。

然后,在DBGrid1的onDrawColumnCell事件中键入以下代码,然后运行程序,就可以看到神奇的结果了。

本代码在Windows98、Delphi5.0环境下调试通过。

procedureTMainForm.DBGrid1DrawColumnCell(Sender:

TObject;

 constRect:

TRect;DataCol:

Integer;Column:

TColumn;State:

TGridDrawState);

vari:

integer;

begin

 ifgdSelectedinStatethenExit;

//定义表头的字体和背景颜色:

  fori:

=0to(SenderasTDBGrid).Columns.Count-1do

  begin

   (SenderasTDBGrid).Columns[i].Title.Font.Name:

='宋体';//字体

   (SenderasTDBGrid).Columns[i].Title.Font.Size:

=9;//字体大小

   (SenderasTDBGrid).Columns[i].Title.Font.Color:

=$000000ff;//字体颜色(红色)

   (SenderasTDBGrid).Columns[i].Title.Color:

=$0000ff00;//背景色(绿色)

  end;

//隔行改变网格背景色:

 ifQuery1.RecNomod2=0then

  (SenderasTDBGrid).Canvas.Brush.Color:

=clInfoBk//定义背景颜色

 else

  (SenderasTDBGrid).Canvas.Brush.Color:

=RGB(191,255,223);//定义背景颜色

//定义网格线的颜色:

  DBGrid1.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;

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

 

在Form1上放置DBGrid1、Query1、DataSource1三个数据库组件,设置相关的属性,使DBGrid1能显示表中的数据。

然后,在DBGrid1的onDrawColumnCell事件中键入以下代码,然后运行程序

procedureTForm1.DBGrid1DrawColumnCell(Sender:

TObject;constRect:

TRect;

 DataCol:

Integer;Column:

TColumn;State:

TGridDrawState);

vari:

integer;

begin

 ifgdSelectedinStatethenExit; //隔行改变网格背景色:

  ifadoQuery1.RecNomod2=0then

   (SenderasTDBGrid).Canvas.Brush.Color:

=clinfobk//定义背景颜色

 else

  (SenderasTDBGrid).Canvas.Brush.Color:

=RGB(191,255,223); //定义背景颜色

//定义网格线的颜色:

 DBGrid1.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:

=clbtnface;//定义画笔颜色(兰色)

  MoveTo(Rect.Right,Rect.Top);//画笔定位

  LineTo(Rect.Right,Rect.Bottom);//画绿色

 end;

end;

3.DBGrid指定列上显示DBComboBox

设置DBGrid1的OnDrawDataCell事件如下:

procedureTForm1.DBGrid1DrawDataCell(Sender:

TObject;constRect:

TRect;Field:

TField;State:

TGridDrawState);

begin

 if(gdFocusedinState)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,设置DBGrid1的OnColExit事件如下:

procedureTForm1.DBGrid1ColExit(Sender:

TObject);

begin

 IfDBGrid1.SelectedField.FieldName=DBComboBox1.DataFieldthen

  begin

   DBComboBox1.Visible:

=false;

  end;

end;

3)、当DBGrid指定列获得焦点时DrawDataCell事件只是绘制单元格,并显示DBComboBox,但是DBComboBox并没有获得焦点,数据的输入还是在单元格上进行。

在DBGrid1的KeyPress事件中调用SendMessage这个WindowsAPI函数将数据输入传输到DBComboBox上,从而达到在DBComboBox上进行数据输入。

因此还要设置KeyPress事件如下:

procedureTForm1.DBGrid1KeyPress(Sender:

TObject;varKey:

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内容----方法1

procedurePrintDbGrid(DataSet:

TDataSet;DbGrid:

TDbGrid;Title:

String);

var

PointX,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;

begin

PointX:

=Round(GetDeviceCaps(printer.Handle,LOGPIXELSX)/2.54);

PointY:

=Round(GetDeviceCaps(printer.Handle,LOGPIXELSY)/2.54);

 ScreenX:

=Round(Screen.PixelsPerInch/2.54);

ScaleX:

=PointX/ScreenX;

 RowPrinted:

=0;

 SumWidth:

=0;

 printer.BeginDoc;

 WithPrinter.Canvasdo

 begin

 DataSet.DisableControls;

  DataSet.First;

  THeight:

=Round(TextHeight('我')*1.5);//设定每行高度为字符高的1.5倍

  SpaceY:

=Round(TextHeight('我')/4);

  SpaceX:

=Round(TextWidth('我')/4);

  RowPerpage:

=Round((printer.PageHeight-5*PointY)/THeight);//上下边缘各2厘米

  ly:

=2*PointY;

  PageCount:

=0;

 whilenotDataSet.Eofdo

  begin

   if(RowPrinted=RowPerPage)or(RowPrinted=0)then

    begin

     ifRowPrinted<>0then

     Printer.NewPage;

      RowPrinted:

=0;

      PageCount:

=PageCount+1;

      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);

      ifRowPerPage*PageCount>DataSet.RecordCountthen

       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;

      ifRowCount=DataSet.RecordCountthen

       py2:

=py1+THeight*(RowCount-RowPerPage*(PageCount-1)+1)

      else

       py2:

=py1+THeight*(RowPerPage+1);

      SumWidth:

=lx;

      fori:

=0toDBGrid.Columns.Count-1do

      begin

      px1:

=SumWidth-SpaceX;

        px2:

=SumWidth;

        MoveTo(px1,py1);

        LineTo(px2,py2);

        TitleWidth:

=TextWidth(DBGrid.Columns[i].Title.Caption);

        lx:

=Round(SumWidth+(DBGrid.Columns[i].width*scaleX-titleWidth)/2);

        TextOut(lx,ly,DBGrid.Columns[i].Title.Caption);

        SumWidth:

=Round(SumWidth+DBGrid.Columns[i].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);

      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);

    fori:

=0toDBGrid.Columns.Count-1do

    begin

      TextOut(lx,ly,DataSet.FieldByname(DBGrid.Columns[i].Fieldname).AsString);

      lx:

=Round(lx+DBGrid.Columns[i].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]);

}

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:

=CreateOleObject(‘Excel.Application‘);

 except

   Screen.Cursor:

=crDefault;

   Exit;

 end;

 XLApp.WorkBooks.Add;

 XLApp.SheetsInNewWorkbook:

=High(Args)+1;

 forI:

=Low(Args)toHigh(Args)do

 begin

   XLApp.WorkBooks[1].WorkSheets[I+1].Name:

=TDBGrid(Args[I].VObject).Name;

   Sheet:

=XLApp.Workbooks[1].WorkSheets[TDBGrid(Args[I].VObject).Name];

   ifnotTDBGrid(Args[I].VObject).DataSource.DataSet.Activethen

   begin

     Screen.Cursor:

=crDefault;

     Exit;

   end;

   TDBGrid(Args[I].VObject).DataSource.DataSet.first;

   foriCount:

=0toTDBGrid(Args[I].VObject).Columns.Count-1do

     Sheet.Cells[1,iCount+1]:

=

   TDBGrid(Args[I].VObject).Columns.Items[iCount].Title.Caption;

   jCount:

=1;

   whilenotTDBGrid(Args[I].VObject).DataSource.DataSet.Eofdo

   begin

     foriCount:

=0toTDBGrid(Args[I].VObject).Columns.Count-1do

       Sheet.Cells[jCount+1,iCount+1]:

=

     TDBGrid(Args[I].VObject).Columns.Items[iCount].Field.AsString;

     Inc(jCount);

     TDBGrid(Args[I].VObject).DataSource.DataSet.Next;

   end;

   XlApp.Visible:

=True;

 end;

 Screen.Cursor:

=crDefault;

end;

dbgrid的數據導入到excel中---方法二

unitUnit1;

interface

uses

 Windows,Messages,SysUtils,Classes,Graphics,Controls,Forms,Dialogs,

 StdCtrls,Grids,DBGrids,Db,DBTables,Excel97,OleServer,Word97;

type

 TForm1=class(TForm)

   ExcelApplication1:

TExcelApplication;

   ExcelWorkbook1:

TExcelWorkbook;

   ExcelWorksheet1:

TExcelWorksheet;

   Table1:

TTable;

   DataSource1:

TDataSource;

   DBGrid1:

TDBGrid;

   Button1:

TButton;

   Button4:

TButton;

   WordApplication1:

TWordApplication;

   WordDocument1:

TWordDocument;

   procedureButton1Click(Sender:

TObject);

   procedureButton4Click(Sender:

TObject);

 private

   {Privatedeclarations}

 public

   {Publicdeclarations}

 end;

var

 Form1:

TForm1;

implementation

{$R*.DFM}

procedureTForm1.Button1Click(Sender:

TObject);

var

 i,row,column:

integer;

begin

 Try

   ExcelApplication1.Connect;

 Except

   MessageDlg('Excelmaynotbeinstalled',

     mtError,[mbOk],0);

   Abort;

 End;

 ExcelAp

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 自然科学

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

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