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_Gri d.Cells[0, i - 1]) then...
If Length(Promotion_No_Grid.C ells[0,i - 1]) > 0 then...
If Trim(Promotion_No_Grid.Cel ls[0,i - l]) <> '' then...
What am I doing wrong?
Thanks,
HabBoy
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_Gri
If Length(Promotion_No_Grid.C
If Trim(Promotion_No_Grid.Cel
What am I doing wrong?
Thanks,
HabBoy
I think that you use i:=0 and when use i-1 then get a -1 value.
If I'm right increase to 1.
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.Cel ls[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.
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
// 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.
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 ;-)
for x := 0 to stringrid1.colcount-1 do
if trim(stringgrid1.cells[y,x
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 ;-)
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.Cel ls[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.Ce lls[0,i - 1])
just asking about this notation,
what is grid()
(looks like a conversion)
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?
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?
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.Cel ls[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 ;-)
btw. you cannot remove a cell
meikl ;-)
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.Cel ls[0,i-1]) and I try and view this it tells me it is an inaccessible value.
Here is my code:
function TGenerate_Sweepstakes_Winn ers.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
However, if I put a trim or length function around the value like so: Trim(Promotion_No_Grid.Cel
Here is my code:
function TGenerate_Sweepstakes_Winn
var i: integer;
var s: string;
begin
result := false;
for i := 1 to Promotion_No_Grid.RowCount
s:= Promotion_No_Grid.Cells[0,
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,
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 ;-)
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
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
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.
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
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