Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How to do a two dimension grid ? How to color the row and column of a grid when focused or selected ?

Posted on 2002-06-11
12
Medium Priority
?
202 Views
Last Modified: 2010-04-04
I have a query on how to come out with a grid like this :-

Year     OFFICE1     OFFICE2     OFFICE3
2000     1000        1000        3000
1999     1000        1100        2000
1998     800         2000        1500

Then how to color them if I click on cells with value "3000" ?  I need to color them in this way where the row of year 2000 is colored and colum of Office3 is colored as well.

Please guide.
0
Comment
Question by:ivylnm
  • 4
  • 4
  • 2
  • +2
12 Comments
 
LVL 1

Expert Comment

by:MBo
ID: 7071876
set
DBGrid1.DefaltDrawing:= False;  

This is an example for TTable to show the principle.

procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
if Column.Field.DisplayName='LAST_NAME' then
    DBGrid1.Canvas.Brush.Color := clBlue;
if Table1.FieldByName('ACCT_NBR').AsInteger =2094056 then
    DBGrid1.Canvas.Font.Color := clLime;
  DBGrid1.DefaultDrawColumnCell(Rect,DataCol, Column, State);
end;
0
 

Author Comment

by:ivylnm
ID: 7074763
Thanks for guiding.

But the codes that you provide is useful only if I want the data to be displayed in different colors.  Now my requirement is when I click on the grid where the data has been loaded into the grid, and that row and column of that cell are colored maybe with blue color.
0
 
LVL 26

Expert Comment

by:Tomas Helgi Johannsson
ID: 7075173
Hi!

I think this is what you are looking for.

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 := clRed;
        DBGrid1.DefaultDrawColumnCell(Rect,DataCol, Column, State);
   end
   else
   begin
        if Column.Field.DisplayName='NAME' then
        DBGrid1.Canvas.Brush.Color := clBlue;
        if Table1.FieldByName('NAME').AsString ='Boa' then
        DBGrid1.Canvas.Font.Color := clLime;
        DBGrid1.DefaultDrawColumnCell(Rect,DataCol, Column, State);
   end;
end;

Regards,
  Tomas Helgi

0
Industry Leaders: 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 4

Accepted Solution

by:
nestorua earned 600 total points
ID: 7076285
HI,
You can do it the following way:
//
var ToHiglight: boolean;
         Value: integer;
//
procedure TForm1.DBGrid1CellClick(Column: TColumn);
begin
  if Column.FieldName='Area'
   then
    begin
     ToHiglight:=True;
     Value:=Column.Field.AsInteger;
    end
   else ToHiglight:=False;
//
 DBGrid1.Invalidate;
end;
//
procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
 with DBGrid1 do
  begin
   if ToHiglight
    then
     begin
      if (((Column.FieldName='Area') or
          (DataSource.DataSet.FieldByName'Area').AsInteger=
                     Value))
            and not (gdSelected in State))
       then
        with Canvas do
         begin
          Brush.Color:=clBlue;
           Font.Color:=clWhite;
         end;
       end;
//
    DefaultDrawColumnCell(Rect, DataCol, Column, State);
//
   end;
end;
I try that on DBDEMOS database (Country table).
Don't forget to set DefaultDrawing=True (the property of the TDBGrid).
Sincerely,
Nestorua.
0
 

Author Comment

by:ivylnm
ID: 7077489
Thanks for the guide.  But this only solve part of my question.  

  How to color that specific column on the run time ? We can easily color a column at design time.  But I tried to color a column at run time but failed.

Besides, any idea of what kind of grid can allow to do two dimensional grid as shown above ?
0
 
LVL 4

Expert Comment

by:nestorua
ID: 7079172
HI,
I don't understand you. My code DOES make what you want in RUNTIME not designtime.
You didn't test it I guess.
Sincerely,
Nestorua.
0
 

Author Comment

by:ivylnm
ID: 7083265
Hi,  the codes does color a cell in RUNTIME, but I am actually looking for codes that can color a row of data and a column of data.

Such as :-
Year     OFFICE1     OFFICE2     OFFICE3
2000     1000        1000        3000
1999     1000        1100        2000
1998     800         2000        1500

If I click on cells value "1500", then it shall highlight the row of 1998 and column of OFFICE3.

Thanks,
Ivy
0
 
LVL 4

Expert Comment

by:nestorua
ID: 7083322
HI,
Still I don't understand you.
Try my code, that's all.
It works and do just what you want - highlight the corresponding row and column.
Sincerely,
Nestorua.
0
 
LVL 4

Expert Comment

by:nestorua
ID: 7083327
HI,
Still I don't understand you.
Try my code, that's all.
It works and does just what you want - highlights the corresponding row and column.
Sincerely,
Nestorua.
0
 

Author Comment

by:ivylnm
ID: 7123947
Sorry, I will try your codes later on as I get my hands on other projects these days.  Thanks for the guidance, shall reply you after I have tried the codes.
0
 
LVL 1

Expert Comment

by:pnh73
ID: 9010657
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Accept answer from nestorua

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Paul (pnh73)
EE Cleanup Volunteer
0
 
LVL 1

Expert Comment

by:pnh73
ID: 9017620
Thankyou for your response.

Paul (pnh73)
EE Cleanup Volunteer
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

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…
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 course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Suggested Courses

886 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