2013-05-28 3 views

답변

14

당신은 쉽게 DBGrids를 사용하여이를 구현할 수 있습니다 onDrawColumnCell 이벤트 : 당신이 교사의 이름을 모르는 경우

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; 
    DataCol: Integer; Column: TColumn; State: TGridDrawState); 
begin 
if Table1.FieldByName('Teacher').AsString = 'Joe' 
then 
    DBGrid1.Canvas.Brush.Color:=clRed; 
DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State); 

end; 

그러나, 당신은 어떤 종류를 구현해야합니다 재귀 조치, 이건 내 구현 :

var TeacherStringList : TStringList; 
    lastColorUsed : TColor; 
    AColors : Array of TColor; 


function mycolor: TColor; 
begin 
    result := RGB(Random(256), Random(256), Random(256)); 
end; 

procedure TForm3.Button1Click(Sender: TObject); 
var CurrS : String; 
    Index : Integer; 
begin 

    if TeacherStringList.Count <> 0 then TeacherStringList.Clear; 
    Table1.DisableControls; 
    try 
    while not Table1.Eof do 
    begin 

     CurrS := Table1.FieldByName('Teacher').AsString; 
     if (not TeacherStringList.Find(CurrS,Index)) and (not currS.IsEmpty) 
     then TeacherStringList.Add(CurrS); 
     Table1.Next; 

    end; 
    Table1.First; 
    SetLength(AColors,TeacherStringList.Count); 
    for Index := Low(AColors) to High(AColors) 
    do AColors[Index] := mycolor; 

    finally 
    Table1.EnableControls; 
    end; 

end; 

procedure TForm3.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; 
    DataCol: Integer; Column: TColumn; State: TGridDrawState); 
var Index : integer; 

begin 

    if (TeacherStringList.Find(Table1.FieldByName('Teacher').AsString,Index)) 
    then 
    DBGrid1.Canvas.Brush.Color:= AColors[index]; 

    DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State); 

end; 

procedure TForm3.FormCreate(Sender: TObject); 
begin 
    teacherStringList := TStringList.Create; 
    teacherStringList.Sorted := True; 
end; 

procedure TForm3.FormDestroy(Sender: TObject); 
begin 
    teacherStringList.Free; 
end; 
+0

내가 알고하지 않기 때문에 그것은 작동하지 않습니다 선생님의 이름 – teocka

+0

가정합니다. Table1.FieldByName ('교사') AsString = '조'처음 10 행; Table1.FieldByName ('교사'). 다음 5 개의 행에서 AsString = 'Jack'; Table1.FieldByName ('교사'). 다음 8 개의 행에 AsString = 'Jim'; 처음 10 행은 회색이어야하고, 다음 5 행은 흰색이고 다음 8 행은 회색이고 다음은 흰색이됩니다. – teocka

+1

다음에 질문 할 때 가능한 한 많은 양의 데이터를 제공하는 것이 중요합니다. – Peter