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
192 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 24

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
 
LVL 4

Accepted Solution

by:
nestorua earned 200 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

759 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

22 Experts available now in Live!

Get 1:1 Help Now