Page 1 of 1
Issue with DataGrid, they don't like me!
Posted: Mon May 23, 2011 12:59 am
by revRider
Another weekend... can't say wasted, but little progress made.
Can get the DataGrid to work fine in the programming environment, but once compiled nada.. What gives?
Basically I'm reading a CSV file, creating the column headers, closing, then reading the CSV data into the DataGrid (cleanest way I've gotten to work so far).
This is the third DataGrid program that I've worked on that seems to be ending in frustration.
I'm using 4.5.2, I refuse to spend anymore money on LiveCode until I can get SOMETHING to work.
Code: Select all
on ReadDataFile
put "Please Wait ..." into Field "txtTeacherName"
put empty into field "text"
put empty into field "NumberOfStaff"
put empty into field "myCurrentPath"
set the dgText of grp "gdStaffData" to empty
set the dgColumns of grp "gdStaffData" to empty
set the dgProp["columns"] of group "gdStaffData" to empty
set the itemDelimiter to "/"
put the volumes into macdrives
put line 1 of macdrives into mydrive
put the effective filename of this stack into theFolder ## Finds the datapath
put item 1 to -2 of theFolder into theFolder # Strip off filename
if the platform is "MacOS" and theFolder contains ".app/Contents/MacOS" then
delete item -3 to -1 of theFolder
end if
if FileNotFound is "0" then
put theFolder&"/"&"StaffData.csv" into tFileName ## Reads the CSV file into tFileContents ## Add the file name
put tFileName into fld "myCurrentPath"
end if
put URL ("file:" & tFileName) into tFileContents
close file tFileName
Put tFileContents into field "text"
put line 1 of tFileContents into tColumnTitles ## Creates Columns from the contents of the CSV's first line
replace comma with return in tColumnTitles
set the dgProp["columns"] of group "gdStaffData" to tColumnTitles
set the dgData of group "gdStaffData" to theDataA
open file tFileName for read ## Populate the DataGrid using the CSV file
put 0 into NumberofTeacher
read from file tFileName until return ## Get the first line
put "No" into myExit
repeat
read from file tFileName until return
if the result is "eof" then
put "Yes" into myExit
End if
add 1 to NumberofTeachers ## Breaks each line down in to 5 different lines when
replace return with empty in it ## are inserted into a column of the DataGrid
replace comma with return in it
put line 1 of it into theDataA[NumberofTeachers]["LastName"]
put line 2 of it into theDataA[NumberofTeachers]["FirstName"]
put line 3 of it into theDataA[NumberofTeachers]["RoomNumber"]
put line 4 of it into theDataA[NumberofTeachers]["JobTitle"]
put line 5 of it into theDataA[NumberofTeachers]["SectionCode"]
if myExit ="Yes" then exit repeat
end repeat
set the dgData of group "gdStaffData" to theDataA ## Commits the DataGrid
put NumberofTeachers into field "NumberOfStaff"
close file tFilename
put "Completed ..." into Field txtTeacherName
end ReadDataFile
Re: Issue with DataGrid, they don't like me!
Posted: Mon May 23, 2011 3:19 pm
by Klaus
Hi revRider,
what exactly is NADA? What exactly does not work?
Do you see the path in field "myCurrentPath"?
etc. etc...
You can check this by adding something like:
...
answer tFolder
...
answer tFileName
...
answer thisandthat
...
in the relevant places of your standalone. Know what I mean?
This way you might get a hint where exactly it does not work.
Best
Klaus
Re: Issue with DataGrid, they don't like me!
Posted: Mon May 23, 2011 5:45 pm
by bangkok
revRider wrote:
I'm using 4.5.2, I refuse to spend anymore money on LiveCode until I can get SOMETHING to work.
Sorry, but your code is confusing, and looks like a "patchwork".
A few problems from my point of view :
Code: Select all
put URL ("file:" & tFileName) into tFileContents
close file tFileName
Close file ? But you haven't opened it yet. Plus you don't need to close the file if you use the format "put URL".
Code: Select all
set the dgProp["columns"] of group "gdStaffData" to tColumnTitles
set the dgData of group "gdStaffData" to theDataA
At that point, theDataA is empty.
Code: Select all
open file tFileName for read ## Populate the DataGrid using the CSV file
put 0 into NumberofTeacher
Why do you want to open the file tFileName ... since you have the content of this file already in your field "text" and in tFileContents ?
Why do you want to use a complex
Code: Select all
read from file tFileName until return
structure to read the content of the file, since again, everything is in the field "text" and in tFileContents ?
etc. etc.
So, I think you should start over, and keep it simple.
Something like that :
Code: Select all
on ReadDataFile
answer file "Select a file"
if it is empty then exit to top
put URL ("file:" & it) into tFileContents
Put tFileContents into field "text"
put line 1 of tFileContents into tColumnTitles ## Creates Columns from the contents of the CSV's first line
replace comma with return in tColumnTitles
set the dgProp["columns"] of group "gdStaffData" to tColumnTitles
delete line 1 of tFileContents
replace "," with tab in tFileContents
set the dgData of group "gdStaffData" to tFileContents
end ReadDataFile
Re: Issue with DataGrid, they don't like me!
Posted: Mon May 23, 2011 6:30 pm
by revRider
Coming from a world of VisualBasic, QuickBasic, RealBasic, Cobol, Fortran and a half dozen web languages, trying to wrap my mind around the new logic (way of thinking) used by LiveCode.
Yes, very much a patch work trying to solve/resolve how to make things work. Getting bits and pieces of information out of documentation, forums, etc.
In other languages if you open a file, you need to close a file. ... Even if not required, good practice... If I don't do it in LiveCode, does that mean the code is bad?
by NADA I mean, in the environment the datagrid is populated with data and the program functions as it should. However once compiled no data is displayed, no tables filled.
Great Example bangkok, thanks! Much 'cleaner code'.. Only remaining question is how do you determine the number of rows/records..
Oh, I'm not prompting the user for a file name, the program should find it in the same folder/location as the program is run.
Thanks to all!
~David
Re: Issue with DataGrid, they don't like me!
Posted: Mon May 23, 2011 7:22 pm
by revRider
Ok...
Looked in "LiveCode" Dictionary, could not find a reference to "dgData"... Tried the LiveCode_Data_Grid.pdf, couldn't find an example either..
Code below reads in the column headers fine, the data is read from the CSV file correctly and placed in Field "text". It removed the first line....
Question, as with CSV files, not all columns have data, meaning some records are missing a field.
Is there a document that shows examples of dealing with importing data (CSV)?
Found out how to return the number of row/records, of course at this point it is Zero since the grid is not populated.
Again, thanks for the help!
~David
Code: Select all
on ReadDataFile
put "Please Wait ..." into Field "txtTeacherName"
put empty into field "text"
put empty into field "NumberOfStaff"
put empty into field "myCurrentPath"
set the dgText of group "gdStaffData" to empty
set the dgColumns of group "gdStaffData" to empty
set the dgProp["columns"] of group "gdStaffData" to empty
set the itemDelimiter to "/"
put the volumes into macdrives
put line 1 of macdrives into mydrive
put the effective filename of this stack into theFolder
put item 1 to -2 of theFolder into theFolder
if the platform is "MacOS" and theFolder contains ".app/Contents/MacOS" then
delete item -3 to -1 of theFolder
end if
if FileNotFound is "0" then
put theFolder&"/"&"StaffData.csv" into tFileName
end if
#------------- Test Code ------------------------
put URL ("file:" & tFileName) into tFileContents
Put tFileContents into field "text"
put line 1 of tFileContents into tColumnTitles
replace comma with return in tColumnTitles
set the dgProp["columns"] of group "gdStaffData" to tColumnTitles
delete line 1 of tFileContents
replace comma with tab in tFileContents
set the dgData of group "gdStaffData" to tFileContents
#------------- End Test Code ---------------------
put the dgNumberOfRecords of group "gdStaffData" into NumberofTeachers
put NumberofTeachers into field "NumberOfStaff"
put tFileName into fld "myCurrentPath"
put "Completed ..." into Field txtTeacherName
end ReadDataFile
Re: Issue with DataGrid, they don't like me!
Posted: Mon May 23, 2011 7:26 pm
by Klaus
Hi revRider,
revRider wrote:...In other languages if you open a file, you need to close a file. ... Even if not required, good practice... If I don't do it in LiveCode, does that mean the code is bad?
No, but this MAY throw an error when you least exspect it! (Murphys law

)
revRider wrote:... Only remaining question is how do you determine the number of rows/records...
...
put the number of lines of tFileContents into tNumOfRecords
## You get the picture

