如何在cxgrid中添加checkbox

如何在cxgrid中添加checkbox,第1张

1,添加单元文件cxCheckBox,窗体添加clientDataset,datasource,adoquery,cxgrid

2,添加类声明

type

TCheckBoxClickEvent=procedure(Sender: TObject) of object

type

TCheckBoxClick = class(TObject)

private

FOnCheckBoxClick:TCheckBoxClickEvent//定义一个内部事件,private里的只能在类内部调用

public

property View_UpCheckBoxColumnPropertiesChange:TCheckBoxClickEvent read FOnCheckBoxClick write FOnCheckBoxClick//定义一个外部的事件

end

在public中,添加方法

procedure View_UpCheckBoxColumnPropertiesChange(Sender: TObject)

3方法实现,根据checkbox状态设置语句

procedure 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单选

procedure Tfrm_w_checker.dw_checker2CellClick(Sender: TcxCustomGridTableView

ACellViewInfo: TcxGridTableDataCellViewInfoAButton: TMouseButton

AShift: TShiftStatevar 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多选

procedure Tfrm_w_add_job.dw_add_jobMouseUp(Sender: TObject

Button: TMouseButtonShift: TShiftStateX, 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

需要使用Ext.grid.CheckColumn 或者使用渲染器生成

var checkColumn_chg_page = new Ext.grid.CheckColumn({  

header: '换页',

dataIndex: 'chang_page',

width:40

})

columns: [{  text: '状态',

        sortable: false,

        dataIndex: 'state',//数据源中的状态列

        renderer: function (v) { return '<input type="checkbox"'+(v=="1"?" checked":"")+'/>' }//根据值返回checkbox是否勾选

        },

        checkColumn_chg_page

       ]

在DXperience中的gridcontrol添加checkbox复选框,需要进行下面的 *** 作步骤:

添加一列,FieldName为 "check",将ColumnEdit 设置为 复选框 样式。

将要绑定的DataTable添加列 "check",Type 为 bool。 必须要设置为bool,如果类型错误的话会出现点选无效的情况。

绑定DataTable到GridControl。

获取:

string value = gridview.GetDataRow(i)["check"].toString()

value == "true" || "" ("false")

设置为多选

gridView1 .OptionsSelection.MultiSelect = true

gridView1.OptionsSelection.MultiSelectMode = DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.Ro


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/bake/11473404.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存