Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

wrong hignlight in TDBGrid

Posted on 1997-04-09
9
Medium Priority
?
412 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 200 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

604 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