Recent

Author Topic: TstringGrid Cells Data Entry  (Read 2248 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1293
TstringGrid Cells Data Entry
« on: January 19, 2019, 06:38:54 pm »
Trying to add a row and populate with data.

The TStringGrid has 1 fixed Column and 1 fixed Row. It has 0 custom rows at startup. Therefor; I add a row get a row count subtract 1 and I'm trying to place an item into Cells(0,1) i.e. Cells[Col,Row]:= IntToStr(aIdx);

 Under the debugger Lines 10, 11 and 12 set Col and Rows and at Line 13 Col = 0, Row = 1.  The aIdx is loaded; however it's in row 1 and column 1. Not in the fixed Column. The cemented line 15 works perfectly.

I missing something somewhere.

Code: Pascal  [Select][+][-]
  1.  procedure TbtnPath.LoadG1300Grid(aIdx  : Integer;
  2.                                   aRCId : String;
  3.                                   aLine : String);
  4.   Var
  5.    i   : Integer = 0;
  6.    Row : Integer = 0;
  7.    Col : Integer = 0;
  8.  Begin
  9.   G1300Grid.RowCount := G1300Grid.RowCount +1;   // Add a new row
  10.   Row := G1300Grid.RowCount;                                 // Get Row Count = 2
  11.   Dec(Row);                                                             // Zero Base so Dec to 1
  12.   Col := 0;
  13.   with G1300Grid do begin
  14.     Cells[Col,Row]:= IntToStr(aIdx);
  15.  // Cells[0,1] := '0,1';
  16.   end;
  17.  end;  
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: TstringGrid Cells Data Entry
« Reply #1 on: January 19, 2019, 07:05:48 pm »
Trying to add a row and populate with data.

The TStringGrid has 1 fixed Column and 1 fixed Row. It has 0 custom rows at startup. Therefor; I add a row get a row count subtract 1 and I'm trying to place an item into Cells(0,1) i.e. Cells[Col,Row]:= IntToStr(aIdx);

 Under the debugger Lines 10, 11 and 12 set Col and Rows and at Line 13 Col = 0, Row = 1.  The aIdx is loaded; however it's in row 1 and column 1. Not in the fixed Column. The cemented line 15 works perfectly.

I missing something somewhere.

Code: Pascal  [Select][+][-]
  1.  procedure TbtnPath.LoadG1300Grid(aIdx  : Integer;
  2.                                   aRCId : String;
  3.                                   aLine : String);
  4.   Var
  5.    i   : Integer = 0;
  6.    Row : Integer = 0;
  7.    Col : Integer = 0;
  8.  Begin
  9.   G1300Grid.RowCount := G1300Grid.RowCount +1;   // Add a new row
  10.   Row := G1300Grid.RowCount;                                 // Get Row Count = 2
  11.   Dec(Row);                                                             // Zero Base so Dec to 1
  12.   Col := 0;
  13.   with G1300Grid do begin
  14.     Cells[Col,Row]:= IntToStr(aIdx);
  15.  // Cells[0,1] := '0,1';
  16.   end;
  17.  end;  
using the "with" construct you instruct the compiler to give priority to the G1300Grid properties over everything else. Since the TStringGrid already has properties named Col and Row you end up using the grids properties instead of the local variables defined in your procedure. If I recall correctly there was a member that was advocating the use of v in the local variable names to avoid this kind of situations can't recall who was though sorry.

In short rename your local variable to avoid the conflict (or stop using with) and everything should work eg.

Code: Pascal  [Select][+][-]
  1.  procedure TbtnPath.LoadG1300Grid(aIdx  : Integer;
  2.                                   aRCId : String;
  3.                                   aLine : String);
  4.   Var
  5.    i   : Integer = 0;
  6.    vRow : Integer = 0;
  7.    vCol : Integer = 0;
  8.  Begin
  9.   G1300Grid.RowCount := G1300Grid.RowCount +1;   // Add a new row
  10.   vRow := G1300Grid.RowCount;                               // Get Row Count = 2
  11.   Dec(vRow);                                                           // Zero Base so Dec to 1
  12.   vCol := 0;
  13.   with G1300Grid do begin
  14.     Cells[vCol,vRow]:= IntToStr(aIdx);
  15.  // Cells[0,1] := '0,1';
  16.   end;
  17.  end;  


jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: TstringGrid Cells Data Entry
« Reply #2 on: January 19, 2019, 07:08:29 pm »
You fast, I was in the middle of replying ! :o
The only true wisdom is knowing you know nothing

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: TstringGrid Cells Data Entry
« Reply #3 on: January 19, 2019, 07:19:57 pm »
WOW! I would never have figured that one out. That's pretty deep into the woods. And for a beginner or a limited knowledge pascal user such as me the answer might as well been on the moon under a rock.

Changed to vRow and vCol and It's works great.

 Thank You
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

HeavyUser

  • Sr. Member
  • ****
  • Posts: 397
Re: TstringGrid Cells Data Entry
« Reply #4 on: January 19, 2019, 07:48:10 pm »
WOW! I would never have figured that one out. That's pretty deep into the woods. And for a beginner or a limited knowledge pascal user such as me the answer might as well been on the moon under a rock.

Changed to vRow and vCol and It's works great.

 Thank You
You can test for this situation easily. just ctrl+click on the variable name, at the line you have the problem, if the editor jumps to the declaration of the variable you expect, then the problem is elsewhere, if not, then you have a conflict that needs intervention to be resolved.

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: TstringGrid Cells Data Entry
« Reply #5 on: January 19, 2019, 08:50:24 pm »
@HeavyUser

Just tested that Clt+Click. Poped up on the list. So that wold tell me I have a conflict or not using the variable I think I'm using?

I thought the local variable had the scope. Not to sure that I would have associate that with the problem. Maybe.

But maybe this is something different than scope. Sounds like inherited class properties.

Thanks for tip
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: TstringGrid Cells Data Entry
« Reply #6 on: January 19, 2019, 11:57:04 pm »
its not the variable, its just the NAME, the "WITH" puts all identifiers in the StringGrid that are visible
first. if it does not find a matching identifier of that name in the StringGrid then it moves back one and that
would be the level you were expecting.

 You can also specify multiple scopes too but they need to be of different types otherwise you'll
only see the last one, for example.

 With Button1, Button2, Button3 do
  Begin
     //  if all these buttons are of the same class type and you specify a identifier of this class
     // you'll only see the identifiers of BUtton3
 End;
 I am not even sure if the compiler will allow this, in reality it shouldn't..

 but you can use this for different types of objects  or simply not use that construct at all! :D
The only true wisdom is knowing you know nothing

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: TstringGrid Cells Data Entry
« Reply #7 on: January 20, 2019, 04:35:39 am »
Thanks
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

 

TinyPortal © 2005-2018