如何在cxgrid中添加checkbox
发布网友
发布时间:2023-04-12 01:00
我来回答
共1个回答
热心网友
时间:2023-09-29 02:51
1,添加单元文件cxCheckBox,窗体添加clientDataset,datasource,adoquery,cxgrid
2,添加类声明
type
TCheckBoxClickEvent=procere(Sender: TObject) of object;
type
TCheckBoxClick = class(TObject)
private
FOnCheckBoxClick:TCheckBoxClickEvent; //定义一个内部事件,private里的只能在类内部调用
public
property View_UpCheckBoxColumnPropertiesChange:TCheckBoxClickEvent read FOnCheckBoxClick write FOnCheckBoxClick; //定义一个外部的事件
end;
在public中,添加方法
procere View_UpCheckBoxColumnPropertiesChange(Sender: TObject);
3方法实现,根据checkbox状态设置语句
procere Tfrm_w_checker.View_UpCheckBoxColumnPropertiesChange(Sender: TObject);
begin
////////////////////////////////////////////////////
if dw_checker2.Focused = true then
if (Sender as TcxCheckBox).checked then
begin
//dw_add_job.Controller.FocusedColumn.Tag := 1;
dw_checker2.ViewData.Rows[dw_checker2.Controller.FocusedRowIndex].Values[0]:= true;
checker := '1';
doctor := trim(dw_checker2.ViewData.Rows[dw_checker2.Controller.FocusedRowIndex].Values[4]);
end
else
begin
// dw_add_job.Controller.FocusedColumn.Tag := 0;
dw_checker2.ViewData.Rows[dw_checker2.Controller.FocusedRowIndex].Values[0]:= false;
checker := '0';
doctor := '';
end;
end;
4.formshow中生成cxgrid如下
str_sql := 'SELECT tblxt_role.role_name,tblxt_user.user_name,tblzy_adtward.ward_name,tblxt_user.user_code,';
str_sql := str_sql + 'tblxt_user.ward_code,tblxt_user.role_code ';
str_sql := str_sql + ' FROM tblxt_role,tblxt_user,tblzy_adtward ';
str_sql := str_sql + ' WHERE ( tblxt_role.role_code = tblxt_user.role_code ) and ';
str_sql := str_sql + ' ( tblxt_user.ward_code = tblzy_adtward.ward_code ) and ';
str_sql := str_sql + ' (tblxt_user.delete_flag = 0 OR tblxt_user.delete_flag = null ) AND ';
str_sql := str_sql + ' tblxt_user.check_flag= 1 and tblxt_user.ward_code = :ward_code AND ';
str_sql := str_sql + ' tblxt_role.subsys = ''ys'' AND tblxt_role.role_name <> ''*'' ORDER BY tblxt_role.role_code ASC';
with qry_dw_checker2 do
begin
close;
sql.Clear;
sql.Add(str_sql);
Parameters.ParamByName('ward_code').Value := gv_ward_code;
open;
end;
ClientDataSet2.FieldDefs.Clear;
for i:=0 to qry_dw_checker2.FieldCount-1 do
begin
with ClientDataSet2.FieldDefs.AddFieldDef do
begin
Name:=qry_dw_checker2.Fields[i].DisplayName;
if qry_dw_checker1.Fields.Fields[i].DataType=ftAutoInc then
DataType:=ftInteger
else if qry_dw_checker1.Fields.Fields[i].DataType=ftWideString then
DataType:=ftString
else
DataType :=qry_dw_checker2.Fields.Fields[i].DataType;//取原数据字段数据类型
Size:=qry_dw_checker2.Fields.Fields[i].Size;
end;
end;
ClientDataSet2.CreateDataSet;
ds_checker2.DataSet := qry_dw_checker2;
dw_checker2.DataController.DataSource := ds_checker2;
dw_checker2.ClearItems;
dw_checker2.CreateColumn;//建立一个没绑定的列
dw_checker2.Columns[0].Caption:='选择';
dw_checker2.DataController.CreateAllItems;//建立所有绑定的列
// dw_checker1.Columns[0].DataBinding.FieldName := 'flag';
dw_checker2.Columns[0].Width:=45;
//下列5行语句是为了让没绑定列成为 CheckBox :
dw_checker2.DataController.KeyFieldNames:='user_code';
dw_checker2.DataController.MasterKeyFieldNames := 'user_code';
dw_checker2.DataController.DetailKeyFieldNames := 'user_code';
dw_checker2.DataController.DataModeController.SmartRefresh:=true;
dw_checker2.Columns[0].DataBinding.ValueType:='Boolean';
dw_checker2.Columns[0].PropertiesClass:= TcxCheckBoxProperties;
(dw_checker2.Columns[0].Properties as TcxCheckBoxProperties).NullStyle:=nssUnchecked;
//由于CheckBox列是动态列,所以需要给其关联一个OnChange的事件:
(dw_checker2.Columns[0].Properties as TcxCheckBoxProperties).OnChange:=View_UpCheckBoxColumnPropertiesChange;//关联事件
dw_checker2.OptionsView.Indicator:=true;
dw_checker2.OptionsView.NoDataToDisplayInfoText := '';
5.定义单击事件
a.令checkbox单选
procere Tfrm_w_checker.dw_checker2CellClick(Sender: TcxCustomGridTableView;
ACellViewInfo: TcxGridTableDataCellViewInfo; AButton: TMouseButton;
AShift: TShiftState; var AHandled: Boolean);
var
Row: Integer;
begin
inherited;
//单选
for Row:=0 to dw_checker2.DataController.RecordCount-1 do
begin
dw_checker2.ViewData.Records[Row].Values[0] := False;
end;
end;
b.令checkbox多选
procere Tfrm_w_add_job.dw_add_jobMouseUp(Sender: TObject;
Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
Row: Integer;
begin
inherited;
//多选
if dw_add_job.DataController.RecordCount<>0 then
begin
Row := dw_add_job.DataController.FocusedRecordIndex;
if dw_add_job.ViewData.Records[Row].Values[1] = True then
dw_add_job.ViewData.Records[Row].Values[1] := False
else
dw_add_job.ViewData.Records[Row].Values[1] := True;
end;
end;