Solved

DBGrid Selected Row Color

Posted on 2000-04-13
9
585 Views
Last Modified: 2010-04-04
Very Simple Q

Delphi5 Hase A DBGrid .Ok.
How can I Change The Color Of Selected Row From Blue To Any Other Color

I Can't Use Any Other Components Due to
BiDiMode Selection I Must Use The DBGrid That Came With Delphi5.
0
Comment
Question by:zaferms
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 4

Expert Comment

by:jeurk
ID: 2712952
in your ondrawcolumncell add :
  //highlight the focused lines
  if ((gdFocused in State) then
  begin
    theGrid.Canvas.Brush.Color := clYourColor;
    theGrid.Canvas.FillRect(Rect);
  end
  else
  begin
    theGrid.Canvas.Brush.Color := clBtnFace;
    theGrid.Canvas.FillRect(Rect);
  end;

  // make the output rectangle a bit smaller to have a small distance from the borders
  InflateRect(Rect, -2, -2);

  //draw the text right or left aligned
  if (Col > 0) and not (gdFixed in state) then
    DrawText(theGrid.Canvas.Handle, PChar(S), Length(S), Rect, DT_RIGHT)
  else
    DrawText(theGrid.Canvas.Handle, PChar(S), Length(S), Rect, DT_LEFT);

or to let delphi draw the text for you call DefaultDrawColumnCell after the fillrect

cU
0
 
LVL 3

Accepted Solution

by:
Stefaan earned 30 total points
ID: 2715385
Hi Jeurk & Zaferms,

There is an easier way to do it.

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
  if (gdSelected) in State then
  begin
    DBGrid1.Canvas.Brush.Color := clMaroon;
    DBGrid1.DefaultDrawColumnCell (Rect, DataCol, Column, State);
  end;
end;

There is no need to use the DrawText, you can let Delphi do the drawing by using the DefaultDrawColumnCell.
0
 
LVL 4

Expert Comment

by:jeurk
ID: 2715396
euh, and what is different with what I say ?
That's what I say at the end, that we can use DefaultDrawColumnCell to draw the text. I left the drawtext, to show how to do it.
But I agree, what you say is somewhat cleaner...

CU
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:rwilson032697
ID: 2715480
Here is another example:

http://community.borland.com/article/0,1410,20845,00.html

Cheers,

Raymond.
0
 
LVL 3

Expert Comment

by:Stefaan
ID: 2715571
Jeurk,

If you do it like I showed, there is no need to call the DBGrid1.Canvas.FillRect(Rect) Method.

So you save some lines of code ;-)
0
 

Author Comment

by:zaferms
ID: 2715713
Thanx Jeurk & stefaan

you are Great both of you

iam confused  how can i grade you both

jeurk : your code is good but long thou
but still work.

stefaan : your code is short and good

please tell me how can i grade you both

or one of you

thanks all


0
 
LVL 3

Expert Comment

by:Stefaan
ID: 2715726
Well, you could always split the points ;-)
0
 

Author Comment

by:zaferms
ID: 2715874
how stefaan
0
 
LVL 3

Expert Comment

by:Stefaan
ID: 2715944
Well, reward one of the us with the points and post a new question with title :

Points for <name>

And add a comment here so that the second user gets notified that he can get his points on another question
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

751 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