Solved

checking value of a cell in a TStringGrid

Posted on 2002-07-04
17
552 Views
Last Modified: 2010-04-04
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
Comment
Question by:HabBoy
  • 5
  • 4
  • 3
  • +3
17 Comments
 
LVL 9

Expert Comment

by:ginsonic
ID: 7129720
I think that you use i:=0 and when use i-1 then get a -1 value.
0
 
LVL 9

Expert Comment

by:ginsonic
ID: 7129725
If I'm right increase to 1.
0
 

Author Comment

by:HabBoy
ID: 7129743
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
 
LVL 7

Expert Comment

by:Cynna
ID: 7129756
> 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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7129779
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
 

Author Comment

by:HabBoy
ID: 7130001
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7130017
>grid(Promotion_No_Grid.Cells[0,i - 1])

just asking about this notation,
what is grid()
(looks like a conversion)
0
 
LVL 7

Expert Comment

by:Cynna
ID: 7130210
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:HabBoy
ID: 7130231
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7130254
maybe you should show a bit code.

btw. you cannot remove a cell

meikl ;-)
0
 

Author Comment

by:HabBoy
ID: 7130263
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
 

Accepted Solution

by:
HabBoy earned 0 total points
ID: 7130274
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
 
LVL 27

Expert Comment

by:kretzschmar
ID: 7130287
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
 
LVL 7

Expert Comment

by:Cynna
ID: 7130318
:))
0
 

Expert Comment

by:CleanupPing
ID: 9343106
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
 
LVL 5

Expert Comment

by:Lukasz Lach
ID: 9460159
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

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This is a video describing the growing solar energy use in Utah. This is a topic that greatly interests me and so I decided to produce a video about it.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

939 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now