• DBGrid应用


    在Delphi语言数据库编程中,DBGrid是显示数据的常用控件之一,下面来介绍一些DBGrid的用法。

    1. 获取DBGrid选中单元格的一些信息

    procedure TForm2.DBGrid1CellClick(Column: TColumn);
    var
        strValue:string;
        iCol,iRow:Integer;
        rRect:TRect;
    begin
        strValue := DBGrid1.SelectedField.Value;        //获取选中单元格的值
        iCol := TDrawGrid(DBGrid1).Col;                 //获取选择单元格的列,第一列为1
        iRow := TDrawGrid(DBGrid1).Row;                 //获取选择单元格的行,第一行为1
        rRect := TDrawGrid(DBGrid1).CellRect(iCol,iRow); //获取选择单元格的矩形框
        //ShowMessage('行:'+IntToStr(iRow)+#13'列:'+IntToStr(iCol)+#13'值:'+strValue);
    end;

    2. 设置列名格式,一般列名只需设置一次(这里一次性绘制所有的列,以避免列数过多时,显示滚动条后的列时再绘制时影响一些正常的操作,譬如按键盘方向键定位单元格时可能会光标移到第一行),即可用全局变量进行记录下。

    procedure TForm2.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    var
        i:Integer;
    begin
        if bDrawTitle=False then    //一般情况画一次就够了,不必重复画
        begin
            for i:=0 to DBGrid1.Columns.Count-1 do  //一下子绘制所有的列
            begin
                if i mod 2 = 0 then
                begin
                    DBGrid1.Columns[i].Title.Font.Color := clRed;   //列名 字体颜色
                    DBGrid1.Columns[i].Title.Color := clGreen;      //列名 背景
                    DBGrid1.Columns[i].Color := clBlue;             //列数据 背景
                end;
            end;
            bDrawTitle := True;
        end;
    
        //将数据背景斑马线显示
        if CDS1.RecNo mod 2 = 0 then
        begin
            (Sender as TDBGrid).Canvas.Brush.Color := clGray;
        end;
        (Sender as TDBGrid).DefaultDrawColumnCell(Rect,DataCol,Column,State);
    end;

    3. 设置Grid单元格边框

    procedure TForm2.DBGrid1CellClick(Column: TColumn);
    var
        iCol,iRow:Integer;
        rRect:TRect;
    begin
        iCol := TDrawGrid(DBGrid1).Col;                 //获取选择单元格的列,第一列为1
        iRow := TDrawGrid(DBGrid1).Row;                 //获取选择单元格的行,第一行为1
        rRect := TDrawGrid(DBGrid1).CellRect(iCol,iRow); //获取选择单元格的矩形框
    
        with DBGrid1.Canvas do
        begin
            Pen.Width := 4;         //设置画笔 宽带
            Pen.Color := clRed;     //设置画笔 颜色
            MoveTo(rRect.Left,rRect.Top);   //设置画笔 起点
            LineTo(rRect.Right,rRect.Top);  //画线
            Pen.Width := 2;
            Pen.Color := clYellow;
            MoveTo(rRect.Right,rRect.Top);
            LineTo(rRect.Right,rRect.Bottom);
        end;
    end;
  • 相关阅读:
    Google's Machine Learning Crash Course #01# Introducing ML & Framing & Fundamental terminology
    MySQL Crash Course #09# Chapter 17. Combining Queries: UNION
    MySQL笔记(二)数据库对象的创建和管理
    浅谈TCP/IP网络编程中socket的行为
    linux网络编程中的shutdown()与close()函数
    c++11中的线程、锁和条件变量
    多线程TcpServer
    TCP网络库:Acceptor、TcpServer、TcpConnection
    epoll 的accept , read, write
    线程安全函数
  • 原文地址:https://www.cnblogs.com/liuke1987/p/2907892.html
Copyright © 2020-2023  润新知