Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > DB-Aware

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 01-21-2002, 01:27 AM
antony antony is offline
Junior Member
 
Join Date: Jan 2002
Posts: 12
Default Change color in DBGrid without use data to decide??

I want to fix the display color in DBGrid with two colors,like one is white and the other is red.
I use 'RecNo mod 2 = 0' to decide but failed,the RecNo's value is '-1'?! How can I reach the purpose? Thanks!!
Reply With Quote
  #2  
Old 01-21-2002, 01:30 AM
Ted On The NeT Ted On The NeT is offline
Senior Member
 
Join Date: Apr 2001
Posts: 2,182
Default RE: Change color in DBGrid without use data to decide??

do you want to color columns or lines?

<tt>
Greetz,
::TeD On The NeT::

=[ QUESTIONS ]=========================================
don't forget to accept this reply if it helped you!
================================================== =====

=[ EXTRA INFO ]========================================
Location : Netherlands (GMT +01.00)
Languages : English, Dutch, German
Age : 25
ICQ : Be smart and figure out what my UIN is...
================================================== =====
</tt>
Reply With Quote
  #3  
Old 01-21-2002, 02:02 AM
antony antony is offline
Junior Member
 
Join Date: Jan 2002
Posts: 12
Default RE: Change color in DBGrid without use data to decide??

color lines
Reply With Quote
  #4  
Old 01-21-2002, 04:19 AM
Ted On The NeT Ted On The NeT is offline
Senior Member
 
Join Date: Apr 2001
Posts: 2,182
Default RE: Change color in DBGrid without use data to decide??

Okay, this can be done, but my way is kinda tricky! but it's the only way it can be done, i guess...

First, add <cr>type RowNumber = class(TCustomDBGrid);</cr>
In the following code, i've used red text to mark the spot where you use the upper type definition.

In the OnDrawColumnCell event, add this code:
<cb>procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject;
const Rect: TRect; DataCol: Integer; Column: TColumn;
State: TGridDrawState);
begin
with grid1 do
begin
with <cr>RowNumber(Sender)</cr> do // use the Type
if <cr>odd(DataLink.ActiveRecord)</cr> then // is active record (row) odd?
Canvas.Brush.Color := clYellow
else
Canvas.Brush.Color := clAqua;
DefaultDrawColumnCell(Rect, Datacol, Column, State);
end;
end;
</cb>

Well, that's it.. you'll 2 colored lines. even if you delete records, the odd lines will be yellow, and the even ones Aqua. give it a try, let me know if it doesn't work. if it does, accept my answer

<tt>
Greetz,
::TeD On The NeT::

=[ QUESTIONS ]=========================================
don't forget to accept this reply if it helped you!
================================================== =====

=[ EXTRA INFO ]========================================
Location : Netherlands (GMT +01.00)
Languages : English, Dutch, German
Age : 25
ICQ : Be smart and figure out what my UIN is...
================================================== =====
</tt>
Reply With Quote
  #5  
Old 01-21-2002, 06:55 PM
antony antony is offline
Junior Member
 
Join Date: Jan 2002
Posts: 12
Default RE: Change color in DBGrid without use data to decide??

Thank you for your answer.
But the other problem happened when I press scroll bar,the
color not changed immediately. Then I click the line and color change to correct color.I think I need to trigger
OnDrawColumnCell event in OnMouseDown or other events, Is it right? And how to do it?
In other case,the record is two in one group.So I need to fix two lines with one color.Can you help me again? Because I'm a new developer in Delphi,and anyway thank you for your
help again!!
Reply With Quote
  #6  
Old 01-21-2002, 07:49 PM
Ted On The NeT Ted On The NeT is offline
Senior Member
 
Join Date: Apr 2001
Posts: 2,182
Default RE: Change color in DBGrid without use data to decide??

Try this, maybe it works in your case:

<cb>procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
DataCol: Integer; Column: TColumn; State: TGridDrawState);
begin
with dbgrid1 do
begin
with RowNumber(Sender) do
begin
SetRoundMode(rmDown);
if odd(Round(DataLink.ActiveRecord / 2)) then
Canvas.Brush.Color := clYellow
else
Canvas.Brush.Color := clAqua;
if gdSelected in state then
Canvas.Brush.Color := clBlue;
DefaultDrawColumnCell(Rect, Datacol, Column, State);
end;
end;
end;</cb>

<tt>
Greetz,
::TeD On The NeT::

=[ QUESTIONS ]=========================================
don't forget to accept this reply if it helped you!
================================================== =====

=[ EXTRA INFO ]========================================
Location : Netherlands (GMT +01.00)
Languages : English, Dutch, German
Age : 25
ICQ : Be smart and figure out what my UIN is...
================================================== =====
</tt>
Reply With Quote
  #7  
Old 01-22-2002, 10:37 PM
antony antony is offline
Junior Member
 
Join Date: Jan 2002
Posts: 12
Default RE: Change color in DBGrid without use data to decide??

It works OK,but just first time.
When I execute the same command again(I did't change any thing,just click the query button again).Terrible things happened.<cb>1:</cb>the display of DBGrid is very strange,there has no data in some columns,like this <cb>101101101101101101101101</cb>
<cr>0:means this column has no data</cr>,then I check the query result in my temporary table,is the same with DBGrid.
<cb>2:</cb>the report in the preview screen has a slope about 30 angle.
If remove the statement that you post,it's OK!
I trace all day,but still can't find what's reason caused?
<cr>ps:I use wwDBGrid(InfoPower 3000 component for D6),and write statement in OnDrawDataCell event,
there is no OnDrawColumnCell event of this component</cr>
I need your help again,thank you very much!!
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 11:56 PM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2014, vBulletin Solutions, Inc.