2014-11-19 4 views
3

"예"와 "아니오"값만있는 열이 있습니다. 열 값이 "예"만 해당 셀의 배경 색상이 빨간색 인 경우 내가 원하는 다른 "아니오"다음 배경 색상은 노란색 하지만이 코드 색상 전체 행 :DBGrid 특수 셀의 색상을 지정하는 방법은 무엇입니까?

if ADOTable1.FieldByName('Clubs').AsString = 'yes' then 
begin 
    DBGrid1.Canvas.Brush.Color := clRed; 
    DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State); 
end; 

편집

답장을 보내 주셔서 감사합니다. 내 진짜 코드처럼 보입니다. "netice"열에는 "L, D, W"만 있습니다.

if Column.FieldName = 'netice' then 
begin 
if ADOTable1.FieldByName('netice').AsString = 'L' then 
DBGrid1.Canvas.Brush.Color := clgreen ; 
if ADOTable1.FieldByName('netice').AsString = 'D' then 
DBGrid1.Canvas.Brush.Color := clRed ; 
if ADOTable1.FieldByName('netice').AsString = 'W' then 
DBGrid1.Canvas.Brush.Color := clYellow ; 
end; 
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State); 
end; 

하지만 L 필요 - D, 녹색 - 빨강, W - 노란색 내가 델파이 2010

enter image description here을 사용하고

+0

답장을 보내 주셔서 감사합니다. 이 코드는 행의 색을 지정합니다. 하지만 값이 "yes"또는 "no"인 셀만 색상을 지정해야합니다. –

+0

Adotable1은 눈금에 바인딩 된 데이터 집합이거나 다른 하나가 있어야 할 수도 있습니다. 'var ADS : TDataset; begin 광고 : = TDBGrid (보낸 사람) .DataSource.DataSet; if Column.FieldName = 'netice'then 시작 Ads.FieldByName ('netice') .AsString = 'L'이면 DBGrid1.Canvas.Brush.Color : = clgreen; ......... ' – bummi

+0

@bummi,'Column.Field'는 정확한 링크 된 데이터 세트 필드를 제공합니다. 그러므로 나는 아래 코드에서 그것을 사용했다. 그러나 여기서 나는 'FieldName' 비교 (OP는 후자가 아니라 첫 번째 경우의 첫 번째 자본을 사용)에서 훨씬 안전하다고 느끼지 않습니다. 어쩌면 뭔가 ['like this'] (http://pastebin.com/SufF1kz8)가 작동 할 수도 있습니다. – TLama

답변

7

브러시 색상을 원하는 열로 만 변경하는 것을 제한하는 조건을 추가해야합니다. 코드에서이 될 수있다 : 나는 아직 이러한 필드는 링크 된 데이터 세트에서이 있음을 보증하지 않습니다 Column.FieldName 때문에 Column.FieldName 전에이 선호하는 것

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; 
    DataCol: Integer; Column: TColumn; State: TGridDrawState); 
var 
    Field: TField; 
begin 
    // store the currently rendered cell's column assigned field reference 
    // (if any) to the local variable (there's quite expensive getter) 
    Field := Column.Field; 
    // if the rendered cell's column has assigned a field and this field's 
    // name is 'Clubs' (compared without case sensitivity), then, and only 
    // then change the brush color... 
    if Assigned(Field) and SameText(Field.FieldName, 'Clubs') then 
    begin 
    if Field.AsString = 'yes' then 
     DBGrid1.Canvas.Brush.Color := clRed 
    else 
     DBGrid1.Canvas.Brush.Color := clYellow; 
    end; 
    DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State); 
end; 

. 따라서 직접 필드에 액세스하는 것이이 방식에서 더 안전합니다.

+1

P. 다른 DB 열 유형을 선택하십시오. String은 부울 값을 저장하는 데 최적이 아닙니다. – TLama

3

당신은이 방법으로 수행 할 수 있습니다

if Column.FieldName = 'Clubs' then 
begin 
    if ADOTable1.FieldByName('Clubs').AsString = 'yes' then 
    DBGrid1.Canvas.Brush.Color := clRed 
    else 
    DBGrid1.Canvas.Brush.Color := clYellow; 
end; 
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State); 
관련 문제