Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to populate grid rows with array 2

Status
Not open for further replies.

SitesMasstec

Programmer
Sep 26, 2010
523
Brasil
Hello colleagues!

How can I populate rows in a grid with an array of values, like in the picture bellow:

grid1_y7eodf.jpg




Thank you,
SitesMasstec
 
Normalisation is the bigger picture, yes, but you can also say it's against VFP rules.
If you want to show multiple lines in a grid, you have multiple records.

You also don't use any table as input vehicle, as it's meant to be here. And even if you do, why must it be one record, you make up rules here, that don't hold true, as you see the grid doesn't support your idea as good idea. And instead of changing your mind, you blame the grid to be the problem.

In an earlier post you're working with browse in edit mode. Grids can do that, too, but the planned look rather is the normal browse/grid mode. And for that matter, you need multiple rows aka multiple records. Nothing prevents you from using multple records in a table for input/editing.

Instead of one record with fields repeated 10 times you create a table with each field once and prepopulate it with 10 records. There's nothing difficult about this.

Bye, Olaf.
 

I already use this, similar to Griff's Function GetCustBuy:

Reading a record: YRCO(1)= field ROR01, YRCO(2) = field ROR02, ... YRCO(20)= field ROR20
Code:
FOR X=1 TO 20
	strX=STR(X,2)
	IF SUBSTR(strX,1,1)=" "
		strX="0"+SUBSTR(strX,2,1)
	ENDIF

	CAMPOX="ROR"+strX
	YROR(X)=&CAMPOX


Saving a record: field RCO01 = YRCO(1), field RCO02=YRCO(2), ... field RCO20=YRCO(20)
Code:
FOR X=1 TO 20
	strX=STR(X,2)
	IF SUBSTR(strX,1,1)=" "
		strX="0"+SUBSTR(strX,2,1)
	ENDIF

	CAMPOX="ROR"+strX
	REPLACE &CAMPOX WITH YROR(X)

So, I will merge it with Olaf's advice:
I will save each element Item(1), Med(1)... till Item(20), Med(20), in a record in a Cursor, and use each record to populate each row in the Grid (total 20 records)

I will try to implement this.



Thank you,
SitesMasstec
 
You will not need this code, as your data already is 10 (20?) records in the first, if you design your table that way. You just need one additional column with an id grouping these records belonging together.

Bye, Olaf.
 

Olaf, I do not use Database container, just free tables...


Thank you,
SitesMasstec
 
That doesn't depend on DBC at all. Along what lines are you thinking?
Even in legacy Foxpro relations between were thought of with the SET RELATION command. You don't have the primarykey index type, but candidate. Free tables nowadays also have autoinc as feature, you don't need DBC to have it. You can't define relations between tables without a DBC, and no trigger code, but you still can make relations, and you should. Everyting is intended for that.

Similar to ONE customer record that has the fields: ...
You don't do that.

For example seeing buys in months is a matter of a view grouping by month(purchasedate).
If you really find it advanced to need to normalize data on the fly to display it in grids instead of simply storing it that way, I fear I can't teach you anything anymore.

Bye, Olaf.
 
Well, I tried to populate the Grid now, but I have not got the awaited result:

telaCursorArray1_xywrw0.jpg


Note that the array is populated as expected (please see note 1), but just the first record is copied to cursor (and showed in the Grid) (please see note 2).


Thank you,
SitesMasstec
 
You shouldn't go with that advice anyway. If the grid only shows row 1 then the grid cursor is only having one record. As the array you append is one dimensional the number of rows is always 1, no matter how large the array is.

Bye, Olaf.
 

Hello Olaf!

I created a one more variable:
DECLARE YINU_DESC(10), YINU_QTDE(10)

Then, in the FOR...NEXT loop:
Code:
YINU_QTDE(Y)="0.000"

Then:
Code:
CREATE CURSOR INFONUT1A (INU_DESC C(20), INU_QTDE C(18))

So, what shoud be the "APPEND FROM ARRAY " command?


Thank you,
SitesMasstec
 
SitesMasstec,

Would you be able to give, in a notepad, txt file an example how your data looks and how you would like this data to be shown in your grid?
Regards,
Koen
 

Hello Koen!

Each line has:

first element, second element
YINU_DESC(Y), YINU_QTDE(Y)

"Valor energético","0.000"
"Carboidratos","0.000"
"Proteínas","0.000"
"Gorduras totais","0.000"
"Gosduras saturadas","0.000"
"Gordura trans","0.000"
"Fibra alimentar","0.000"
"Sódio","0.000"

I want to put theses elements in the Grid, so they will appear as:
[pre]
Valor energético | 0.000
Carboidratos | 0.000
Proteínas | 0.000
Gorduras totais" | 0.000
Gosduras saturadas | 0.000
Gordura trans | 0.000
Fibra alimentar | 0.000
Sódio | 0.000
[/pre]

Thank you,
SitesMasstec
 

Olaf said:
For example seeing buys in months is a matter of a view grouping by month(purchasedate).

Yes, Olaf, of course I have been using this for a long time, in all my applications. If not, I should have abandoned programming.

But the application I am working now, is quite different and somewhat complex. Maybe I was not able to express its complexity, I am just showing some part of it here.


Thank you,
SitesMasstec
 

Dear colleagues:

I could not find an answer in the VFP 9 Help file, and in books about this:

So, I have this array, with 2 elements -- YINU_DESC(Y), YINU_QTDE(Y) --:
"Valor energético","0.000"
"Carboidratos","0.000"
"Proteínas","0.000"
"Gorduras totais","0.000"
"Gosduras saturadas","0.000"
"Gordura trans","0.000"
"Fibra alimentar","0.000"
"Sódio","0.000"

