Recent

Author Topic: DBGrid onDrawColumnCell Scroll up messes order of records  (Read 3411 times)

Jkey

  • New Member
  • *
  • Posts: 44
DBGrid onDrawColumnCell Scroll up messes order of records
« on: February 09, 2017, 04:12:21 pm »
Hello,
I use DBGrid's onDrawColumnCell event to show row numbers there:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.DBGrid1DrawColumnCell(Sender: TObject; const Rect: TRect; DataCol: Integer; Column: TColumn; State: TGridDrawState);
  2. begin
  3.   if (DataCol = 0) then
  4.     DBGrid1.Canvas.TextOut(Rect.Left + 2, Rect.Top, IntToStr(DBGrid1.DataSource.DataSet.RecNo));
  5. end;
At a first glance it seems to work, but when I scroll down the DBGrid, and start to scroll it up by either mouse wheel or arrow up key of keyboard, it won't go up and starts repeating records strangely from lower sections.
You can find attached a very small zip packed demo project to be able to reproduce the issue. I was able to reproduce it using Lazarus 1.6.2 on both Windows 7 x86 and Ubuntu x64. Demo contains a simple SQLite database as source of data to display.
If I comment out the contents of onDrawColumnCell event, DBGrid will start behaving normally again, but I would loose the rowIDs.
  • Is there any other way to show RowIDs in DBGrid?
  • Or do you have ideas, how I can solve the strange issue of scrolling up?
Thank you in advance.

Jkey

  • New Member
  • *
  • Posts: 44
Re: DBGrid onDrawColumnCell Scroll up messes order of records
« Reply #1 on: February 09, 2017, 10:08:03 pm »
Anyone who face with this issue in the future, here is my workaround:
Create a dummy SQLQuery.BeforeScroll event and read the value of Dataset.RecNo. Then do nothing with it, and that's all. Since then both dataset scrolling and DBGrid onDrawColumnCell with displaying row numbers work as expected.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.SQLQuery1BeforeScroll(DataSet: TDataSet);
  2. var  i: longint;
  3. begin
  4.   i := DataSet.RecNo;
  5. end;

marcio2003

  • Jr. Member
  • **
  • Posts: 69
Re: DBGrid onDrawColumnCell Scroll up messes order of records
« Reply #2 on: February 09, 2017, 11:21:35 pm »
I think you want use fixed column(zero) to go numbering the lines. Is it?
Lazarus 2.0.10 Windows 10 64bits

Jkey

  • New Member
  • *
  • Posts: 44
Re: DBGrid onDrawColumnCell Scroll up messes order of records
« Reply #3 on: February 10, 2017, 09:26:51 am »
Yes.
My provided code (in my first post) can do it. But when I scrolled my DBGrid (mainly to up direction), records used to get mixed strangely.
Declaring a dummy BeforeScroll event (my second post) fortunately somehow prevents this issue to happen, and everything is OK.

 

TinyPortal © 2005-2018