Solved

checking value of a cell in a TStringGrid

Posted on 2002-07-04
17
553 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
Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

 
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
 

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

773 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