Subject Re: [IBO] IB_Grid and selected rows
Author Geoff Worboys
> I know how to select several records in IB_Grid, but I'd like to
> know how I can process them (in standard DBGrid there is property
> SelectedRows and I didn't found anything similar in IB_Grid).

When you select records on IB_Grid the selected status is kept with
the record in the dataset.

You use the IB_Query.Selected[] property to get/set the selection
state of a particular row number. To use this in conjunction with
IB_Grid use the grid DataRow and GridRow properties of IB_Grid.

See also the IB_Query methods: SelectAll, SelectRange etc. In
particular SelectedBookmarks is very usful for processing all selected
rows. eg.

var
TmpStrs: TStringList;
i: integer;
begin
TmpStrs := TStringList.Create;
try
IB_Query.SelectedBookmarks( TmpStrs );
for i := 0 to TmpStrs.Count - 1 do
begin
IB_Query.BufferBookmark := TmpStrs[i];
// Now do something with the row selected by
// using the various IB_Query.Buffer* properties
end;
finally
TmpStrs.Free;
end;
end;


> Is there any way to make certain row "current row" in IB_Grid
> clicking on fixed cells in the grid? The perfect solutionwould
> be having a small checkbox in the first column of the grid and
> making the row selected simply clicking the checkbox.

Not sure exactly what you mean here. The user can make any row the
current row by clicking on a cell in that row. IB_Grid will show
which rows are selected using the IB_Session.SelectedColor.

In my applications I use an additional border column and show a small
glyph to indicate the selected state - allowing the selected state to
still show even on the focused row.

Here are some snippets from one of my applications...

- Grid1 is setup with BorderCols = 1 so that I have an extra border
column to draw and click on.

- ImageList1 contains the images I want to draw on the border to
indicate selected or not selected. The code below selects the
required image (0 or 1) to display on the border and draws the
required image.

procedure TForm1.Grid1DrawBorder(Sender: TObject; ACol,
ARow: Integer; Rect: TRect; State: TGridDrawState);
var
i, j, k: integer;
begin
if (ARow >= Grid1.FixedRows) then
with IB_Query1 do
begin
k := -1;
i := ((Rect.Right - Rect.Left) - ImageList1.Width) div 2;
j := ((Rect.Bottom - Rect.Top) - ImageList1.Height) div 2;
if Selected[Grid1.DataRow[ARow]] then
k := 0;
else k := 1;
if k >= 0 then
ImageList1.Draw( Grid1.Canvas, Rect.Left + i,
Rect.Top + j, k, true );
end;
end;


- The following handler toggles the selected status when the user
clicks on the border column.

procedure TForm1.Grid1BorderClick(Sender: TObject; ACol,
ARow: Integer; AButton: TMouseButton; AShift: TShiftState);
var
i: integer;
begin
if (ARow >= Grid1.FixedRows) then
with IB_Query1 do
begin
i := Grid1.DataRow[ARow];
if Selected[i] then
Selected[i] := false
else
Selected[i] := true;
end;
end;


Not sure if this is what you had in mind or not, but perhaps it gives
you some ideas.

Geoff Worboys
Telesis Computing