...
Best
Klaus
Re: Issue with DataGrid, they don't like me!
Posted: Mon May 23, 2011 8:32 pm
by bangkok
revRider wrote:
Great Example bangkok, thanks! Much 'cleaner code'.. Only remaining question is how do you determine the number of rows/records..
What about :
Code: Select all
put the number of lines of tFileContents into NumberofTeacher
revRider wrote:
Oh, I'm not prompting the user for a file name, the program should find it in the same folder/location as the program is run.
Once you have a better and clearer view, you could put back the first part of your initial code that deals with the automatic reading of the file.
revRider wrote:
In other languages if you open a file, you need to close a file. ... Even if not required, good practice... If I don't do it in LiveCode, does that mean the code is bad?
Don't you think that :
Code: Select all
PUT URL "file:"&myFile into myData
is much more "forceful" than the regular open file /read / close file routine ?
My point : the money you've spent on LiveCode is without any doubt a good
investment.
Like everything else, don't give up. And then you'll enjoy it.
A lot.
Re: Issue with DataGrid, they don't like me!
Posted: Mon May 23, 2011 8:35 pm
by bangkok
Re: Issue with DataGrid, they don't like me!
Posted: Tue May 24, 2011 12:38 pm
by revRider
Thanks,
The data dictionary is wonderful, if not hard to find. Saving it in safe place!
So why isn't the data populating the data grid after compiling? Is the new code any better, should it not work correctly?
~David
Re: Issue with DataGrid, they don't like me!
Posted: Wed May 25, 2011 1:19 pm
by revRider
Thanks for all the wonderful help..
Figured it out...
First off I deleted the datagrid..
Then I set dgFocus before each attempt to access the DataGrid...
The working code is below.. I was instructed to use
dgData, but after countless attempts I couldn't get it to work..
Then I tried using
dgText, worked flawlessly..
NOW How to get this program on the web?
~David
Code: Select all
on ReadDataFile
put "Please Wait ..." into Field "txtTeacherName"
put empty into field "text"
put empty into field "NumberOfStaff"
put empty into field "myCurrentPath"
set the dgText of group "gdStaffData" to empty
set the dgColumns of group "gdStaffData" to empty
set the dgProp["columns"] of group "gdStaffData" to empty
set the itemDelimiter to "/"
put the volumes into macdrives
put line 1 of macdrives into mydrive
put the effective filename of this stack into theFolder
put item 1 to -2 of theFolder into theFolder
if the platform is "MacOS" and theFolder contains ".app/Contents/MacOS" then
delete item -3 to -1 of theFolder
end if
if FileNotFound is "0" then
put theFolder&"/"&"StaffData.csv" into tFileName
end if
#------------- Test Code ------------------------
put URL ("file:" & tFileName) into tFileContents
Put tFileContents into field "text"
put line 1 of tFileContents into tColumnTitles
replace comma with return in tColumnTitles
set the dgProp["columns"] of group "gdStaffData" to tColumnTitles
delete line 1 of tFileContents
put the number of lines of tFileContents into NumberofTeachers
replace comma with tab in tFileContents
#set the dgData of group "gdStaffData" to tFileContents
set the dgText of group "gdStaffData" to tFileContents
#------------- End Test Code ---------------------
#put the dgNumberOfRecords of group "gdStaffData" into NumberofTeachers
put NumberofTeachers into field "NumberOfStaff"
put tFileName into fld "myCurrentPath"
put "Completed ..." into Field txtTeacherName
end ReadDataFile