Link to home
Start Free TrialLog in
Avatar of HabBoy
HabBoy

asked on

checking value of a cell in a TStringGrid

Hi,

I am new to Delphi programming so any help would be greatly appreciated.
I am looping through a TStringGrid checking the value of each cell. If there is no value I would like to remove the cell. I am having trouble with checking the value of the cell.
Each of the things I have tried below when in debug mode tell me that is an inaccessable value. Even if I let it run it does not evaluate properly.

If VarIsNull(Promotion_No_Grid.Cells[0,i - 1]) then...

If Length(Promotion_No_Grid.Cells[0,i - 1]) > 0 then...

If Trim(Promotion_No_Grid.Cells[0,i - l]) <> '' then...

What am I doing wrong?

Thanks,

HabBoy
Avatar of ginsonic
ginsonic
Flag of Romania image

I think that you use i:=0 and when use i-1 then get a -1 value.
If I'm right increase to 1.
Avatar of HabBoy
HabBoy

ASKER

No, that is not my problem, i is equal to 1 for the start of the loop. I can read the value in the grid(Promotion_No_Grid.Cells[0,i - 1]) no problem but when I put Lenght, Trim, VarIsNull or VarIsEmpty functions around the value it evaluates to inaccessable value.
> when in debug mode tell me that is an inaccessable value

It's compiler optimization. Go to:
Project -> Options -> Compiler, and turn optimization off.
Now try it.
This will reduce effect, but won't totally eliminate it.

You might also try this:

var s: String; // Dummy tmp var added
(....)
s:=Promotion_No_Grid.Cells[0,i - 1];
// Set a breakpoint on following line:
if Length(s)>0 then ....



> Even if I let it run it does not evaluate properly

Please post simple reproducible example for this one.
Avatar of kretzschmar
for y := 0 to stringgrid1.rowcount-1 do
  for x := 0 to stringrid1.colcount-1 do
    if trim(stringgrid1.cells[y,x]) = '' then
      do_your_stuff;

should work,
if i not mixed up the x,y

about inaccesable value,
may caused, if you want to evaluate the value,
when your loopvar isn't  initialized,
means you can only evaluate within the loop

meikl ;-)
Avatar of HabBoy

ASKER

No, that is not my problem, i is equal to 1 for the start of the loop. I can read the value in the grid(Promotion_No_Grid.Cells[0,i - 1]) no problem but when I put Lenght, Trim, VarIsNull or VarIsEmpty functions around the value it evaluates to inaccessable value.
>grid(Promotion_No_Grid.Cells[0,i - 1])

just asking about this notation,
what is grid()
(looks like a conversion)
HabBoy,

I don't quite follow you....

> ... value it evaluates to inaccessable value.
Are you asking "Why can I see some value during debbuging?"

Or, are you asking "Why is my code not *working* the way it's supposed to?" (runtime)
In another words, if you don't stop it (no breakpoints), does it produce expected results?
Avatar of HabBoy

ASKER

No, that is not my problem, i is equal to 1 for the start of the loop. I can read the value in the grid(Promotion_No_Grid.Cells[0,i - 1]) no problem but when I put Lenght, Trim, VarIsNull or VarIsEmpty functions around the value it evaluates to inaccessable value.
maybe you should show a bit code.

btw. you cannot remove a cell

meikl ;-)
Avatar of HabBoy

ASKER

During debugging if I strictly try and view the value in the current cell on the grid, I can see it.
However, if I put a trim or length function around the value like so: Trim(Promotion_No_Grid.Cells[0,i-1]) and I try and view this it tells me it is an inaccessible value.

Here is my code:
function TGenerate_Sweepstakes_Winners.Valid_Promotion: Boolean;
var i: integer;
var s: string;
begin
   result := false;

    for i := 1 to Promotion_No_Grid.RowCount do
        s:= Promotion_No_Grid.Cells[0,i-1];
        if Trim(s) <> '' then
          PROMOTIONS_.Close;
          PROMOTIONS_.SQL.Clear;
          PROMOTIONS_.SQL.Add ('SELECT promo_status_code, promo_start_date, promo_end_date, ');
          PROMOTIONS_.SQL.Add ('promo_desc_english ');
          PROMOTIONS_.SQL.Add ('FROM   promotion ');
          PROMOTIONS_.SQL.Add ('WHERE  promotion_number = ' + Promotion_No_Grid.Cells[0,i - 1]);
          PROMOTIONS_.Open;


If I put nothing or spaces in a cell in the grid it still falls into the sql, which obviously generates an error.
What am I doing wrong? Please help.

Thanks
ASKER CERTIFIED SOLUTION
Avatar of HabBoy
HabBoy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
well, just a silly mistake,
glad you got it sort out

in the meantime i've coded a little sample,
and i will post it here, even if it is never needed now

unit grid_cells_eval_u;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Grids;

type
  TForm1 = class(TForm)
    StringGrid1: TStringGrid;
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
var r, c : integer;
begin
  with stringgrid1 do
  begin
  //clear grid
   rowcount := fixedrows+1;
   colcount := fixedcols+1;
   rowcount := 10;
   colcount := 10;
  //fill up the grid
    for r := 0 + fixedrows to rowcount-1 do
      for c := 0 + fixedCols to colcount-1 do
        if (r*c) mod 3 <> 0 then   //left out some
          cells[r,c] := intToStr(trunc(random(1000)));

  end;

end;

procedure TForm1.Button2Click(Sender: TObject);
var r, c : integer;
begin
  //check cells
  with stringgrid1 do
  begin
    for r := 0 + fixedrows to rowcount-1 do
      for c := 0 + fixedCols to colcount-1 do
        if trim(cells[r,c]) = '' then
          cells[r,c] := 'was empty';
  end;
end;

end.

works properly

meikl ;-)


:))
HabBoy:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
HabBoy,
No comment has been added lately (18 days), so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:

RECOMMENDATION: Accept HabBoy's answer http:#7130274 and Refund

Please leave any comments here within 7 days.

-- Please DO NOT accept this comment as an answer ! --

Thanks,

anAKiN
EE Cleanup Volunteer