Page 1 of 1
FindNextLine in a DataGrid
Posted: Wed Jan 06, 2010 12:46 am
by lohill
Under either program control or in the message box I can use code like the following to find a line in a DataGrid that contains a specific value.
Code: Select all
dispatch "FindLine" to group "DataGrid 1" with "Ticker","NLY"
Is there a way to go about finding the next line containing "NLY" if one exists?
Thanks,
Larry
Re: FindNextLine in a DataGrid
Posted: Wed Jan 06, 2010 4:28 pm
by trevordevore
There is no FindNext command build into the Data Grid so you would need to write your own. The lesson on exporting data from a Data Grid shows how to loop through the Data Grid array. You could just add your own logic for the find into the loop.
How Do I Export Data From A Data Grid?
Re: FindNextLine in a DataGrid
Posted: Wed Jan 06, 2010 6:17 pm
by lohill
Thanks Trevor,
I have been avoiding working with the Index in favor of working with the line simply because it seems easier for my mind to grasp. I do realize that the index is a 'fixed' thing while the line is subject to the whim of sorting by the column headings. I think I am correct in assuming that if I did a FindIndex I would always find the the lowest index first. From there I could step through the rest of the index values looking for more occurrences of 'NLY'. Is there any way once you have an Index value to convert it to the Line value for the current physical arrangement of the grid?
It looks like I will have to convert myself to working with the 'Index' exclusively because with 'Lines' I would always have to loop through the whole collection to find duplicate values.
Thanks for your 'invention' of the DataGrid. I think I'll get used to it eventually.
Larry
Re: FindNextLine in a DataGrid
Posted: Wed Jan 06, 2010 7:04 pm
by trevordevore
You can get the line of an index using dgLineOfIndex (which doesn't appear to be in the docs).
Code: Select all
put the dgLineOfIndex[theIndex] of group "DataGrid" into theLineNo
Using the above code you would only work with indexes in the routine you write to implement a FindNext. Something like this might work in a Data Grid group script:
Code: Select all
command FindNext pStartingLine, pColumn, pSearchStr
local theFoundIndex = 0
put the dgData of me into theDataA
put item pStartlingLine to -1 of the dgIndexes of me into theIndexes
repeat for each item theIndex in theIndexes
-- perform search: if theDataA[theIndex][pColumn] ...
-- if found then stick theIndex into theFoundIndex
end repeat
if theFoundIndex > 0 then
return the dgLineOfIndex[theFoundIndex] of me
else
return 0
end if
end FindNext
Re: FindNextLine in a DataGrid
Posted: Wed Jan 06, 2010 7:56 pm
by lohill
Thanks Trevor,
My code is nowhere nearly as elegant as yours but I had come up with the following to get a lis of Indexes for lines containing containing the same value in a given column of a DataGrid:
Code: Select all
on FindIndexes tName tContents
-- Returns a list of Indexes with with same content for the given column name
-- Returns 0 if nothing found else returns list of Indexes
dispatch "FindIndex" to Group "DataGrid 1" with tName, tContents
put the result into tFirstIndex
put tFirstIndex into theList
If tFirstIndex > 0 then
put the dgNumberOfRecords of group "DataGrid 1" into tLast
repeat with i = tFirstIndex + 1 to tLast
put the dgDataofIndex[i] of group "DataGrid 1" into theData
if theData[tName] = tContents then
put comma & i after theList
end if
end repeat
end if
return theList
end FindIndexes
Unfortunately for me this code has to be in the stack script because I haven't yet figured how to make DataGrid 1 a part of the card's background and still be able to fill it under program control. As it is now each time I create a new card I have to copy DataGrid 1 to the new card and then fill it with data. (see
http://forums.runrev.com/phpBB2/viewtop ... f=8&t=4588)
At any rate my code appears to work where it is and with your dgLineOfIndex I will now have to make the choice whether to use Lines or Indexes as my predominate way of accessing data. I am actually beginning to lean toward Indexes now.
Best regards,
Larry