【Delphi】在DBGridEh的CheckBox列中根据条件禁用部分CheckBox(显示为灰色)的示例

                        

unit Unit1;

interface
uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  DBGridEhGrouping, ToolCtrlsEh, DBGridEhToolCtrls, DynVarsEh, EhLibVCL,
  GridsEh, DBAxisGridsEh, DBGridEh, Data.DB, DBAccess, MSAccess, MemDS;

type

  TDBGridEh = Class(DBGridEh.TDBGridEh)
  public
    function  ColumnByName(Name: String): TColumnEh;
    procedure DrawGrayedCheckBox(const Rect: TRect; DataCol: Integer;
                                    Column: TColumnEh; State: TGridDrawState);
  end;

  TForm1 = class(TForm)
    Connection1: TMSConnection; //数据库组件为sdac
    Query1: TMSQuery;
    DBGridEh1: TDBGridEh;
    DataSource1: TDataSource;
    procedure FormCreate(Sender: TObject);
    procedure DBGridEh1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumnEh; State: TGridDrawState);
    procedure DBGridEh1CellClick(Column: TColumnEh);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{ SQL测试脚本:
  use msdb
  go

  DROP TABLE test
  CREATE TABLE test (选取 BIT, 数量 INTEGER)
  INSERT INTO test VALUES (0, 200), (1, 50), (null, 3), (1, 80), (1, 300)
  SELECT * FROM test
}

function TDBGridEh.ColumnByName(Name: String): TColumnEh;
var
  I: Integer;
begin
  Result := nil;
  for I := 0 to Columns.Count-1 do
  begin
    if Columns[I].Title.Caption = Name then
    begin
      Result := Columns[I];
      break;
    end;
  end;
end;


procedure TDBGridEh.DrawGrayedCheckBox(const Rect: TRect;
            DataCol: Integer; Column: TColumnEh; State: TGridDrawState);
var
  ARect, ARect1: TRect;
begin
  if Column.GetBarType = ctCheckboxes then
  begin
    ARect := Rect;
    Canvas.FillRect(ARect);
    ARect1.Left := ARect.Left + iif(ARect.Right - ARect.Left < DefaultCheckBoxWidth, 0,
      (ARect.Right - ARect.Left) shr 1 - DefaultCheckBoxWidth shr 1);
    ARect1.Right := iif(ARect.Right - ARect.Left < DefaultCheckBoxWidth, ARect.Right,
      ARect1.Left + DefaultCheckBoxWidth);
    ARect1.Top := ARect.Top + iif(ARect.Bottom - ARect.Top < DefaultCheckBoxHeight, 0,
      (ARect.Bottom - ARect.Top) shr 1 - DefaultCheckBoxHeight shr 1);
    ARect1.Bottom := iif(ARect.Bottom - ARect.Top < DefaultCheckBoxHeight, ARect.Bottom,
      ARect1.Top + DefaultCheckBoxHeight);


    PaintButtonControl(Canvas, ARect1, clGray, bcsCheckboxEh,
                                  0, Flat, False, False, Column.CheckboxState);
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  DBGridEh1.OnCellClick := DBGridEh1CellClick;
  DBGridEh1.OnDrawColumnCell := DBGridEh1DrawColumnCell;

  DBGridEh1.Columns.Clear;
  with DBGridEh1.Columns.Add do
  begin
    Title.Caption := '选取';
    FieldName     := '选取';
    Checkboxes    := True;
    ReadOnly      := True;
  end;
  with DBGridEh1.Columns.Add do
  begin
    Title.Caption := '数量';
    FieldName     := '数量';
  end;

  with Connection1 do
  begin
    LoginPrompt := False;
    Server   := 'LocalHost';
    Username := 'sa';
    Password := '你的密码';
    port     := 1433;
  end;

  DataSource1.DataSet  := Query1;
  DBGridEh1.DataSource := DataSource1;

  with Query1 do
  begin
    Connection := Connection1;
    SQL.Text  := 'select * from msdb..test';
    Open;
  end;
end;


procedure TForm1.DBGridEh1CellClick(Column: TColumnEh);
var
  DataSet: TDataSet;
begin
  DataSet := DBGridEh1.DataSource.DataSet;
  if (Column.Title.Caption = '选取') and
       (DBGridEh1.ColumnByName('数量').Field.Value >= 100) then
  begin           //数量少于100不允许修改选择
    DataSet.Edit;
    Column.Field.Value := not Column.Field.AsBoolean;
    DataSet.Post;
  end;
end;


procedure TForm1.DBGridEh1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumnEh; State: TGridDrawState);
begin
  if (Column.Title.Caption = '选取') and  //数量少于100记录的选择框显示为灰色
       (DBGridEh1.ColumnByName('数量').Field.Value < 100) then
    DBGridEh1.DrawGrayedCheckBox(Rect, DataCol, Column, State);
end;
 

end.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值