使用Lazarus1.1和Freepascal2.7.1,我有两个StringGrids - StringGrid1和StringGrid2。
StringGrid1包含三列;第三列包含唯一值。
StringGrid2也有三列,第三列也包含相同的唯一值,但它们是从另一个来源提取的,它们的顺序不同,有些可能会丢失。
我需要查看Grid1的Col3,并查找对应的唯一值在Grid2中出现的位置(哪一行)。因此,我需要解析StringGrid1 Col3的所有行,并说“对于找到的每个值,找到StringGrid2的Column3中也包含该值的相应行,如果找到,则返回它,如果没有找到,则告诉我它丢失了,直到搜索完SG1 Col3的所有行”。
你知道我是怎么做到的吗?希望对我来说,这是一件比实际情况更复杂的事情,但希望有人能帮助我(我确实找到了这个,但我不认为它是我需要的?:Delphi Pages Entry?我也找到了这个,但它并不能很好地解释我正在做的事情,我不认为wiki entry
发布于 2012-12-03 18:40:34
发现了两种方式:
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;另一种效率较低但仍然有用的方法是:
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;或
ShowMessage(SG.IndexOf('Text To Search For')): 发布于 2012-12-07 21:04:35
我的解决方案是:
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; 这是直接写到盒子里的..但应该会给你一些解决方案的想法。
https://stackoverflow.com/questions/13658881
复制相似问题