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
194 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 25

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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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
 

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Delphi 2 61
Delphi and Access based Enumeration 9 64
How to make Sign in, using Clientdataset? 1 19
shape, triangle, dbctrlgrid 3 5
The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

831 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