Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to Highlight an entire row in a TStringGrid Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
05-Nov-02
Category
VCL-General
Language
Delphi 4.x
Views
78
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas

Using Delphi 4 and a TStringGrid component: How do I highlight the entire row in 
the grid where the cursor is currently? In other words, if the user clicks their 
mouse cursor on row 3, I want the entire row 3 to be highlighted pale yellow. When 
they then move the cursor to row 6, I want row 3 to revert back to white, and then 
have the entire row 6 turn pale yellow. Please note that I must be able to also 
edit the contents of any cell in the highlighted row.

Answer:

You can achieve this fairly easily with a combination of handlers for the 
OnSelectCell and OndrawCell events of the grid:

1   type
2     TGridCracker = class(TStringGrid);
3     {required to access protected method InvalidateRow}
4   
5   procedure TForm1.StringGrid1SelectCell(Sender: TObject; ACol, ARow: Integer;
6     var CanSelect: Boolean);
7   begin
8     with TGridCracker(Sender as TStringGrid) do
9     begin
10      InvalidateRow(Row);
11      InvalidateRow(aRow);
12    end;
13  end;
14  
15  procedure TForm1.StringGrid1DrawCell(Sender: TObject; ACol, ARow: Integer;
16    Rect: TRect; State: TGridDrawState);
17  var
18    grid: TStringGrid;
19  begin
20    if gdFixed in State then
21      Exit;
22    grid := Sender as TStringGrid;
23    if grid.Row = aRow then
24    begin
25      with Grid.Canvas.Brush do
26      begin
27        Color := $C0FFFF; {pale yellow}
28        Style := bsSolid;
29      end;
30      grid.Canvas.FillRect(Rect);
31      grid.Canvas.Font.Color := clBlack;
32      grid.Canvas.TextRect(Rect, Rect.Left + 2, Rect.Top + 2, grid.Cells[acol, arow]);
33      Grid.Canvas.Brush := grid.Brush;
34    end;
35  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC