Solved

coloring dbgrid rows

Posted on 2004-11-02
408 Views
Last Modified: 2010-04-05
hi,

i would like to color dbgrid rows if row count is odd, row color is red else blue.
i tried this,

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin

  if (query1.RecNo mod 2) = 1 then
    DBGrid1.Canvas.Brush.Color := clRed
  else
    DBGrid1.Canvas.Brush.Color := clBlue;

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

but it doesnt work, cause query1.RecNo is always -1.

if you help me, i will pleased.

thanks...
0
Question by:komputer
    10 Comments
     
    LVL 6

    Expert Comment

    by:vadim_ti
    try instead of
    query1.RecNo

    DBGrid1.DataSource.DataSet.ActiveRecord
    0
     

    Author Comment

    by:komputer
    but i cant DBGrid1.DataSource.DataSet.ActiveRecord property,

    given error message 'undeclared identifier: 'ActiveRecord'
    0
     
    LVL 27

    Expert Comment

    by:kretzschmar
    >but it doesnt work, cause query1.RecNo is always -1.

    after the open-method, or in the afteropen-event use

    query1.fetchall;

    this will edjust the recno-property

    attention: this will cause a performance leak after open a huge dataset

    meikl ;-)
    0
     

    Author Comment

    by:komputer
    to kretzschmar

    i tried but it doesnt work. it stills return -1 as RecNo
    0
     
    LVL 27

    Expert Comment

    by:kretzschmar
    well, in this case use additional the last-method like

    query1.fetchall;
    query1.last;

    hope thats it

    meikl ;-)
    0
     

    Author Comment

    by:komputer
    to kretzschmar again

    i tried but it doesnt work. it still returns -1 as RecNo
    0
     
    LVL 27

    Expert Comment

    by:kretzschmar
    well, then you can't use the recno for this
    (the correct entry depends on the database u use)
    0
     
    LVL 14

    Expert Comment

    by:Pierre Cornelius
    I tested the following with both a TTable and TQuery dataset linked to the datasource. Both works fine.

    procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
      DataCol: Integer; Column: TColumn; State: TGridDrawState);
    begin
      if Odd(DbGrid1.DataSource.DataSet.RecNo)
        then DBGRID1.Canvas.Brush.Color:= clBlue
        else DBGRID1.Canvas.Brush.Color:= clRed;
      DBGRID1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
      Invalidate;
    end;

    What dataset are you using? What type query?
    0
     
    LVL 26

    Accepted Solution

    by:
    Try this instead, it even works when you have datasets that don't have a RecNo property or it is always -1:
    {originally posted to the Borland newsgroups by Sebastian Garde}

    // Write an OnDrawColumnCell handler like this:
    procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
     DataCol: Integer; Column: TColumn; State: TGridDrawState);
    begin
      if ((((Rect.Top div (Rect.Bottom - Rect.Top)) mod 2) <> 1))
        and (not (gdSelected in State)) then
      begin
         DBGrid1.Canvas.Brush.Color := clRed;
      end
      else
      begin
         DBGrid1.Canvas.Brush.Color := clBlue;
      end;
     DBGrid1.DefaultDrawColumnCell(Rect, DataCol, Column, State);
    end;

    // set the afterscroll for your dataset like this:
    // I used a TADOQuery to test...
    procedure TForm1.ADOQuery1AfterScroll(DataSet: TDataSet);
    begin
      DBGrid1.Repaint;
    end;
    0
     

    Author Comment

    by:komputer
    i think the problem is my database (ms sql server) doesnt hve recno property.
    EddieShipman's solution is ok.

    thaks guys...
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT Security CISA, CISSP & CISM Certification

    Master the advanced techniques required to protect network resources from external threats with the IT Cyber Security bundle. Built around industry best-practice guidelines, the IT Cyber Security bundle consists of three in-depth courses.

    Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
    In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
    This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
    Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

    931 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now