And I think that if I have to use the command "APPEND FROM ARRAY ArrayName", how can I define a name for the above array?



Thank you,
SitesMasstec
 
If a software get's more complex, it's even more important to store your data in a normalized manner.

Anyway, APPEND FROM ARRAY is a dead end.That was Mikes idea to actually bring 2d array data into the cursor and display the cursor itself.
You need a for loop, like I did in my example, appending via APPEND BLANK as many times, as the rows you need.

But again you don't need it at all, if you change your data storage to normal ways.

Even if any part of your software needs the data in the structure you now use for your DBF, that is called pivoting, and that is done on data stored in rows and transposed and aggregated into columns wheny querying data and laoding it into memory/cursor. You never store it that way, though. NEVER. EVER.

If you would stop your resistance against a much simpler solution, you would already have it.

Bye, Olaf.
 
Hi Olaf, happy news from me (with your valuable help):


Problem solved:

telaCursorArray2_e7d88j.jpg


Thank you,
SitesMasstec
 
And now you actually can display the infonut1a cursor instead of the array, throw out the controlsource expressions. You don't even need the array. Don't you see you populate the array just to store it into the cursor as next step?
And if you store data that way you even don't need any code unpivoting it that or any other way.

It's good you're happy, but you could be happier with less.

Bye, Olaf.
 
Wow!

Right, Olaf, I do not need to use array:

telaCursorArray3_rg8l1t.jpg


Thanks again, Olaf!



Thank you,
SitesMasstec
 
Sitesmacc.

Just create a cursor

CREATE CURSOR alias_name
...[CODEPAGE=nCodePage]
(fname1 cFieldType [(nFieldWidth [, nPrecision])] [NULL | NOT NULL]
[CHECK lExpression [ERROR cMessageText]]
[AUTOINC [NEXTVALUE NextValue [STEP StepValue]]]
[DEFAULT eExpression] [UNIQUE [COLLATE cCollateSequence]]
[NOCPTRANS] [, fname2 ...])
| FROM ARRAY ArrayName

and populate your grid with that cursor.

Regards,
Koen
 
Koen, this comes very late, he already does.

Let's summarize from the start: You came here specifying you have arrays you need to display in a grid. Actually, what you designed is a single table and that's what you wanted to display in a grid. You seem to design all your data in a way you only need one record to bind to controls. That only works with single field controls like textbox and editbox, spinner, checkbox, and so on. There are several controls, not only the grid, that display and are bindable to a whole table, to a list of records.

The reasooning for your bad data design seems to me your only used to a business intelligence view of aggregated data. But that's not coming 1:1 from tables designed that way, that is a typical result of aggregating data, grouping it, and pivoting it.

Instead of your one table you need two, one for the single fields and an identifier, the second for related tuples of always the same structure, it's own identifier, the identifier of the main record and then the tuple of desc, qtde, medi, perc, so overall (id, parentid, desc, qtde, medi, perc)

And then grid can be populated from that second table using the desc and qtde fields of the table filtered for a certain id value in parentid. That's all. And you can easily switch to other parentids, if you also open the parent table, display it in another grid and have a relation between them from parent.id to child.parentid, the child table having an index on parentid.

And I know you know enough VFP to make all this. Instead you choose to go the hard route. Why?

Bye, Olaf.
 
Hi,
Why not simply use the Grid's PARTITION and VIEW properties?

Code:
PUBLIC go_Form
go_Form = CreateObject ("frmForm")
go_Form.Visible = .T.

WITH go_Form.grdNames
	.Readonly = .T.
	.Column1.Header1.Caption = "Name" 
	.Column2.Header1.Caption = "M"
	.Column3.Header1.Caption = "V"
ENDWITH 

go_Form.Show

READ Events
CLOSE ALL
CLEAR ALL


DEFINE CLASS frmForm As Form
  Width = 400
  Height = 360
  AutoCenter = .T.

  * Add a grid to the form
  Add Object grdNames as Grid with;
    RecordSource = "curNames", ColumnCount = 3, Visible = .t., Top = 20, Left = 20, Width = 360, [COLOR=#EF2929]Partition = 120, View = 1[/color] 
    			
  * Add cancelbutton to the form
  ADD OBJECT cmdCancel As CommandButton WITH;
    Width=60, Height=30, Left=20, Top=250, Caption="Exit"

	PROCEDURE cmdCancel.Click()
		CLEAR Events
		ThisForm.Release
	ENDPROC
  
	PROCEDURE Destroy()
		CLEAR Events
		ThisForm.Release
	ENDPROC
    
	PROCEDURE Load
		Create Cursor curNames (cName C(10), nMeters I, nVolume I)
			For li_I = 1 to 20
				INSERT INTO curNames (cName, nMeters, nVolume) VALUES ("Name" + PADL(li_I,3,"0"), li_I, li_I * 3)
			Next li_I   
		
		Go Top
	ENDPROC

ENDDEFINE

hth
MK
 
Olaf,
I agree, I come only after 30+ messages, however I had a slight idea OP was looking for a pivot table in a certain way, not very sure, but it seems like that. I believe in VFP you are much better of with cursors than array's and more over for a grid a cursor is an ideal control source. Now only one needs to sort/modify the cursor in the way you would like to see it in a grid, the more a grid is nothing less than an other way of browsing your table/cursor or view but not browsing an array, which is technicaly possible but not the road one should go to construct a grid in VFP.
I also read that the (starting) table is far from in an ideal format, however this could be modified.
If OP is willing to show a his table (structure) and desired result, I am sure we can make a small procedure to convert his data for a in a grid presentation.
I also dont understand why nobody has asked the OP for his data presentation and the required presentation in a grid, that would have solved his problem is a few minutes instead of 30+ messages
Regards,
Koen
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top