Search code examples
searchfreepascallazaruststringgrid

Searching the column of StringGrid1 to see what row it appears in StringGrid2 - Lazarus & Freepascal


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


Solution

  • 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')):