文件文件内容如下所示:                  
                     代码        名称                          
                      110000,北京市
                         110100,北京市市辖区                      
                         110101,北京市东城区
                         110102,北京市西城区
                         110103,北京市崇文区
                         ....  ,  ........
                      共有五千多行.读出之后将代码和名称部分 分开,存放在StringGrid(或其它类型控件上)让其一个代码对应一个名称,然后每页显示20条数据,点击下一页显示后面的数据.并能提供首页,第一页,最后一页,末页,转到某一页的功能.另外如此多的数据行数能否做优化处理.请各位大侠多多指教了!谢谢!

解决方案 »

  1.   

    用ado连接这个文本文件就行了啥
      

  2.   


    //五千行应该没问题吧?试试,试试 ^_^procedure TForm1.Button1Click(Sender: TObject);
    var
      SS, SS2: TStringList;
      I, L : integer;
    begin
      SS := TStringList.Create;
      SS2 := TStringList.Create;
      SS.LoadFromFile('c:\test.txt');
      L := 1;
      for I := 0 to SS.Count-1 do begin
        SS2.Text := StringReplace(SS[I], ',', #10, [rfReplaceAll]);
        StringGrid1.Cells[1,L+I] := SS2[0];
        StringGrid1.Cells[2,L+I] := SS2[1];
        StringGrid1.RowCount := StringGrid1.RowCount+1;
      end;
      SS2.Free;
      SS.Free;
    end;
      

  3.   

    {一些细节上还需要你自己修饰一下!}unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, Grids, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        StringGrid1: TStringGrid;
        Button2: TButton;
        procedure Button1Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
        procedure Button2Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
        function GridPageScroll(Arrow: boolean): boolean;
      end;var
      Form1: TForm1;implementation{$R *.dfm}var
      SS: TStringList;
      Page: integer    = 0;//当前页码
      PerPage: integer = 4;//每页行数 我用每页4行测试的。你自己改吧function TForm1.GridPageScroll(Arrow: boolean): boolean;
    var
      SS2: TStringList;
      I, F, T, L : integer;
    begin
      Result := True;
      if Arrow then Inc(Page) else Dec(Page);
      if (Page<1) or (Page>(SS.Count div PerPage + 1)) then begin
        Result := False;
        Exit;
      end else
      SS2 := TStringList.Create;
      F := Page * PerPage - PerPage;
      T := Page * PerPage;
      if Page * PerPage > SS.Count-1 then T := SS.Count-1;
      L := 1;
      for I := F to T do begin
        SS2.Text := StringReplace(SS[I], ',', #10, [rfReplaceAll]);
        StringGrid1.Cells[1,L] := SS2[0];
        StringGrid1.Cells[2,L] := SS2[1];
        Inc(L);
      end;
      SS2.Free;
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      if not GridPageScroll(True) then Showmessage('已到尾'); //向后翻
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
      if not GridPageScroll(False) then Showmessage('已到头'); //向前翻
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      StringGrid1.RowCount := PerPage+1;
      SS := TStringList.Create;
      SS.LoadFromFile('c:\test.txt');
    end;procedure TForm1.FormDestroy(Sender: TObject);
    begin
      if SS<>nil then FreeAndNil(SS);
    end;
    end.
      

  4.   


    朋友啊,上面实现的是向后翻页和向前翻页。至于第一页、末页、转到某一页,你自己改程序吧!其实就是利用了Page这个当前页变量!不要企望别人帮你写一个完完整整的程序啊.....^_^
      

  5.   

    是的是的,已经非常感谢了!最后我想问一下,能否做一下查询,也就是遍历一下名称,并定位到到该行,能否给个思路,谢谢了lihuasoft.待我测试一下.
      

  6.   

    要做查询,就循环比对StringList的每个元素行了。例如:for I := 0 to SS.Count-1 do
      if SS[I]='10010,北京市' then
        Found!I就是行号!通过I与每页数量,确定出它在哪一页,然后“翻页”到那页显示就行了道理很简单,代码写起来很费力,因为要考虑各种错误产生的因素
      

  7.   


    {实现上翻、下翻、到某页、首页、末页}
    {还是那句话,有点小问题,是细节问题,自己改}unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, Grids, StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        StringGrid1: TStringGrid;
        Button2: TButton;
        Button3: TButton;
        Edit1: TEdit;
        Button4: TButton;
        Button5: TButton;
        procedure Button1Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
        procedure Button4Click(Sender: TObject);
        procedure Button5Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
        function GridPageScroll: boolean;
      end;var
      Form1: TForm1;implementation{$R *.dfm}var
      SS: TStringList;
      Page: integer    = 1;//当前页码
      PerPage: integer = 4;//每页行数function TForm1.GridPageScroll: boolean;
    var
      SS2: TStringList;
      I, F, T, L : integer;
    begin
      Result := True;
      if (Page<1) or (Page>(SS.Count div PerPage)+1) then begin
        Result := False;
        Exit;
      end;
      SS2 := TStringList.Create;
      F := Page * PerPage - PerPage;
      T := Page * PerPage;
      if Page * PerPage > SS.Count-1 then T := SS.Count-1;
      L := 1;
      for I := F to T do begin
        SS2.Text := StringReplace(SS[I], ',', #10, [rfReplaceAll]);
        StringGrid1.Cells[1,L] := SS2[0];
        StringGrid1.Cells[2,L] := SS2[1];
        Inc(L);
      end;
      SS2.Free;
      Edit1.Text := InttoStr(Page);
    end;procedure TForm1.Button1Click(Sender: TObject);
    begin
      if not GridPageScroll then begin
        Showmessage('已到尾');
        Dec(Page);
      end else Inc(Page);
    end;procedure TForm1.Button2Click(Sender: TObject);
    begin
      if not GridPageScroll then begin
        Showmessage('已到头');
        Inc(Page);
      end else Dec(Page);
    end;procedure TForm1.Button3Click(Sender: TObject);
    begin
      Page := StrtoInt(Edit1.Text);
      GridPageScroll;
    end;procedure TForm1.Button4Click(Sender: TObject);
    begin
      Page := 1;
      GridPageScroll;
    end;procedure TForm1.Button5Click(Sender: TObject);
    begin
      Page := SS.Count div PerPage + 1;//这句有问题,自己改
      GridPageScroll;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      Button1.Caption := '向下翻';
      Button2.Caption := '向上翻';
      Button3.Caption := '跳到某页';
      Button4.Caption := '首页';
      Button5.Caption := '末页';
      Edit1.Text := '1';
      StringGrid1.RowCount := PerPage+1;
      SS := TStringList.Create;
      SS.LoadFromFile('c:\test.txt');
      GridPageScroll;
    end;procedure TForm1.FormDestroy(Sender: TObject);
    begin
      if SS<>nil then FreeAndNil(SS);
    end;end.