For those who are interested and want to have a simple way to add CSV file data to a datagrid you can use this code.
I was in need of a very simple way to create a datagrid and add, change and remove data So i made the below routine for this.
I can probably be improved a lot but it does work.
CSV Data must be comma and LF delimited. But this is easy to change for other CSV file types with the replace function of LiveCode.
For testing:
1) Copy the code into the script of a button.
2) Put a datagrid in your interface and give it a name.
3) Then replace <YourDataGridName> with the name of your DataGrid.
Test the different options and please forgive me any mistakes

Regards,
Paul (MrCoolLion)
Code: Select all
on mouseUp pMouseButton
put "ID,Colom A,Colom B,Colom C"&lf&"A01,Info A,Info B,Info C"&lf&"A02,Info D,Info E,Info F" into tFileData
//put "ID,Colom A,Colom B,Colom C"&lf&"A03,Info J,Info K,Info L" into tAddLineData
//put "ID,Colom A,Colom B,Colom C"&lf&"A02,Info UJ,Info UK,Info UL" into tUpdateLineData // Whole line example first item must be Unique identifier.
//put "ID,Colom B"&lf&"A02,Info UPD" into tUpdateLineData // Change only one item in a line first item must be Unique identifier.
//put "ID"&lf&"A02" into tDeleteLineData // first item must be Unique identifier.
-----------------------------------------------------------------------------
//put "Clear" into tTask
put "Create" into tTask // tFileData first item must be Unique identifier.//put "ID,Colom A,Colom B,Colom C"&lf&"A01,Info A,Info B,Info C"&lf&"A02,Info D,Info E,Info F" into tFileData
--
//put "AddLine" into tTask //put "ID,Colom A,Colom B,Colom C"&lf&"A03,Info J,Info K,Info L" into tAddLineData
//put tAddLineData into tFileData
--
//put "UpdateLine" into tTask //put "ID,Colom B"&lf&"A02,Info UPD" into tUpdateLineData // first item must be Unique identifier.
//put tUpdateLineData into tFileData
--
//put "DeleteLine" into tTask //put "ID"&lf&"A02" into tDeleteLineData // first item must be Unique identifier.
//put tDeleteLineData into tFileData
--
put "<YourDataGridName>" into tDataGridName
DataGridCreateColumnsFillFromCSV tDataGridName, tFileData, tTask // "Clear" or "Create" or AddLine or UpdateLine or DeleteLine ; If Task is Create or Update Firstline must be column Names also file must be comma and lf seperated,
--
end mouseUp
command DataGridCreateColumnsFillFromCSV tDataGridName, tFileData, tTask
// With this routine you can fill an empty datagrid from a csv file, add a line, update a line or line items, and delete a line
// "Clear" or "Create" or "AddLine" or "UpdateLine" or DeleteLine
// If Task is "Create" Firstline must be column Names also file must be comma seperated and lf delimited,
// if Task is "Add" or "Update" Firstline must be column Names then lf then Columns Data
// if Task is "DeleteLine" Firstline must be column ID Name then lf then Line ID Data
-- Examples --
//put "ID,Colom A,Colom B,Colom C"&lf&"A01,Info A,Info B,Info C"&lf&"A02,Info D,Info E,Info F" into tFileData
//put "ID,Colom A,Colom B,Colom C"&lf&"A03,Info J,Info K,Info L" into tAddLineData
//put "ID,Colom A,Colom B,Colom C"&lf&"A02,Info UJ,Info UK,Info UL" into tUpdateLineData // Only per line not idividual item first item must be Unique identifier.
//put "ID,Colom B"&lf&"A02,Info UPD" into tUpdateLineData // first item must be Unique identifier.
//put "ID"&lf&"A02" into tDeleteLineData // first item must be Unique identifier.
--
//put "Clear" into tTask
//put "Create" into tTask // tFileData
--
//put "AddLine" into tTask
//put tAddLineData into tFileData
--
//put "UpdateLine" into tTask
//put tUpdateLineData into tFileData
--
//put "DeleteLine" into tTask
//put tDeleteLineData into tFileData
----------------------------------------------------
if tTask = "Clear" or tTask = "Create"
then
set the dgProps["columns"] of control tDataGridName to "" // Empty DataGrid Columns
set the dgData of group tDataGridName to "" // Empty DataGrid Data
if tTask = "Clear"
then
exit DataGridCreateColumnsFillFromCSV
end if
end if
--------------------------------------------------
if tTask = "AddLine"
then
put the dgNumberOfLines of group tDataGridName into tLineNbrs
if tLineNbrs = 0
then
put "Create" into tTask
end if
end if
----------------------------------------------------
if tTask = "Create"
then
put line 1 of tFileData into tLine1Data
put tLine1Data into tLine1CRData
replace "," with cr in tLine1CRData
set the dgProps["columns"] of control tDataGridName to tLine1CRData
--
put the number of lines of tFileData into tNumberOfLines
put the number of items of tLine1Data into tNumberOfColumns
repeat with tLineNbr = 2 to tNumberOfLines
put line tLineNbr of tFileData into tLine2Data
repeat with tItemNbr = 1 to tNumberOfColumns
put item tItemNbr of tLine1Data into tColumnName
put item tItemNbr of tLine2Data into tItemData
put tItemData into aDataArray[tLineNbr-1][tColumnName]
end repeat
end repeat
set the dgData of group tDataGridName to aDataArray
end if
----------------------------------------------------
if tTask = "AddLine"
then
------------------------------
put line 1 of tFileData into tStringColumnNames
put line 2 of tFileData into tRowData
replace "," with cr in tStringColumnNames
replace "," with tab in tRowData
put the dgNumberOfLines of group tDataGridName + 1 into tNewLineNbr
dispatch "AddLine" to group tDataGridName with tRowData, tStringColumnNames, tNewLineNbr
dispatch "ScrollIndexIntoView" to group tDataGridName with tNewLineNbr
end if
------------------------------
// put "ID, Colom A,Colom B,Colom C"&lf&"A02,Info UJ,Info UK,Info UL" into tUpdateLineData
if tTask = "UpdateLine"
then
put line 1 of tFileData into tColumnNames
put line 2 of tFileData into tLineData
put item 1 of line 1 of tFileData into tLineColumnID
put item 1 of line 2 of tFileData into tLineID
put the dgData of group tDataGridName into aDataArray
put the keys of aDataArray into tKEYNumbers
put tKEYNumbers into tOrgKeyNumbers
replace cr with "," in tKEYNumbers
put max(tKEYNumbers) into tMaxLineNbr
put min(tKEYNumbers) into tMinLineNbr
put the keys of aDataArray[tMinLineNbr] into tArrayColumnNames
repeat for each line tLineNbr in tOrgKeyNumbers
put aDataArray[tLineNbr][tLineColumnID] into tRowLineID
if tRowLineID = tLineID
then
put tLineNbr into tLineID2Update
put the keys of aDataArray[tLineNbr] into tColumnData
repeat for each item tColumnName in tColumnNames
put itemOffset(tColumnName,tColumnNames) into tItemPosition
if tItemPosition > 1
then
put item tItemPosition of tLineData into tItemNewData
put tItemNewData into aDataArray[tLineNbr][tColumnName]
end if
end repeat
end if
end repeat
set the dgDataOfIndex[tLineID2Update] of group tDataGridName to aDataArray[tLineID2Update]
end if
------------------------------------------------------
if tTask = "DeleteLine"
then
put line 1 of tFileData into tColumnNames
put line 2 of tFileData into tLineData
put item 1 of line 1 of tFileData into tLineColumnID
put item 1 of line 2 of tFileData into tLineID
put the dgData of group tDataGridName into aDataArray
put the keys of aDataArray into tKEYNumbers
----
repeat for each line tLineNbr in tKEYNumbers
put aDataArray[tLineNbr][tLineColumnID] into tRowLineID
if tRowLineID = tLineID
then
put tLineNbr into tLine2Delete
end if
end repeat
send "DeleteIndex "& tLine2Delete to the dgControl of group tDataGridName in 0 seconds
end if
------------------------------
end DataGridCreateColumnsFillFromCSV