Solved

wrong hignlight in TDBGrid

Posted on 1997-04-09
9
362 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
 

Author Comment

by:zhuhail
ID: 1335265
Adjusted points to 70
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

708 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

12 Experts available now in Live!

Get 1:1 Help Now