Question

Using Lazarus 1.1 and Freepascal 2.7.1, I have two StringGrids - StringGrid1 and StringGrid2.

StringGrid1 contains three columns; the 3rd column of which contains unique values.

StringGrid2 also has three columns, the 3rd of which also contains the same unique values but they are drawn from another source, they are in a different order and some may be missing.

I need to look through Col3 of Grid1 and lookup where (which row) the corresponding unique value appears in Grid2. So I need to parse ALL the rows of StringGrid1 Col3 and say "For each value found, find the corresponding row of Column3 of StringGrid2 that also contains that value and return it, if found, or tell me it's missing if not found, until all rows of SG1 Col3 have been searched".

Any ideas as to how I do that? It's hopefully one of things that seems to me to be more complicated than it actually is but hopefully someone can help (I did find this but I don't think it's quite what I need? : Delphi Pages Entry? I also found this but it doesn't quite explain how to apply it what I am doing, I don't think wiki entry

Was it helpful?

Solution 2

Discovered two ways:

for count1 := 0 to StringGrid1.RowCount - 1 do
  for count2 := 0 to StringGrid2.RowCount - 1 do
  begin
    if StringGrid1.Cells[3, count1] = StringGrid2.Cells[3, count2] then
    begin
      ShowMessage(StringGrid1.Cells[3, count1] + ' Found In Row ' + IntToStr(count2));
      Break;
    end;
  end;

The other, less efficient but still useful way using:

for i := 0 to SL.Count - 1 do 
begin
  ShowMessage(SG.IndexOf(SL.Strings[i])): 
  // (SG being the StringGrid and SL being a StringList)
end;

or

ShowMessage(SG.IndexOf('Text To Search For')): 

OTHER TIPS

My solution:

      VAR
        List_Found_Values,
        List_Not_Found        : TSTRINGLIST;
        i, I_Found            : INTEGER;
    BEGIN
      List_Found_Values   := TSTringList.Create;
      List_Not_Found      := TStringList.Create;

      FOR i := 0 TO StringGrid1.Count - 1 DO
      BEGIN
        I_Found := StringGrid2.Cols[2].IndexOf (StringGrid1.Cells[2, i]);
        IF I_Found > -1 THEN
          List_Found_Values.Add (StringGrid2.Cells[0, I_Found]+' + StringGrid2.Cells[1, I_Found]+' '+StringGrid2.Cells[2, I_Found])
        else
          List_Not_Founds.Add (StringGrid1.Cells[2, I_Found]);
      END;
      {use the tstringlist items List_Found and List_Not_Found etc. count to deside what to do }
    END;  

This is just written straight into box.. but should give you some idea of solution.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top