让TDBGrid的奇偶数行顯示不同的顏色

在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);
}

阅读更多
版权声明:原创文章转载时请附上原文链接 https://blog.csdn.net/aniven/article/details/556288
个人分类: C++/BCB开发
想对作者说点什么? 我来说一句
相关热词

没有更多推荐了,返回首页

关闭
关闭
关闭