Solved

wrong hignlight in TDBGrid

Posted on 1997-04-09
9
382 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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
how to center only a line in richedit? 4 61
Delphi XE10 Round Image 2 154
Dev Express grid collapse 2 45
tidtcpserver connection lost handle 2 100
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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

790 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