在Indicator中添加動態Checkbox,無需綁定數據源,支持全選 - Ehlib學習(二)


先做設置

DBGrideh屬性設置:

IndicatorOptions =

[gioShowRowIndicatorEh, //小三角指示

gioShowRecNoEh,    //數據源行號

gioShowRowselCheckboxesEh]  //顯示CheckBox

 

Options = [……, dgMultiSelect]  //開啟多選,才能對CheckBox進行編輯

以上設置完成,功能就有了,對於選中的行進行遍歷讀取

  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,發現了一段關鍵的代碼

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去掉
    

 

   


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM