Board index » delphi » property Cells[a,b] to DBGrid ?

property Cells[a,b] to DBGrid ?

I need know a value of a cell[a,b] in DBGrid.
how?

thank you!

--
PpSoft
aatwebmas...@arrakis.es

 

Re:property Cells[a,b] to DBGrid ?


Quote
"PpSoft" <aatwebmas...@arrakis.es> wrote:

> I need know a value of a cell[a,b] in DBGrid.
> how?

Here's one straight out of some 'Publics' unit I made and append to my projects, just to give an
idea:

function GetCellValue(TheGrid: TDBGrid; RowIndex, ColumnIndex: integer; AbsPos: boolean): string;
var
  TheBookmark    : TBookmark;
begin
  with TDatasource(TheGrid.Datasource).Dataset do
    begin
      { At least, inhibit scrolling while moving to the cell }
      DisableControls;
      { save current position }
      TheBookmark := GetBookmark;
      if not AbsPos then
        begin
          { set row,column relative to selected field }
          RowIndex := GetRowNumber(TDatasource(TheGrid.Datasource).Dataset) + RowIndex;
          ColumnIndex := TheGrid.SelectedIndex + ColumnIndex;
        end
      else
        { row,column are absolute }
        Dec(ColumnIndex);
      try
        begin
          First;
          if ( MoveBy(RowIndex-1) < (RowIndex-1) )
          or ( TheGrid.FieldCount <= ColumnIndex )
          or ( RowIndex < 1 )
          or ( ColumnIndex < 0 ) then
            { inhibit 'List index out of bounds' }
            Result := 'Cell not found'
          else
            Result := Fields[ColumnIndex].AsString;
        end;
      finally { make sure we always get back to original position }
        GotoBookmark(TheBookmark);
        FreeBookMark(TheBookmark);
        EnableControls;
      end;
    end;
end;

function GetRowNumber(TheDataset: TDataset): integer;
var
  TheBookmark : TBookmark;
begin
  with TheDataset do
    begin
      { inhibit scrolling... }
      DisableControls;
      TheBookmark := GetBookmark;
      try
        Result := Abs(MoveBy(-MAXLONGINT))+1;
      finally
        { always go back to original position }
        GotoBookmark(TheBookmark);
        FreeBookmark(TheBookmark);
        EnableControls;
      end;
    end;
end;

It should work with Delphi 1 or 2 (in the last case, I suppose you could use Bookmark in place of
the GetBookmark, GotoBookmark and FreeBookmark calls), is 1-based (the upper left cell is 1,1) and
should give correct results for whatever (r)dbms (that's what the GetRowNumber function is for),
provided you don't have more rows in the result set than MaxLongInt (currently 2,147,483,647) :-))
But then, if you have more, you'd have some other problem(s). Anyway, I tested against Paradox,
Interbase and Oracle.

There's one {*word*193} behaviour however: the grid repositiones itself as to show the original selection
'vertically centered'. I still have to work on that (TopRow being protected and giving... the
column). And yes, I know, that should be part of a derived TmyDBGrid. Every single day, I say to
myself: "I'll do that. Tomorrow..." :-)

Hope it helps.

Jasper

Re:property Cells[a,b] to DBGrid ?


Hi!

The value of the cell at any position is the value of the
underlying field.  You can use FieldByName().

HTH

Ken
--
Ken White
kwh...@westelcom.com
qpwpla...@aol.com

Clipper Functions 3.0 for Delphi
http://members.aol.com/clipfunc

Quote
PpSoft wrote:

> I need know a value of a cell[a,b] in DBGrid.
> how?

> thank you!

> --
> PpSoft
> aatwebmas...@arrakis.es

Other Threads