Solved

wrong hignlight in TDBGrid

Posted on 1997-04-09
9
370 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

896 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

14 Experts available now in Live!

Get 1:1 Help Now