Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 576
  • Last Modified:

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
0
HabBoy
Asked:
HabBoy
  • 5
  • 4
  • 3
  • +3
1 Solution
 
ginsonicCommented:
I think that you use i:=0 and when use i-1 then get a -1 value.
0
 
ginsonicCommented:
If I'm right increase to 1.
0
 
HabBoyAuthor Commented:
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.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
CynnaCommented:
> 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.
0
 
kretzschmarCommented:
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 ;-)
0
 
HabBoyAuthor Commented:
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.
0
 
kretzschmarCommented:
>grid(Promotion_No_Grid.Cells[0,i - 1])

just asking about this notation,
what is grid()
(looks like a conversion)
0
 
CynnaCommented:
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?
0
 
HabBoyAuthor Commented:
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.
0
 
kretzschmarCommented:
maybe you should show a bit code.

btw. you cannot remove a cell

meikl ;-)
0
 
HabBoyAuthor Commented:
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
0
 
HabBoyAuthor Commented:
I just realized what I was doing wrong. I forgot to put the begin and end reserved words in my if statement.

Sorry, I am an idiot!

I have many more stupid questions but will save them for another day.

Thanks for your input.

HabBoy

0
 
kretzschmarCommented:
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 ;-)


0
 
CynnaCommented:
:))
0
 
CleanupPingCommented:
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.
0
 
Lukasz LachCommented:
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now