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

想請教如何控制StringGrid某值小於時候就整列變色呢?

答題得分者是:careychen
lovemari
中階會員


發表:134
回覆:224
積分:76
註冊:2005-08-18

發送簡訊給我
#1 引用回覆 回覆 發表時間:2008-11-17 17:20:07 IP:122.116.xxx.xxx 訂閱
想控制某攔位的值如果小於 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
編輯記錄
lovemari 重新編輯於 2008-11-17 17:21:32, 註解 無‧
lovemari 重新編輯於 2008-11-17 17:21:57, 註解 無‧
careychen
尊榮會員


發表:41
回覆:580
積分:959
註冊:2004-03-03

發送簡訊給我
#2 引用回覆 回覆 發表時間:2008-11-17 17:58:46 IP:60.248.xxx.xxx 訂閱
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

發送簡訊給我
#3 引用回覆 回覆 發表時間:2008-11-17 18:21:50 IP:220.141.xxx.xxx 訂閱
參考一下StringGrid 特效
「VCL How To - 公告發表區」 » VCL How To - Additional 元件篇
http://delphi.ktop.com.tw/board.php?cid=169&fid=957&tid=20183
------
努力會更接近成功
lovemari
中階會員


發表:134
回覆:224
積分:76
註冊:2005-08-18

發送簡訊給我
#4 引用回覆 回覆 發表時間:2008-11-18 14:48:07 IP:122.116.xxx.xxx 訂閱
 謝謝兩位大大的分享

可好像不是小弟想要的耶

小弟想要的是

我的StringGrid 資料會一值變動 (程式開啟之後就都不會去動 只會看而已)
當我指定(在這不是指我滑鼠去點,而是我程式寫好的 假設 StringGrid (7,rowcount-1))
的欄位資料變動小於我設定的標準時候..
該列資料就要變色 並且觸發BUTTON 的功能
------
Program : Delphi 7
DataBase : Oracle 9i
Client : ClientDataSet
編輯記錄
lovemari 重新編輯於 2008-11-18 14:48:44, 註解 無‧
lovemari 重新編輯於 2008-11-18 14:49:04, 註解 無‧
pceyes
尊榮會員


發表:70
回覆:657
積分:1140
註冊:2003-03-13

發送簡訊給我
#5 引用回覆 回覆 發表時間:2008-11-19 09:34:17 IP:122.118.xxx.xxx 訂閱

[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

發送簡訊給我
#6 引用回覆 回覆 發表時間:2008-11-19 09:49:48 IP:118.168.xxx.xxx 訂閱
我只會 BCB,應該大同小異吧!! 請參考~

[code cpp]
//---------------------------------------------------------------------------
#include
#include
#pragma hdrstop
#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
std::deque g_StringGridColored;
//---------------------------------------------------------------------------
__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

發送簡訊給我
#7 引用回覆 回覆 發表時間:2008-11-19 10:25:51 IP:60.248.xxx.xxx 訂閱
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;
IF Cells[7,RowCount-1] = Fieldbyname('
直通率').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

發送簡訊給我
#8 引用回覆 回覆 發表時間:2008-11-19 12:06:54 IP:122.118.xxx.xxx 訂閱
嘻!又偷學了二個函數
謝謝 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

發送簡訊給我
#9 引用回覆 回覆 發表時間:2008-11-19 16:17:34 IP:122.116.xxx.xxx 訂閱
哇...好詳細喔

可是careychen ...我是用D7

所以沒有 ReplaceStr = =
------
Program : Delphi 7
DataBase : Oracle 9i
Client : ClientDataSet
編輯記錄
lovemari 重新編輯於 2008-11-19 16:48:25, 註解 無‧
Coffee
版主


發表:31
回覆:878
積分:561
註冊:2006-11-15

發送簡訊給我
#10 引用回覆 回覆 發表時間:2008-11-19 17:40:10 IP:59.124.xxx.xxx 訂閱
都已經寫成這樣了,難不成你要別人餵你啊?
真的快看不下去...

===================引 用 lovemari 文 章===================
哇...好詳細喔

可是careychen ...我是用D7

所以沒有 ReplaceStr = =
------
不論是否我發的文,在能力範圍皆很樂意為大家回答問題。
為了補我的能力不足之處,以及讓答案可以被重複的使用,希望大家能儘量以公開的方式問問題。
在引述到我的文時自然會儘量替各位想辦法,謝謝大家!
careychen
尊榮會員


發表:41
回覆:580
積分:959
註冊:2004-03-03

發送簡訊給我
#11 引用回覆 回覆 發表時間:2008-11-19 18:11:11 IP:60.248.xxx.xxx 訂閱
在 D7 裡,他叫 AnsiReplaceStr 多了 Ansi

D2007 之後 (是不是 2007 還是更早的版本我就不曉得了),他把 AnsiReplaceStr 改名叫 ReplaceStr
但實際上還是呼叫 AnsiReplaceStr

所以你記得上面要 use StrUtils ,然後把 ReplaceStr 改成 AnsiReplaceStr 即可
===================引 用 lovemari 文 章===================
哇...好詳細喔

可是careychen ...我是用D7

所以沒有 ReplaceStr = =
------
價值的展現,來自於你用哪一個角度來看待它!!
lovemari
中階會員


發表:134
回覆:224
積分:76
註冊:2005-08-18

發送簡訊給我
#12 引用回覆 回覆 發表時間:2008-11-19 20:05:21 IP:122.116.xxx.xxx 訂閱

真對不住 小弟這方面真的很新手
造成各位大大困擾真的很抱歉

也有由衷謝謝回答我的大大
由衷的感謝

===================引 用 Coffee 文 章===================
都已經寫成這樣了,難不成你要別人餵你啊?
真的快看不下去...

===================引 用 lovemari 文 章===================
哇...好詳細喔

可是careychen ...我是用D7

所以沒有 ReplaceStr = =
------
Program : Delphi 7
DataBase : Oracle 9i
Client : ClientDataSet
系統時間:2024-04-26 13:59:32
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!