全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1794
推到 Plurk!
推到 Facebook!

grid 顯示 Memo 欄位還是不太懂

尚未結案
carter6
初階會員


發表:53
回覆:108
積分:42
註冊:2002-12-16

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-04-15 15:11:15 IP:61.216.xxx.xxx 未訂閱
不知有哪為高手能說明這段 因為我直接加進去不能用 procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect; Field: TField; State: TGridDrawState); var P : array [0..50] of char; {array size is number of characters needed} bs : TBlobStream; {from the memo field} hStr : String; begin if Field is TMemoField then begin with (Sender as TDBGrid).Canvas do begin {Table1Notes is the TMemoField} bs := TBlobStream.Create(Table1Notes, bmRead); FillChar(P,SizeOf(P),#0); {terminate the null string} bs.Read(P, 50); {read 50 chars from memo into blobStream} bs.Free; hStr := StrPas(P); while Pos(#13, hStr) > 0 do {remove carriage returns and} hStr[Pos(#13, hStr)] := ' '; while Pos(#10, hStr) > 0 do {line feeds} S[Pos(#10, hStr)] := ' '; FillRect(Rect); {clear the cell} TextOut(Rect.Left, Rect.Top, hStr); {fill cell with memo data} end; end; end; 謝謝!! Delphi 初學者
ddy
站務副站長


發表:262
回覆:2105
積分:1169
註冊:2002-07-13

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-04-15 16:54:23 IP:61.218.xxx.xxx 未訂閱
請參考此篇 關於DBGrid顯示memo型態資料問題 http://delphi.ktop.com.tw/topic.php?topic_id=21008 ccchen 版主已有詳細回答    小弟整理如下,紅色部份,ccchen 已於上面link 裡有提到,你不能執行的原因大概在此,修改為紅色部份即可執行,我已測試過    
procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
  Field: TField; State: TGridDrawState);
var
    P: array[0..50] of char; {array size is number of characters needed}
    bs: TStream; {from the memo field}
    hStr: string;
begin
    if Field is TMemoField then //若欄位屬TMemoField則處理
    begin
        with (Sender as TDBGrid).Canvas do
        begin 
            //由資料庫欄位裡讀出資料至TStream
            bs :=ClientDataSet1.CreateBlobStream(Field, bmRead);
            FillChar(P, SizeOf(P), #0); {terminate the null string}
            bs.Read(P, 50); {read 50 chars from memo into blobStream}
            bs.Free;
            hStr := StrPas(P); //PChar -->String
            //處理#13#10 讓多行文字取代為空白
            while Pos(#13, hStr) > 0 do {remove carriage returns and}
                hStr[Pos(#13, hStr)] := ' ';
            while Pos(#10, hStr) > 0 do {line feeds}
                hStr[Pos(#10, hStr)] := ' ';
            FillRect(Rect); {clear the cell}
            TextOut(Rect.Left, Rect.Top, hStr);//畫出文字 
        end;
    end;
end;    
PS:上面 bs :=ClientDataSet1.CreateBlobStream(Field, bmRead); 的ClientDataSet1 可以更換,不一定是ClientDataSet 也可以是ADODataSet、ADOTable.....等 視你所用的元件而定 程式碼若還有不懂之處再提出來一起討論 < src="http://delphi.ktop.com.tw/loadfile.php?TOPICID=8147403&CC=182217"> 發表人 - ddy 於 2003/04/15 17:11:23
carter6
初階會員


發表:53
回覆:108
積分:42
註冊:2002-12-16

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-04-16 12:54:10 IP:61.216.xxx.xxx 未訂閱
不好意思 我已經改好了 謝謝版主給的意見 Delphi 初學者
系統時間:2024-05-03 0:24:14
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!