首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >搜索StringGrid1的列,查看它在StringGrid2 - Lazarus & Freepascal中出现的行

搜索StringGrid1的列,查看它在StringGrid2 - Lazarus & Freepascal中出现的行
EN

Stack Overflow用户
提问于 2012-12-01 19:17:26
回答 2查看 1.8K关注 0票数 0

使用Lazarus1.1和Freepascal2.7.1,我有两个StringGrids - StringGrid1和StringGrid2。

StringGrid1包含三列;第三列包含唯一值。

StringGrid2也有三列,第三列也包含相同的唯一值,但它们是从另一个来源提取的,它们的顺序不同,有些可能会丢失。

我需要查看Grid1的Col3,并查找对应的唯一值在Grid2中出现的位置(哪一行)。因此,我需要解析StringGrid1 Col3的所有行,并说“对于找到的每个值,找到StringGrid2的Column3中也包含该值的相应行,如果找到,则返回它,如果没有找到,则告诉我它丢失了,直到搜索完SG1 Col3的所有行”。

你知道我是怎么做到的吗?希望对我来说,这是一件比实际情况更复杂的事情,但希望有人能帮助我(我确实找到了这个,但我不认为它是我需要的?:Delphi Pages Entry?我也找到了这个,但它并不能很好地解释我正在做的事情,我不认为wiki entry

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-12-03 18:40:34

发现了两种方式:

代码语言:javascript
复制
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;

另一种效率较低但仍然有用的方法是:

代码语言:javascript
复制
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;

代码语言:javascript
复制
ShowMessage(SG.IndexOf('Text To Search For')): 
票数 0
EN

Stack Overflow用户

发布于 2012-12-07 21:04:35

我的解决方案是:

代码语言:javascript
复制
      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;  

这是直接写到盒子里的..但应该会给你一些解决方案的想法。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13658881

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档