線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:3721
推到 Plurk!
推到 Facebook!

如何在DBGrid中選取某一欄改變整行的顏色(dgRowSelect 不能為True)

尚未結案
shinjie
資深會員


發表:12
回覆:275
積分:287
註冊:2003-03-19

發送簡訊給我
#1 引用回覆 回覆 發表時間:2005-04-26 16:42:59 IP:203.73.xxx.xxx 未訂閱
請教各位大大一個問題: 因資料來源為BDE故無法使用DataSource1.DataSet.RecNo 因程式功能需求,dgRowSelect不能設為True 問題:要在DBGRID中,選擇某一欄時(if gdselected in state)要改變整行的 顏色。 謝謝各位的幫忙。 ========================================= 程式有問題並不可怕,最怕是不知問題出在那 不知問題在那不可怕,更怕是不知如何問問題 寫程式要從最簡單的開始
------
我將在茫茫人海中尋訪我唯一之靈魂伴侶。
得之;我幸。不得;我命。
jimmy_wei
高階會員


發表:9
回覆:176
積分:147
註冊:2003-08-28

發送簡訊給我
#2 引用回覆 回覆 發表時間:2005-04-26 17:07:36 IP:210.68.xxx.xxx 未訂閱
少打了一行.....會錯....已修正存到下一篇了... 發表人 - jimmy_wei 於 2005/04/26 17:34:14
jimmy_wei
高階會員


發表:9
回覆:176
積分:147
註冊:2003-08-28

發送簡訊給我
#3 引用回覆 回覆 發表時間:2005-04-26 17:30:06 IP:210.68.xxx.xxx 未訂閱
shinjie 你好:    
    procedure TFORM.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  oGrid : TDBGrid;
  oDataSet : TDataSet;
  sFieldName :string;
begin
  inherited;
  oGrid := Sender as TDBGrid;
  oDataSet := (Sender as TDBGrid).DataSource.DataSet ;      if not oDataSet.Active then Exit;
  if oDataSet.RecordCount = 0 then Exit;      if gdselected in state
    oGrid.Canvas.Brush.Color:=RGB(200, 100, 100);      oGrid.DefaultDrawColumnCell(Rect, DataCol, Column, State);    end;    
我又看了一下....我好像答非所問耶....傷腦筋 發表人 - jimmy_wei 於 2005/04/26 19:35:11
wameng
版主


發表:31
回覆:1336
積分:1188
註冊:2004-09-16

發送簡訊給我
#4 引用回覆 回覆 發表時間:2005-04-26 22:49:57 IP:219.86.xxx.xxx 未訂閱
簡單一點將 Options 中 dtMultiSelect := True; dtAlwaysShowEditor := False;    或者
定義 Form1私有變數 RowID     procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  I :Integer;
  ARect: TRect;
begin
  if not ADOQuery1.Active then Exit;      With TStringGrid(DBGrid1),DBGrid1 do
    if gdSelected in state then
    begin
      RowID := Row;
      For I:=LeftCol to LeftCol VisibleColCount do
        if I<>(DataCol 1) then
        begin
          ARect := CellRect(I,Row);
          Windows.InvalidateRect(Handle, @ARect, True);
        end;
    end else if RowID=Row then
    begin
      ARect := CellRect(DataCol 1,Row);
      ARect.Right :=ARect.Left ColWidths[DataCol 1];          Canvas.Brush.Color := ClHighLight;
      Canvas.Font.Color := ClHighLightText;
      DefaultDrawColumnCell(ARect,DataCol,Columns[DataCol],[gdFocused]);
    end;
end;
仍有些瑕疵,如移動滑鼠滾輪[:]可能就會有殘影現象。 要解決此問題,可能要攔截 WM_MOUSEWHEEL 訊息。 自行補強一下吧。 參考。
shinjie
資深會員


發表:12
回覆:275
積分:287
註冊:2003-03-19

發送簡訊給我
#5 引用回覆 回覆 發表時間:2005-04-27 13:37:17 IP:203.73.xxx.xxx 未訂閱
謝謝wameng大大的回答,我原本也是照這種方式寫, 但是一直控制不好,試了很久還是找不到更好的解法。 也謝謝jimmy_wei大大的回答,但因wameng大大的回答 比較接近,所以分數給wameng大大了。 ========================================= 程式有問題並不可怕,最怕是不知問題出在那 不知問題在那不可怕,更怕是不知如何問問題 寫程式要從最簡單的開始
------
我將在茫茫人海中尋訪我唯一之靈魂伴侶。
得之;我幸。不得;我命。
系統時間:2024-06-24 21:02:47
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!