在Indicator中添加动态Checkbox
先做设置
DBGrideh属性设置:
IndicatorOptions =
[gioShowRowIndicatorEh, //小三角指示
gioShowRecNoEh, //数据源行号
gioShowRowselCheckboxesEh] //显示CheckBox
Options = [……, dgMultiSelect] //开启多选,才能对CheckBox进行编辑
以上设置完成,功能就有了,对于选中的行进行遍历读取
1
2
3
4
5
|
for I := 0 to DBGrideh . SelectedRows . Count - 1 do begin DBGrideh . DataSource . DataSet . Bookmark := DBGrideh . SelectedRows[I]; //定位 …… //读取定位后数据源其他操作 end ; |
补充全选及反选功能
DBGrideh.Selection.Rows.SelectAll; //全选,此时 SelectionType=gstRecordBookmarks
//下面会提到一个AllowedSelections,结合下文,这里不能用DBGrideh.Selection.SelectAll, 它的SelectionType=gstAll
DBGrideh.Selection.Clear; //全部取消
在以上基础上做一个全选功能升级。
默认DBGrideh的设置中有如下设置
AllowedSelections = [gstRecordBookmarks,gstRectangle,gstColumns,gstAll]
此时,鼠标点击DBGrideh左上角IndicatorTitle可以触发全选事件,不过却无法对全选的数据记录进行利用,查找了下DBGrideh.pas,发现了一段关键的代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
procedure TCustomDBGridEh . DefaultIndicatorTitleMouseDown(Cell: TGridCoord; Button: TMouseButton; Shift: TShiftState; X, Y: Integer ); var DropdownMenu: TPopupMenu; P: TPoint; ARect: TRect; begin ...... end else if (dgMultiSelect in Options) and DataLink . Active and ([gstRecordBookmarks, gstAll] * AllowedSelections <> []) then begin if Selection . SelectionType <> gstNon then Selection . Clear else if gstAll in AllowedSelections then Selection . SelectAll else if gstRecordBookmarks in AllowedSelections then Selection . Rows . SelectAll; end ; end ; |
DBGrideh是通过Bookmarks定位数据源游标行的,在此,默认设置AllowedSelections中[gstAll,gstRecordBookmarks],当触发IndicatorTitle鼠标点击事件时,发现上一段代码运行是先检查gstAll,然后检查gstRecordBookmarks,所以虽然全选了,但是无法定位数据源游标,所以只要在AllowedSelections中去掉[gstAll]即可
-----------------------------------
回复中有人提出通过此种方法,点击DBGrideh任意地方,所选行容易消失
解决方法是设置DBGrideh中OptionsEh,使dghClearSelection=false
设置如下
OptionsEh = [..., dghClearSelection, ...]//,此内dghClearSelection去掉
分类:
EhGRID
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?