Board index » delphi » Moving selected record to top of grid

Moving selected record to top of grid

Hi,
  I am trying to move a selected record to the top of a DBGrid. Does anyone
know how to do it.

Nadav.

 

Re:Moving selected record to top of grid


Not tested:

  s := Table1.<yourkeyfieldname>.Value;
  with Table1 do
  begin
    DisableControls;
    First;
    FindKey([s]);
    EnableControls;
  end;

Paul / ColumbuSoft

Quote
Nadav <na...@svivot.com> wrote in message

news:889dhd$i8e2@bornews.borland.com...
Quote
> Hi,
>   I am trying to move a selected record to the top of a DBGrid. Does
anyone
> know how to do it.

> Nadav.

Re:Moving selected record to top of grid


Quote
On Mon, 14 Feb 2000 19:18:45 +0200, "Nadav" <na...@svivot.com> wrote:
>  I am trying to move a selected record to the top of a DBGrid. Does anyone
>know how to do it.

Change the index key for that record to something smaller than any
other record.

Jan

Re:Moving selected record to top of grid


I'm sorry but that doesn't make sense.  #1, the table may not be indexed or
have an index active.  #2, the index might be something like 'customer id.'

I could have misread what he wanted but it sounded to me like he just wanted
the hightlighted record moved to the first visible record in the grid.

Paul / ColumbuSoft

Quote
Jan Sprengers <j...@nospamplease.adm2000.be> wrote in message

news:38a91e84.586436070@forums.inprise.com...
Quote
> On Mon, 14 Feb 2000 19:18:45 +0200, "Nadav" <na...@svivot.com> wrote:

> >  I am trying to move a selected record to the top of a DBGrid. Does
anyone
> >know how to do it.

> Change the index key for that record to something smaller than any
> other record.

> Jan

Re:Moving selected record to top of grid


On Tue, 15 Feb 2000 06:22:46 -0500, "Paul Ferrara"

Quote
<p...@nospam.columbusoft.com> wrote:
>I could have misread what he wanted but it sounded to me like he just wanted
>the hightlighted record moved to the first visible record in the grid.

If that's what he wants, the solution is a bit more complicated.

Here is a method that I use in a subclassed DBGrid.  You can move to
the top row with:
   MoveToRow(TopRow);

procedure TMyDBGrid.MoveToRow(NewRow: Integer);
// Scrolls the visible records so that the current record is shown
// on the NewRow position (if possible)
var
  Mark: TBookmarkStr;
begin
  Mark := DataLink.DataSet.Bookmark;
  // Set the current row to NewRow
  // (this also moves the record pointer)
  DataLink.ActiveRecord := NewRow;
  // Goto the original record
  DbiSetToBookmark(TDBDataSet(DataLink.DataSet).Handle,
    Pointer(Mark));
  // Force a reread of the record buffer with the current settings
  DataLink.DataSet.Resync([rmExact]);
end;

Row, TopRow and DataLink are protected properties of DBGrid, so they
should be used in a DBGrid subclass.

Jan

Re:Moving selected record to top of grid


Handy solution.

Paul / ColumbuSoft

Quote
Jan Sprengers <j...@nospamplease.adm2000.be> wrote in message

news:38a94b20.597856392@forums.inprise.com...
Quote
> On Tue, 15 Feb 2000 06:22:46 -0500, "Paul Ferrara"
> <p...@nospam.columbusoft.com> wrote:

> >I could have misread what he wanted but it sounded to me like he just
wanted
> >the hightlighted record moved to the first visible record in the grid.

> If that's what he wants, the solution is a bit more complicated.

> Here is a method that I use in a subclassed DBGrid.  You can move to
> the top row with:
>    MoveToRow(TopRow);

> procedure TMyDBGrid.MoveToRow(NewRow: Integer);
> // Scrolls the visible records so that the current record is shown
> // on the NewRow position (if possible)
> var
>   Mark: TBookmarkStr;
> begin
>   Mark := DataLink.DataSet.Bookmark;
>   // Set the current row to NewRow
>   // (this also moves the record pointer)
>   DataLink.ActiveRecord := NewRow;
>   // Goto the original record
>   DbiSetToBookmark(TDBDataSet(DataLink.DataSet).Handle,
>     Pointer(Mark));
>   // Force a reread of the record buffer with the current settings
>   DataLink.DataSet.Resync([rmExact]);
> end;

> Row, TopRow and DataLink are protected properties of DBGrid, so they
> should be used in a DBGrid subclass.

> Jan

Re:Moving selected record to top of grid


Hi Jan,
this is a very interesting procedure.
My problem with this: I haven't used Subclasses.
Can you give a more detailed Sample to do this?

Many Thanks
Kurt

Re:Moving selected record to top of grid


On Wed, 16 Feb 2000 16:18:01 +0100, "Kurt Mller"

Quote
<kurt-muel...@gmx.de> wrote:
>Can you give a more detailed Sample to do this?

Delphi makes creating custom components easy.  Here is how you
proceed:

- click on New Component in the Component menu of Delphi
- ancestor type = TDBGrid; class name = TMyDBGrid (for example)
- click OK
- add "DB, DBTables, BDE" to the uses clause
- put this code in the 'public' section in the interface part:

    procedure MoveToRow(NewRow: Integer);
    property Row;

- put this code under 'implementation':

procedure TMyDBGrid.MoveToRow(NewRow: Integer);
// Scrolls the visible records so that the current record is shown
// on the NewRow position (if possible)
var
  Mark: TBookmarkStr;
begin
  Mark := DataLink.DataSet.Bookmark;
  // Set the current row to NewRow
  // (this also moves the record pointer)
  DataLink.ActiveRecord := NewRow;
  // Goto the original record
  DbiSetToBookmark(TDBDataSet(DataLink.DataSet).Handle,
    Pointer(Mark));
  // Force a reread of the record buffer with the current settings
  DataLink.DataSet.Resync([rmExact]);
end;

- now you can use Install Component in the Component menu to create a
package to contain this component or to add to an existing package.

When you select this component from the component palette, you can now
save the current row with the Row property to move back to that grid
line with MoveToRow() after an operation on the dataset, or you can go
to the first line with MoveToRow(0).

HTH,

Jan

Other Threads