integrating Datagrids

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
bsouthuk
Posts: 261
Joined: Fri Dec 05, 2008 7:25 pm

integrating Datagrids

Post by bsouthuk » Sat Dec 03, 2011 4:08 pm

Another datagrid problem I have encountered!

I am trying to integrate data in 2 datagrids. Datagrid 1 has 2 columns, column A and Column B. Datagrid 2 has 3 Colums - A, B and C.

I am trying to work out a repeach for each line formula but Im getting nowhere fast!

I want it so that if the cells of columns A of datagrid 1 are the same/equal to the cells in column A of datagrid 2 then whatever is in the cell of column B of datagrid 1 should be copied into the cell of column B of Datagrid 2.

Could anyone help me with the code for this one?

Thanks

Daniel

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10356
Joined: Wed May 06, 2009 2:28 pm

Re: integrating Datagrids

Post by dunbarx » Sun Dec 04, 2011 6:55 pm

I don't use datagrids much, and I believe that there is a way to extract the column data alone.

But the method would be similar to what I would do offhand, which is to get the dgText of each datagrid. Each will be a tab and return delimited data set. Write a repeat loop like this (untested):

put the dgText of group "yourDataGrid1" into dataSet1
put the dgText of group "yourDataGrid2" into dataSet2
set the itemdelimiter to tab

repeat with y = 1 to the number of lines of dataSet1
if item 1 of line y of dataSet1 = item 1 of line y of dataSet2 then put item 2 of line y of dataSet1 into item 2 of line y of dataSet2
end repeat
set the dgText of group "yourDataGrid2" to dataSet2

I hope you get the idea. Not sure about the length of your two datagrids, and this may be an issue.

I think that there are more direct tools to manipulate dataGrids, where the data resides in an array. But the method ought to be similar.

Craig Newman

bsouthuk
Posts: 261
Joined: Fri Dec 05, 2008 7:25 pm

Re: integrating Datagrids

Post by bsouthuk » Mon Dec 05, 2011 12:09 am

HI Craig

Thank you very much for your help and the code that you provided which works great.

I have encountered a flaw which I didnt consider though which reall complicates things and is way above my development skills!

The code you provided checks the items in each datagrid 1 line at a time. However what I need is: if item 1 of line y of dataset1 is 'in any of the lines of column 4' then to put item 2 of the line that matches item 1 of line y of dataset2 into item 2 of line y of dataset2

Is this possible?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10356
Joined: Wed May 06, 2009 2:28 pm

Re: integrating Datagrids

Post by dunbarx » Mon Dec 05, 2011 6:01 am

Hi.

You should, one day, check out the lessons on datagrids at:

http://lessons.runrev.com/s/lessons/m/d ... or-Column-

Where, likely, much more compact tools to access and manipulate datagrids are to be found.

Column 4? Thought there were only 2 in dg 1 and 3 in dg 2. Oh well.

But if you want to use old fashioned methods, you can dissassemble dataSet2 as follows:

put the dgText of group "yourDataGrid2" into dataSet2
set the itemdelimiter to tab

repeat with y = 1 to the number of lines of dataSet2
put item 4 of line y of dataSet2 & return after temp --create list of only column 4 data
end repeat

repeat with y = 1 to the number of lines of dataSet1
if item 1 of line y of dataSet1 is among the lines of temp then put item 2 of line y of dataSet1 into item 2 of line y of dataSet2
end repeat
set the dgText of group "yourDataGrid2" to dataSet2

Now I suspect that I have missed your point,( since I don't really understand your explanation). Not sure which columns (items) you want to match and relocate.

But I bet you do. What I want you to do is look up "is among" in the dictionary, which is what you are really asking for, and to understand how to extract the data in column 4 by assembling that fourth item into a return delimited list per the above code snippet. If you want to find the line number of the found data, check out the "lineOffSet", "offset", and "itemOffSet". With these tools you should be able to find and relate the line of the matched data in the way that you want to.

Continue to write back.

Craig newman

bsouthuk
Posts: 261
Joined: Fri Dec 05, 2008 7:25 pm

Re: integrating Datagrids

Post by bsouthuk » Mon Dec 05, 2011 3:30 pm

Thanks again Craig - I'm now really getting to understand how datagrids work!

What I now need to do is update the data that is stored in mySQL with the data that is in my datagrid.

I currently have the following code:

Code: Select all


 put the dgText of group "Upgrade" into tUpgrade
set the itemdelimiter to tab

repeat with y = 1 to the number of lines of tUpgrade

   put "UPDATE MobileInfo Login SET AccountManager=" & "'" & (item 2 of line y of tUpgrade) & "'" & "WHERE ID=" & "'" & (item 1 of line y of tUpgrade) & "'" into tSQL
   
   end repeat


-- send the SQL to the database, filling in the placeholders with data from variables
revExecuteSQL gConnectionID, tSQL


The code updates only the last line in my mySQL database. I am using the repeat loop and still only updates just the last line.

Sorry to bug you again on this - but I am banging my head against the walls! :evil:

What have I missed out in the code?

Post Reply