Solved

DBGrid Selected Row Color

Posted on 2000-04-13
9
571 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
  • 4
  • 2
  • 2
  • +1
9 Comments
 
LVL 4

Expert Comment

by:jeurk
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 12

Expert Comment

by:rwilson032697
Comment Utility
Here is another example:

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

Cheers,

Raymond.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 3

Expert Comment

by:Stefaan
Comment Utility
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
Comment Utility
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
Comment Utility
Well, you could always split the points ;-)
0
 

Author Comment

by:zaferms
Comment Utility
how stefaan
0
 
LVL 3

Expert Comment

by:Stefaan
Comment Utility
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

763 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

15 Experts available now in Live!

Get 1:1 Help Now