想請教如何控制StringGrid某值小於時候就整列變色呢? |
答題得分者是:careychen
|
lovemari
中階會員 發表:134 回覆:224 積分:76 註冊:2005-08-18 發送簡訊給我 |
想控制某攔位的值如果小於 90%
就該整列都顯示紅色 [code delphi] With AdvStringGrid1 do begin Cells[1,0] := '工單'; Cells[2,0] := '機種名稱'; Cells[3,0] := '投產時間'; Cells[4,0] := '投產'; Cells[5,0] := '良品'; Cells[6,0] := '不良品'; Cells[7,0] := '直通率'; end; with csTempWOa do begin Close; Params.Clear; w1 := 'SELECT A.WORK_ORDER "工單",B.PART_NO "機種名稱", A.WO_START_DATE "投產時間",A.INPUT_QTY "投產", ' 'A.INPUT_QTY - COUNT(C.SERIAL_NUMBER) "良品數量", COUNT(C.SERIAL_NUMBER) "不良品數量", ' 'TRUNC((A.INPUT_QTY - COUNT(C.SERIAL_NUMBER)) / A.INPUT_QTY,2)*100||''%'' "直通率" ' 'FROM SAJET.G_WO_BASE A, ' 'SAJET.SYS_PART B, ' 'SAJET.G_SN_STATUS C ' 'WHERE A.MODEL_ID = B.PART_ID ' 'AND A.WORK_ORDER = C.WORK_ORDER ' 'AND A.WO_STATUS = ''3'' ' 'AND C.CURRENT_STATUS = ''1'' ' 'AND A.WORK_ORDER <> ''51A00001'' ' 'AND A.INPUT_QTY <> A.OUTPUT_QTY ' 'GROUP BY A.WORK_ORDER,B.PART_NO,A.WO_START_DATE,A.INPUT_QTY ' 'ORDER BY A.WO_START_DATE DESC '; commandtext := w1; open; Label1.Caption := 'Last Update Time:' TimeToStr(Time); while not eof do BEGIN With AdvStringGrid1 do begin Params.Clear; Cells[1,RowCount-1] := Fieldbyname('工單').AsString; Cells[2,RowCount-1] := Fieldbyname('機種名稱').AsString; Cells[3,RowCount-1] := Fieldbyname('投產時間').AsString; Cells[4,RowCount-1] := Fieldbyname('投產').AsString; Cells[5,RowCount-1] := Fieldbyname('良品數量').AsString; Cells[6,RowCount-1] := Fieldbyname('不良品數量').AsString; Cells[7,RowCount-1] := Fieldbyname('直通率').AsString; RowCount := RowCount 1; IF Cells[7,RowCount-1] = Fieldbyname(' 直通率').AsString < '90%' THEN BEGIN // ??????? END; NEXT; END; cLOSE; END; end; [/code]
------
Program : Delphi 7 DataBase : Oracle 9i Client : ClientDataSet |
careychen
尊榮會員 發表:41 回覆:580 積分:959 註冊:2004-03-03 發送簡訊給我 |
hi,早您一點點的文章中就有相關的方式,由 st33chen 大所回的
DBGrid如何使选中的整行变颜色(同时可以编辑) 請記得多搜尋就可以找到您要的資料 ===================引 用 lovemari 文 章=================== 想控制某攔位的值如果小於 90% 就該整列都顯示紅色 [code delphi] With AdvStringGrid1 do begin Cells[1,0] := '工單'; Cells[2,0] := '機種名稱'; Cells[3,0] := '投產時間'; Cells[4,0] := '投產'; Cells[5,0] := '良品'; Cells[6,0] := '不良品'; Cells[7,0] := '直通率'; end; with csTempWOa do begin Close; Params.Clear; w1 := 'SELECT A.WORK_ORDER "工單",B.PART_NO "機種名稱", A.WO_START_DATE "投產時間",A.INPUT_QTY "投產", ' 'A.INPUT_QTY - COUNT(C.SERIAL_NUMBER) "良品數量", COUNT(C.SERIAL_NUMBER) "不良品數量", ' 'TRUNC((A.INPUT_QTY - COUNT(C.SERIAL_NUMBER)) / A.INPUT_QTY,2)*100||''%'' "直通率" ' 'FROM SAJET.G_WO_BASE A, ' 'SAJET.SYS_PART B, ' 'SAJET.G_SN_STATUS C ' 'WHERE A.MODEL_ID = B.PART_ID ' 'AND A.WORK_ORDER = C.WORK_ORDER ' 'AND A.WO_STATUS = ''3'' ' 'AND C.CURRENT_STATUS = ''1'' ' 'AND A.WORK_ORDER <> ''51A00001'' ' 'AND A.INPUT_QTY <> A.OUTPUT_QTY ' 'GROUP BY A.WORK_ORDER,B.PART_NO,A.WO_START_DATE,A.INPUT_QTY ' 'ORDER BY A.WO_START_DATE DESC '; commandtext := w1; open; Label1.Caption := 'Last Update Time:' TimeToStr(Time); while not eof do BEGIN With AdvStringGrid1 do begin Params.Clear; Cells[1,RowCount-1] := Fieldbyname('工單').AsString; Cells[2,RowCount-1] := Fieldbyname('機種名稱').AsString; Cells[3,RowCount-1] := Fieldbyname('投產時間').AsString; Cells[4,RowCount-1] := Fieldbyname('投產').AsString; Cells[5,RowCount-1] := Fieldbyname('良品數量').AsString; Cells[6,RowCount-1] := Fieldbyname('不良品數量').AsString; Cells[7,RowCount-1] := Fieldbyname('直通率').AsString; RowCount := RowCount 1; IF Cells[7,RowCount-1] = Fieldbyname(' 直通率').AsString < '90%' THEN BEGIN // ??????? END; NEXT; END; cLOSE; END; end; [/code]
------
價值的展現,來自於你用哪一個角度來看待它!! |
pceyes
尊榮會員 發表:70 回覆:657 積分:1140 註冊:2003-03-13 發送簡訊給我 |
|
lovemari
中階會員 發表:134 回覆:224 積分:76 註冊:2005-08-18 發送簡訊給我 |
謝謝兩位大大的分享
可好像不是小弟想要的耶 小弟想要的是 我的StringGrid 資料會一值變動 (程式開啟之後就都不會去動 只會看而已) 當我指定(在這不是指我滑鼠去點,而是我程式寫好的 假設 StringGrid (7,rowcount-1)) 的欄位資料變動小於我設定的標準時候.. 該列資料就要變色 並且觸發BUTTON 的功能
------
Program : Delphi 7 DataBase : Oracle 9i Client : ClientDataSet |
pceyes
尊榮會員 發表:70 回覆:657 積分:1140 註冊:2003-03-13 發送簡訊給我 |
[code delphi] unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Grids; type TForm1 = class(TForm) Button1: TButton; StringGrid1: TStringGrid; procedure Button1Click(Sender: TObject); procedure StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); procedure StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean); private { Private declarations } public { Public declarations } end; // 新增 type TGridCracker = Class( TStringGrid ); {required to access protected method InvalidateRow} var Form1: TForm1; implementation {$R *.dfm} // 這是測試資料 procedure TForm1.Button1Click(Sender: TObject); var i,j : integer; begin for i := 0 to stringgrid1.ColCount -1 do begin for j := 0 to stringgrid1.RowCount-1 do begin stringgrid1.Cells[i,j]:= inttostr((i*10) j); end; end; end; procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); var Grid :Tstringgrid; begin // 要用顏色改這裏 grid := Sender As TStringGrid; if grid.Row = aRow then begin with Grid.Canvas.Brush do // 背景顏色 begin Color := ClYellow; Style := bsSolid; end; grid.Canvas.FillRect( Rect ); grid.Canvas.Font.Color := clGreen; // 字型顏色 grid.Canvas.TextRect( Rect, Rect.Left 2, Rect.Top 2, grid.Cells[acol, arow]); Grid.Canvas.Brush := grid.Brush; end; // 要自動偵測改這裏 33 是測試,改你要的值 if grid.Cells[Acol,Arow] = '33' then Grid.Row := Arow; // 跑到指定的列 end; // 記得要加 procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer; var CanSelect: Boolean); begin with TGridCracker( Sender as TStringGrid ) do begin InvalidateRow( Row ); InvalidateRow( aRow ); end; end; end. [/code]
------
努力會更接近成功 |
gac
初階會員 發表:2 回覆:28 積分:26 註冊:2004-08-16 發送簡訊給我 |
我只會 BCB,應該大同小異吧!! 請參考~
[code cpp] //--------------------------------------------------------------------------- #include #include #pragma hdrstop #include "Unit1.h" //--------------------------------------------------------------------------- #pragma package(smart_init) #pragma resource "*.dfm" TForm1 *Form1; std::deque //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { // 防止更新時閃爍 StringGrid1->DoubleBuffered = true; // 變數初始化 g_StringGridColored.resize(StringGrid1->RowCount, false); } //--------------------------------------------------------------------------- void __fastcall TForm1::BitBtn1Click(TObject *Sender) { // } //--------------------------------------------------------------------------- void __fastcall TForm1::StringGrid1DrawCell(TObject *Sender, int ACol, int ARow, TRect &Rect, TGridDrawState State) { // 過濾掉 Fixed 的 Cells if(State.Contains(gdFixed)) return; // *** 自行設定判斷條件 if(StringGrid1->Cells[ACol][ARow].ToIntDef(0) > 500) { // 檢查未著色時才著色 if(!g_StringGridColored[ARow]) { g_StringGridColored[ARow] = true; StringGrid1->Canvas->Brush->Color = clLime; // 取得此列左邊界 TRect rLRect = StringGrid1->CellRect(StringGrid1->FixedCols, ARow); // 取得此列右邊界 TRect rRRect = StringGrid1->CellRect(StringGrid1->ColCount-1, ARow); TRect rRect = rLRect; rRect.Left = rLRect.Left; rRect.Right = rRRect.Right; // 著色 StringGrid1->Canvas->Rectangle(rRect); // 貼字 StringGrid1->Canvas->TextRect(Rect, Rect.left 5, Rect.top 5, StringGrid1->Cells[ACol][ARow]); // 更新其他 Cells 的顏色 StringGrid1->Refresh(); // 觸發 BitBtn1 的 OnClick 事件 BitBtn1Click(StringGrid1); } } // 更新時塗上該有的顏色 if(g_StringGridColored[ARow]) { StringGrid1->Canvas->Brush->Color = clLime; StringGrid1->Canvas->TextRect(Rect, Rect.left 5, Rect.top 5, StringGrid1->Cells[ACol][ARow]); } } //--------------------------------------------------------------------------- [/code]
------
我..........還很嫩!
編輯記錄
gac 重新編輯於 2008-11-19 09:53:19, 註解 無‧
|
careychen
尊榮會員 發表:41 回覆:580 積分:959 註冊:2004-03-03 發送簡訊給我 |
HI, 不好意思,現在才回您
您可以在 AdvStringGrid 的 OnDrawCell 中,加入 [code delphi] procedure TForm1.AdvStringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); var iRate: Integer; begin if gdFixed in State then Exit; with Sender as TAdvStringGrid do begin iRate := StrToIntDef(ReplaceStr(Cells[7, ARow], '%', ''), -1); if iRate >= 0 then begin case iRate of 0..89: Canvas.Brush.Color := clRed; 90..100: Canvas.Brush.Color := clWhite; end; Canvas.Rectangle(CellRect(1, 2).Left, CellRect(1, 2).Top, CellRect(ColCount-1, 2).Right, CellRect(ColCount-1, 2).Bottom); Canvas.TextRect(Rect, Rect.Left, Rect.Top, Cells[ACol, ARow]); end; end; end; [/code] 觸發 Button 的功能 Params.Clear; Cells[1,RowCount-1] := Fieldbyname('工單').AsString; Cells[2,RowCount-1] := Fieldbyname('機種名稱').AsString; Cells[3,RowCount-1] := Fieldbyname('投產時間').AsString; Cells[4,RowCount-1] := Fieldbyname('投產').AsString; Cells[5,RowCount-1] := Fieldbyname('良品數量').AsString; Cells[6,RowCount-1] := Fieldbyname('不良品數量').AsString; Cells[7,RowCount-1] := Fieldbyname('直通率').AsString; RowCount := RowCount 1; 直通率').AsString < '90%' THEN BEGIN // ??????? // 為什麼要用 999 ,因為我怕您會有 Null 或是其他不正常值的產生,結果也寄出了 Mail // 不過您如果有需要的話,把他改成 0 也行,代表【不正常】或是【小於 90%】的都會寄 Mail if (StrToIntDef(ReplaceStr(Cells[7, ARow], '%', ''), 999) < 90 then btnMail.Click; NEXT; END; cLOSE; END; end;
------
價值的展現,來自於你用哪一個角度來看待它!! |
pceyes
尊榮會員 發表:70 回覆:657 積分:1140 註冊:2003-03-13 發送簡訊給我 |
嘻!又偷學了二個函數
謝謝 careychen 大 在此之前只會用IntToStr,用 StrToIntDef 更好,不用再Try Except 了。 StrToIntDef 在 D7 就有了,但 ReplaceStr 網路上找了半天,想在D7是沒有的(不知從那個版本冒出來的),但在D2009是有的,但要uses Cells[7, ARow], '' ,[rfReplaceAll])。 ===================引 用 careychen 文 章=================== .......略 // 為什麼要用 999 ,因為我怕您會有 Null 或是其他不正常值的產生,結果也寄出了 Mail // 不過您如果有需要的話,把他改成 0 也行,代表【不正常】或是【小於 90%】的都會寄 Mail if (StrToIntDef(ReplaceStr(Cells[7, ARow], '%', ''), 999) < 90 then btnMail.Click; NEXT; END; cLOSE; END; end;
------
努力會更接近成功 |
lovemari
中階會員 發表:134 回覆:224 積分:76 註冊:2005-08-18 發送簡訊給我 |
|
Coffee
版主 發表:31 回覆:878 積分:561 註冊:2006-11-15 發送簡訊給我 |
|
careychen
尊榮會員 發表:41 回覆:580 積分:959 註冊:2004-03-03 發送簡訊給我 |
在 D7 裡,他叫 AnsiReplaceStr 多了 Ansi
D2007 之後 (是不是 2007 還是更早的版本我就不曉得了),他把 AnsiReplaceStr 改名叫 ReplaceStr 但實際上還是呼叫 AnsiReplaceStr 所以你記得上面要 use StrUtils ,然後把 ReplaceStr 改成 AnsiReplaceStr 即可 ===================引 用 lovemari 文 章=================== 哇...好詳細喔 可是careychen 大...我是用D7 所以沒有 ReplaceStr = =
------
價值的展現,來自於你用哪一個角度來看待它!! |
lovemari
中階會員 發表:134 回覆:224 積分:76 註冊:2005-08-18 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |