如何给DBGRID 单元格上色
就是我根据每一行最后一个字段的值的判断来给该行第一个字段的字体或者背景改变颜色。该怎么实现?
[解决办法]
自己改一改,我这个是DBGridEh1的跟你的差不多,我这个是根据记录号的单双变化的颜色,你相应的改一下
void __fastcall TForm1::DBGridEh1DrawDataCell(TObject *Sender, const TRect &Rect, TField *Field, Gridseh::TGridDrawState State){ if( DBGridEh1->DataSource->DataSet->RecNo%2==0) { DBGridEh1->Canvas->Brush->Color = clCream;//clSkyBlue;// }else{ DBGridEh1->Canvas->Brush->Color = clMoneyGreen; } if(State.Contains(Gridseh::gdSelected)) DBGridEh1->Canvas->Brush->Color = TColor(0x00800040);//clAqua; if(State.Contains(Gridseh::gdFocused)) DBGridEh1->Canvas->Brush->Color = TColor(0x00800040);//clAqua; DBGridEh1->DefaultDrawDataCell(Rect,Field,State); }
[解决办法]
根据数值改变字体颜色
void __fastcall TForm1::DBGrid1DrawColumnCell(TObject *Sender, const TRect &Rect, int DataCol, TColumn *Column, TGridDrawState State){ DBGrid1->Canvas->Brush->Style= bsSolid; DBGrid1->Canvas->Brush->Color=State.Contains(gdSelected) ? (TColor)RGB(49,106,197) : Column->Field->DataSet->RecNo%2 != 0 ? clWhite : clCream;; DBGrid1->Canvas->FillRect(Rect); DBGrid1->Canvas->Pen->Style=psSolid; // DBGrid1->Canvas->Font->Style=TFontStyles()<< fsBold; DBGrid1->Canvas->Font->Color=clBlack; int ARow=Column->Field->DataSet->RecNo,ACol=DataCol; if ( ACol==4 && ARow>0 && StrToFloat(Column->Field->Text)>ad->MaxValue[0] || ACol==5 && ARow>0 && StrToFloat(Column->Field->Text)>ad->MaxValue[1] || ACol==6 && ARow>0 && StrToFloat(Column->Field->Text)>ad->MaxValue[2] || ACol==7 && ARow>0 && StrToFloat(Column->Field->Text)>ad->MaxValue[3] || ACol==8 && ARow>0 && StrToFloat(Column->Field->Text)>ad->MaxValue[4] || ACol==9 && ARow>0 && StrToFloat(Column->Field->Text)>ad->MaxValue[5] || ACol==10 && ARow>0 && StrToFloat(Column->Field->Text)>ad->MaxValue[6] || ACol==11 && ARow>0 && StrToFloat(Column->Field->Text)>ad->MaxValue[7] || ACol==13 && ARow>0 && StrToFloat(Column->Field->Text)>ad->MaxValue[8] ) { DBGrid1->Canvas->Font->Color = State.Contains(gdSelected) ? clBlack : clRed; } if(ACol==0 && sts.Pos(Column->Field->Text)) DBGrid1->Canvas->Font->Color = State.Contains(gdSelected) ? clBlack : clBlue; DrawText(DBGrid1->Canvas->Handle, Column->Field->Text.c_str(),-1,(RECT*)&Rect,DT_SINGLELINE | DT_VCENTER |DT_CENTER);}
[解决办法]