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.