Solved

wrong hignlight in TDBGrid

Posted on 1997-04-09
9
376 Views
Last Modified: 2010-04-06
A TDBGrid with dgMultiSelect = True and is connected to a database.
In run time, the rows may be highlighted when they are clicked and the corresponding records are selected, no problem.

However, when you scroll the grid, a row at the top or the bottow may also be highlighted automatically but the corresponding record is NOT be selected! So, some highlighted rows related to the records you selected but one highlighted row related to nothing in the same time.

How to avoid that?
0
Comment
Question by:zhuhail
  • 7
9 Comments
 
LVL 2

Expert Comment

by:javiertb
ID: 1335262
I've tried what you said but I didn't see anything wrong. I've selected 3 rows and then scroll up and down but the 3 rows still were selected and only one else ,let's say selected (but not actually), it's the one where you are positioned in the table, but when you look for the selected ones via SelectedRows it isn't there.
0
 

Author Comment

by:zhuhail
ID: 1335263
Hi,

Let me give you more detail:

Delphi 2.0.
A TQuery + a TDBGrid (with dgMultiSelect = True).
They are connected to a database (the demo).
Bring all the records in the table of the database to the TDBGrid by SQL "%".
Click 3 rows (make sure they are far from the top or bottow) in the TDBGrid, 3 rows are highlighted and 3 corresponding records in the TQuery are selected.
  ("highlighted" means something happened in the TDBGrid.
   "selected" means something happened inside the TQuery.)
Roll one step up (or down) the TDBGrid, a row at the top (or bottow) will be highlighted automatically but its corresponding record in the TQuery is not been selected.
Now, 4 rows are highlighted but only 3 records are selected!
I want: Any row highlighted = the corresponding record selected.
0
 
LVL 7

Accepted Solution

by:
RBertora earned 100 total points
ID: 1335264
Hi there,

First of all I would like to say that I agree with the comment
from javiertb, I tried what you suggested and :

Three rows selected: note all the columns in the selected rows are highlighted.
Then scrolling down: only one column in bottom row is highlighted
but not selected... you can tell the difference between highlited
and selected.... so three highlited across all columns and one is
highlited only across first column...


Any way you wanted a better answer:

The answer involves a whole lot of hard work, which I started on
and gave up cause, geeze for 50 points, or a max of 4*50=200 if
you rate it superbly... its not worth the effort.. any way..
What you should do is set

  Dbgrid.DefaultDrawing = False

and then write your routines to handle the drawing, you can get most of the needed stuff in the source code DBgrids.pas.. it will look very hairy and messy something like this:


if I were you I would just settle for what is supported. :)

*** Yuck ***



procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect;
  DataCol: Integer; Column: TColumn; State: TGridDrawState);
var
  Value: string;
begin
  Value := '';
  if Assigned(Column.Field) then
    Value := Column.Field.DisplayText;
  WriteText(Canvas, Rect, 0, 0, Value, Column.Alignment);
end;

procedure WriteText(ACanvas: TCanvas; ARect: TRect; DX, DY: Integer;
  const Text: string; Alignment: TAlignment);
const
  AlignFlags : array [TAlignment] of Integer =
    ( DT_LEFT or DT_WORDBREAK or DT_EXPANDTABS or DT_NOPREFIX,
      DT_RIGHT or DT_WORDBREAK or DT_EXPANDTABS or DT_NOPREFIX,
      DT_CENTER or DT_WORDBREAK or DT_EXPANDTABS or DT_NOPREFIX );
var
  B, R: TRect;
  I, Left: Integer;
begin
  I := ColorToRGB(ACanvas.Brush.Color);
  if GetNearestColor(ACanvas.Handle, I) = I then
  begin                       { Use ExtTextOut for solid colors }
    case Alignment of
      taLeftJustify:
        Left := ARect.Left + DX;
      taRightJustify:
        Left := ARect.Right - ACanvas.TextWidth(Text) - 3;
    else { taCenter }
      Left := ARect.Left + (ARect.Right - ARect.Left) shr 1
        - (ACanvas.TextWidth(Text) shr 1);
    end;
    ExtTextOut(ACanvas.Handle, Left, ARect.Top + DY, ETO_OPAQUE or
      ETO_CLIPPED, @ARect, PChar(Text), Length(Text), nil);
  end
  else begin                  { Use FillRect and Drawtext for dithered colors }
    with DrawBitmap, ARect do { Use offscreen bitmap to eliminate flicker and }
    begin                     { brush origin tics in painting / scrolling.    }
      Width := Max(Width, Right - Left);
      Height := Max(Height, Bottom - Top);
      R := Rect(DX, DY, Right - Left - 1, Bottom - Top - 1);
      B := Rect(0, 0, Right - Left, Bottom - Top);
    end;
    with DrawBitmap.Canvas do
    begin
      Font := ACanvas.Font;
      Font.Color := ACanvas.Font.Color;
      Brush := ACanvas.Brush;
      Brush.Style := bsSolid;
      FillRect(B);
      SetBkMode(Handle, TRANSPARENT);
      DrawText(Handle, PChar(Text), Length(Text), R, AlignFlags[Alignment]);
    end;
    ACanvas.CopyRect(ARect, DrawBitmap.Canvas, B);
  end;
end;




procedure TForm1.DBGrid1DrawDataCell(Sender: TObject; const Rect: TRect;
  Field: TField; State: TGridDrawState);
var
  Alignment: TAlignment;
  Value: string;
begin
  Alignment := taLeftJustify;
  Value := '';
  if Assigned(Field) then
  begin
    Alignment := Field.Alignment;
    Value := Field.DisplayText;
  end;
  WriteText(Canvas, Rect, 0, 0, Value, Alignment);
end;

Cheers,
Rob.







0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:zhuhail
ID: 1335265
Adjusted points to 70
0
 

Author Comment

by:zhuhail
ID: 1335266
Adjusted points to 75
0
 

Author Comment

by:zhuhail
ID: 1335267
Hi Rob,

Yes, that is what I need! Thank you very much.
I didn't expect the problem would be so difficult, sorry....The point is increasing :-)

Perhaps I missed something, when run your code, I got some error messages which are listed (with corresponding statements) as forllows:

with DrawBitmap, ARect do
     Undeclared identitier: 'DrawBitmap'.

FillRect(B);
     Incompatible types: 'Integer' and 'TRect'.

SetBkMode(Handle, TRANSPAPENT);
     Undeclared identifier: 'Handle'.

ACanvas.CapyRect(ARect, DrawBitmap.Canvas, B);
     Not enough actual parameters.

ACanvas.CapyRect(ARect, DrawBitmap.Canvas, B);
end;
end;
     '.' expected but ';'found. (reffers to the last line)
     
Can you help?

Regards.

0
 

Author Comment

by:zhuhail
ID: 1335268
Adjusted points to 85
0
 

Author Comment

by:zhuhail
ID: 1335269
Adjusted points to 90
0
 

Author Comment

by:zhuhail
ID: 1335270
Adjusted points to 100
0

Featured Post

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Suggested Solutions

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

770 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