DBGrid問題 |
尚未結案
|
egghead253
一般會員 發表:25 回覆:13 積分:7 註冊:2009-05-01 發送簡訊給我 |
|
GrandRURU
站務副站長 發表:240 回覆:1680 積分:1874 註冊:2005-06-21 發送簡訊給我 |
因為dbgrid不支援 LOB 屬性的欄位
===================引 用 egghead253 文 章=================== 我用SQL搜尋出來的資料用DBGrid顯示,可是有些欄位出現MEMO,這是什麼原因? |
egghead253
一般會員 發表:25 回覆:13 積分:7 註冊:2009-05-01 發送簡訊給我 |
我用Access匯入的,那有辨法可以呼叫出來嗎?
===================引 用 GrandRURU 文 章=================== 因為dbgrid不支援 LOB 屬性的欄位 ===================引 用 egghead253 文 章=================== 我用SQL搜尋出來的資料用DBGrid顯示,可是有些欄位出現MEMO,這是什麼原因? |
GrandRURU
站務副站長 發表:240 回覆:1680 積分:1874 註冊:2005-06-21 發送簡訊給我 |
出處:使用tdbgrid如何使memo欄位出現內容
[code delphi] { Uploader: Helmut Dollinger Company: Reference: N/A Question/Problem/Abstract: By default the DBGrid Control of the VCL can't display memo fields. Here is a solution... Answer: The problem can be solved with the OnDrawDataCell event of the DBGrid. } 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; {Note: before running create a TMemoField object for the memo field by double clicking on the TTable component and adding the memo field.} [/code]
編輯記錄
GrandRURU 重新編輯於 2009-12-21 08:35:34, 註解 無‧
|
senso
高階會員 發表:5 回覆:126 積分:226 註冊:2003-11-27 發送簡訊給我 |
我試著改bcb有兩個地方不曉得怎麼改,請指導一下,謝謝
[code cpp] void __fastcall TForm1::DBGrid1DrawDataCell(TObject *Sender, const TRect &Rect, TField *Field, TGridDrawState State) { char P[50]; TBlobStream *bs; AnsiString hStr; //if Field is TMemoField then //這行不知道怎麼改 if (Field->DataType == ftMemo) { TCanvas *ACanvas = ((TDBGrid *)Sender)->Canvas; bs = new TBlobStream(Table1Notes, bmRead); //這行不知道怎麼改,一直error //bs = new TBlobStream((TMemoField *)Field, bmRead); //raised exception class EInvalidCast with message 'Invalid class typecast' memset(P,char(0x0),sizeof(P)); bs->Read(P, 50); bs->Free(); hStr = StrPas(P); while (hStr.Pos(char(0x13)) > 0) hStr[hStr.Pos(char(0x13))] = ' '; while (hStr.Pos(char(0x10)) > 0) hStr[hStr.Pos(char(0x10))] = ' '; ACanvas->FillRect(Rect); ACanvas->TextOut(Rect.Left, Rect.Top, hStr); } } [/code] 另外有找到一篇DBGrid如何顯示text型態欄位資料 沒使用TBlobStream效果好像也差不多 [code cpp] void __fastcall TForm1::DBGrid1DrawColumnCell(TObject *Sender, const TRect &Rect, int DataCol, TColumn *Column, TGridDrawState State) { TField *F; TCanvas *ACanvas; F=Column->Field; if (!(F->DataType == ftMemo || F->DataType == ftFmtMemo )) return; ACanvas = ((TDBGrid *)Sender)->Canvas; ACanvas->TextRect(Rect,Rect.Left,Rect.Top,F->AsString); } [/code] |
GrandRURU
站務副站長 發表:240 回覆:1680 積分:1874 註冊:2005-06-21 發送簡訊給我 |
===================引 用 senso 文 章=================== 我試著改bcb有兩個地方不曉得怎麼改,請指導一下,謝謝 [code cpp] void __fastcall TForm1::DBGrid1DrawDataCell(TObject *Sender, const TRect &Rect, TField *Field, TGridDrawState State) { char P[50]; TBlobStream *bs; AnsiString hStr; // if (Field->ClassNameIs(TMemoField)) ↓ 試試看 //if Field is TMemoField then //這行不知道怎麼改 if (Field->DataType == ftMemo) { TCanvas *ACanvas = ((TDBGrid *)Sender)->Canvas; // #include <DBTables.hpp> 試試看 bs = new TBlobStream(Table1Notes, bmRead); //這行不知道怎麼改,一直error } } [/code]
編輯記錄
GrandRURU 重新編輯於 2009-12-24 17:18:20, 註解 無‧
|
senso
高階會員 發表:5 回覆:126 積分:226 註冊:2003-11-27 發送簡訊給我 |
謝謝指導!
原來是ClassName...汗 我有#include 還是搞不懂怎麼new TBlobStream,只好參考別的方式來寫 關於DBGrid顯示memo型態資料問題 另外 在 DBGrid 內顯示 Memo 內容之後的欄位格子會有殘影 [code cpp] void __fastcall TForm1::DBGrid1DrawDataCell(TObject *Sender, const TRect &Rect, TField *Field, TGridDrawState State) { char P[50]; TBlobStream *bs; //或用TStream *bs; AnsiString hStr=Field->AsString; if (Field->ClassNameIs("TMemoField")) //或 if (Field->ClassNameIs(Field->ClassType(),"TMemoField")) //其實一樣, ClassNameIs(const AnsiString string)就是call ClassNameIs(TClass cls, const AnsiString string) { TCanvas *ACanvas = ((TDBGrid *)Sender)->Canvas; //bs = new TBlobStream(Table1Notes, bmRead); //這行不知道怎麼改,一直error //bs = new TBlobStream((TMemoField *)Field, bmRead); //raised exception class EInvalidCast with message 'Invalid class typecast' bs = (TBlobStream *)((TDBGrid *)Sender)->DataSource->DataSet->CreateBlobStream(Field,bmRead); //或用TStream //bs = ((TDBGrid *)Sender)->DataSource->DataSet->CreateBlobStream(Field,bmRead); memset(P,char(0x0),sizeof(P)); bs->Read(P, 50); bs->Free(); hStr = StrPas(P); while (hStr.Pos(char(0x13)) > 0) hStr[hStr.Pos(char(0x13))] = ' '; while (hStr.Pos(char(0x10)) > 0) hStr[hStr.Pos(char(0x10))] = ' '; ACanvas->FillRect(Rect); //ACanvas->TextOut(Rect.Left, Rect.Top, hStr); //改用TextRect ACanvas->TextRect(Rect,Rect.Left,Rect.Top,hStr); } } [/code] |
GrandRURU
站務副站長 發表:240 回覆:1680 積分:1874 註冊:2005-06-21 發送簡訊給我 |
|
senso
高階會員 發表:5 回覆:126 積分:226 註冊:2003-11-27 發送簡訊給我 |
我剛好是用ADO來試... 0rz
改TADOBlobStream就沒跳error了 謝謝~ ===================引 用 GrandRURU 文 章=================== 那個寫法是給bde的,如果你是用ado的話,有另一種寫法。 請參考CSDN這篇 将图片存入SQL数据库的image字段,使用流类,出错,怎么解决? |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |