Page 1 of 1

Setting TMyQuery IndexFieldNames>"" changes current record

Posted: Tue 27 Jan 2009 20:06
by NoComprende
I'm using c++ builder (rad studio 2009). I'm finding that if I have a TMyQuery Q showing in a TDBGrid with
Q->IndexFieldNames="" and I click a button that programatically sets Q->IndexFieldNames="SomeField" then the current record is not maintained. The new top record becomes the current record but the AfterScroll event is not even triggered despite the change of record.

It only seems to happen if it's the first thing I do when the programme opens and the grid appears. If I scroll the grid before clicking the sort button the record indicator follows the current record to its new position after sorting. Subsequent sorts made on other fields also work as they should.

When sorting is working as it should the current record is often not 'centred' (as happens with the Locate method) after sorting.

Posted: Wed 28 Jan 2009 11:26
by NoComprende
The following code makes sure the record position is maintained and the record is centred

TBookmark Mark=Q->GetBookmark();
Q->IndexFieldNames="SomeField";
Q->GotoBookmark(Mark);
Q->FreeBookmark(Mark);

Also, GotoBookmark results in AfterScroll being called.

Posted: Wed 28 Jan 2009 15:20
by Dimon
Was the problem solved?

Posted: Wed 28 Jan 2009 16:22
by NoComprende
Depends what you mean by solved Dimon. The second post is a workaround but the bug's still there (whether it's down to MyDAC or the borland TDBGrid I don't know).

Posted: Fri 30 Jan 2009 10:14
by Dimon
The point is that a just opened table stays in BOF state. Therefore when you set the IndexFieldNames property of this table, the cursor remains on the first record.
In order to solve this problem you should set the first record in the dataset after its opening, like this:

Code: Select all

Q->Open();
Q->First();

Posted: Sat 31 Jan 2009 09:50
by NoComprende
Thanks Dimon, I'll try it out later.