zoukankan      html  css  js  c++  java
  • 如何在DBGrid的每一行前加一个单选框?

    Unit Unit1;

    Interface

    Uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, Grids, DBGrids, DB, ADODB;

    Type
      TForm1 = Class(TForm)
        DataSource1: TDataSource;
        ADOTable1: TADOTable;
        DBGrid1: TDBGrid;
        Procedure DBGrid1DrawColumnCell(Sender: TObject; Const Rect: TRect;
          DataCol: Integer; Column: TColumn; State: TGridDrawState);
        Procedure DBGrid1ColEnter(Sender: TObject);
        Procedure DBGrid1ColExit(Sender: TObject);
        Procedure DBGrid1CellClick(Column: TColumn);
        Procedure DBGrid1KeyDown(Sender: TObject; Var Key: Word;
          Shift: TShiftState);
      private
        OriginalOptions: TDBGridOptions;
        Procedure SaveBoolean;
        { Private declarations }
      public
        { Public declarations }
      End;

    Var
      Form1: TForm1;

    Implementation

    {$R *.dfm}

    Procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; Const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    Const // 这个整数值将按照布尔值返回,并送入数组
      CtrlState: Array[Boolean] Of Integer = (DFCS_BUTTONCHECK, DFCS_BUTTONCHECK Or DFCS_CHECKED);
     // CtrlString:=string;
    Begin
    //确保只有在逻辑字段才能插入组件
      If Column.Field.DataType = ftBoolean Then
      Begin
        DBGrid1.Canvas.FillRect(Rect);
        DrawFrameControl(DBGrid1.Canvas.Handle, Rect, DFC_BUTTON, CtrlState[Column.Field.AsBoolean]); //画框框
       // Column.Field.Text:='google';

      End;
    End;

    Procedure TForm1.DBGrid1ColEnter(Sender: TObject);
    Begin
    // 确保该栏是逻辑字段
      If DBGrid1.SelectedField.DataType = ftBoolean Then
      Begin
        OriginalOptions := DBGrid1.Options;
        DBGrid1.Options := DBGrid1.Options - [dgEditing];
      End;
    End;

    Procedure TForm1.DBGrid1ColExit(Sender: TObject);
    Begin
    //确保该栏是逻辑字段
      If DBGrid1.SelectedField.DataType = ftBoolean Then
        DBGrid1.Options := OriginalOptions;
    End;

    Procedure TForm1.DBGrid1CellClick(Column: TColumn);
    Begin
    //确保该栏是逻辑字段
      If DBGrid1.SelectedField.DataType = ftBoolean Then
        SaveBoolean();
    End;

    Procedure TForm1.DBGrid1KeyDown(Sender: TObject; Var Key: Word;
      Shift: TShiftState);
    Begin
    //确保该栏是逻辑字段和空格键在键盘中被敲击
      If (Key = VK_SPACE) And (DBGrid1.SelectedField.DataType = ftBoolean) Then
        SaveBoolean();
    End;

    Procedure TForm1.SaveBoolean;
    Begin
      DBGrid1.SelectedField.Dataset.Edit;
      DBGrid1.SelectedField.AsBoolean := Not DBGrid1.SelectedField.AsBoolean;
      DBGrid1.SelectedField.Dataset.Post;
    End;
    End.

  • 相关阅读:
    IronRuby:元编程特性【method_missing】的使用
    DNN(DotNetNuke) 3.0感官刺激零距x接触!!! :)
    (MS SQL)如何实现相关文章功能(多关键字匹配)改进版
    谁有微软认证,如MCSD,MCDBA,MCXX等等,马上告诉我
    开源代码2004/12/25 codeproject
    开源代码2004/1220-PDF格式/文件相关
    强烈推荐一个超酷的跨平台、支持多数据库的数据库管理工具
    (MS SQL)如何实现相关文章功能(多关键字匹配)
    DotNetNuke(DNN)从入门到进阶(1)-怎样写自己的模块
    推荐开源代码2004/12/17
  • 原文地址:https://www.cnblogs.com/fengju/p/6173866.html
Copyright © 2011-2022 走看看