在DBGrid的onDrawColumnCell事件中加入以下代碼
void __fastcall TForm1::DBGrid1DrawColumnCell(TObject *Sender,
const TRect &Rect, int DataCol, TColumn *Column,
TGridDrawState State)
{
TDBGrid *grid=(TDBGrid *)Sender;
if(Column->Field->DataSet->RecNo%2)
{
grid->Canvas->Font->Color=clWindowText;
grid->Canvas->Brush->Color=(TColor)RGB(229,236,249);
//grid->Canvas->FillRect(Rect); //可有可無
if (State.Contains(gdSelected) || State.Contains(gdFocused))
{
grid->Canvas->Font->Color=clHighlightText;
grid->Canvas->Brush->Color=clHighlight;
}
}
grid->DefaultDrawColumnCell(Rect,DataCol,Column,State);
}
或者(下列只在DBGrid沒有自定義Column時可用)
void __fastcall TForm1::DBGrid1DrawDataCell(TObject *Sender,
const TRect &Rect, TField *Field, TGridDrawState State)
{
TDBGrid *grid=(TDBGrid *)Sender;
if (Field->DataSet->RecNo%2)
{
grid->Canvas->Font->Color=clWindowText;
grid->Canvas->Brush->Color=(TColor)RGB(238,238,238);
if (State.Contains(gdSelected) || State.Contains(gdFocused))
{
grid->Canvas->Font->Color=clHighlightText;
grid->Canvas->Brush->Color=clHighlight;
}
}
grid->DefaultDrawDataCell(Rect,Field,State);
}