Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

TDBGrid.SelectedRows returning invalid bookmarks

840 views
Skip to first unread message

Magno Machado

unread,
Feb 28, 2008, 6:53:52 AM2/28/08
to
I'm having a strange bug in my application.
There is a situation when I need to save all entries of the SelectedRows
property of a dbgrid, and afterwards I have to apply each of these
entries(which are actually bookmarks from the dbgrid's dataset) as a
bookmark on the dataset which is linked to that dbgrid. But when I do this,
an exception is thrown because the bookmark isn't valid on the dataset(!!!).
Using the debugger, when I was about to read the SelectedRows property, I
had gone to the Evaluate & Modify and evaluated the following expression:
DBGrid.DataSource.DataSet.BookmarkValid(Pointer(DBGrid.SelectedRows[0])) and
the result was: False.
How can it be possible? It's certain that there was at least one element in
the DBGrid.SelectedRows. If not, an exception would be thrown. And I think
all the bookmarks returned by the SelectedRows should be valid bookmarks on
the dataset which the dbgrid is linked on, shouldn't it? Based on this, how
could the above expression evaluate to False?

Marc Rohloff [TeamB]

unread,
Feb 28, 2008, 9:09:10 AM2/28/08
to

A dataset supports two types of bookmarks:
TBookMark returned by the GetBookmark function and a TBookMarkStr
returned by the Bookmark property. IIRC, the two are not
interchangeable.
BookMarkValid requires a TBookMark and DBGrid.SelectedRows returns a
list of TBookMarkStr's.

--
Marc Rohloff [TeamB]
marc -at- marc rohloff -dot- com

Magno Machado

unread,
Feb 28, 2008, 10:13:51 AM2/28/08
to
But the bookmark returned by DBGrid.SelectedRows is compatible with
TDataSet.Bookmark property?

My problem is that an exception is thrown when I try to assign a bookmark
returned by DBGrid.SelectedRows to the bookmark property of the dataset.

Magno Machado

unread,
Feb 28, 2008, 10:15:11 AM2/28/08
to
Just for curiosity: Why 2 kinds of bookmarks? Why not only one kind?

Marc Rohloff [TeamB]

unread,
Feb 28, 2008, 12:17:26 PM2/28/08
to
On Thu, 28 Feb 2008 12:15:11 -0300, Magno Machado wrote:

> Just for curiosity: Why 2 kinds of bookmarks? Why not only one kind?

The string type was introduced later, I assume either because it was
simpler or because the change to reference counted strings in Delphi 2
made it more viable.

I don't know why you are getting an AV. I would try debugging into the
RTL code.

Bill Todd [TeamB]

unread,
Feb 28, 2008, 12:33:42 PM2/28/08
to
From the D7 help file. Note the cast to type Pointer.

The following example copies the selected rows in a db grid to a list
box.

procedure TForm1.Button1Click(Sender: TObject);
var
i, j: Integer;
s: string;
begin
if DBGrid1.SelectedRows.Count>0 then
with DBGrid1.DataSource.DataSet do
for i:=0 to DBGrid1.SelectedRows.Count-1 do
begin
GotoBookmark(pointer(DBGrid1.SelectedRows.Items[i]));
for j := 0 to FieldCount-1 do
begin

if (j>0) then s:=s+', ';
s:=s+Fields[j].AsString;
end;
Listbox1.Items.Add(s);
s:= '';
end;
end;

--
Bill Todd (TeamB)

Magno Machado

unread,
Feb 28, 2008, 12:50:43 PM2/28/08
to
>I don't know why you are getting an AV
I'm not getting an AV, I'm getting an EDatabaseError with the message
'Record not found'

Magno Machado

unread,
Feb 28, 2008, 1:25:16 PM2/28/08
to
I managed to solve the problem.
The problem is that I had an record selected on the grid and I have deleted
it. I thought the DBGrid.SelectedRows would be opdated to remove this
record's bookmark, but it wasn't.
After delete the record, as I thought the SelectedRows was pointing to the
current record on the dataset, so I tried to use it to apply on the dataset
bookmark property, so it raised an exception because the bookmark I was
using was pointing to a record that wasn't on the dataset.

Thanks for both Marc and Bill

0 new messages