怎样使用代码移动StringGrid的行?上移下移或者删除,最后是无缝的。

解决方案 »

  1.   

    procedure TForm1.Button1Click(Sender: TObject); {上移}
    var
      s: TStrings;
      i: Integer;
    begin
      i := sg.Row;
      if i <=1 then Exit;
      s := TStringList.Create;
      s.Assign(sg.Rows[i-1]);
      sg.Rows[i-1] := sg.Rows[i];
      sg.Rows[i] := s;
      sg.Row := i-1;
      s.Free;
    end;procedure TForm1.Button2Click(Sender: TObject); {下移}
    var
      s: TStrings;
      i: Integer;
    begin
      i := sg.Row;
      if i >= sg.RowCount-1 then Exit;
      s := TStringList.Create;
      s.Assign(sg.Rows[i+1]);
      sg.Rows[i+1] := sg.Rows[i];
      sg.Rows[i] := s;
      sg.Row := i+1;
      s.Free;
    end;procedure TForm1.Button3Click(Sender: TObject); {删除}
    var
      i: Integer;
    begin
      for i := sg.Row to sg.RowCount - 1 do
        sg.Rows[i] := sg.Rows[i + 1];
      sg.RowCount := sg.RowCount - 1;
    end;
      

  2.   


    {
    编号:
    函数名:    gvStrGridMove(sg: TStringGrid; FromIndex,ToIndex: Integer);
    文件名:    PubFunUnit.pas
    入口参数:  sg:目标StringGrid
                FromIndex: 源记录索引
                ToIndex:目的记录索引
    出口参数:
    引用全局变量:
    修改全局变量:                                                                            
    父函数:
    子函数:
    引用数据:
    函数功能:  在StringGrid中移动一条记录
    }
    procedure gvStrGridMove(sg: TStringGrid; FromIndex,ToIndex: Integer);
    var
      lst:TStringList;
      i,id,x:integer;
    begin
      lst :=TStringList.Create;
      id:=ToIndex;  // 目标指针
      if FromIndex>=ToIndex then x:=1 else x:=-1;  // 判断指针下移还上移
      with sg do
      begin
        while id<>FromIndex do
        begin
          lst.clear;
          for i:=0 to ColCount-1 do
            lst.Add(Cells[i,id]);
          for i:=0 to ColCount-1 do
            Cells[i,id] :=Cells[i,FromIndex];
          for i:=0 to ColCount-1 do
            Cells[i,FromIndex] :=lst.Strings[i];
          id :=id+x; // 指针转移 (x=-1 or x=1)
        end;
      end;
      FreeAndNil(lst);
    end;{
    编号:
    函数名:    procedure gvStrGridDel(sg: TStringGrid; id: integer;iDelRows: integer);
    文件名:    PubFunUnit.pas
    入口参数:  sg:目标StringGrid
                FromIndex: 源记录索引
    出口参数:
    引用全局变量:
    修改全局变量:                                                                            
    父函数:
    子函数:
    引用数据:
    函数功能:  在StringGrid中删除iDelRows条记录
    }
    procedure gvStrGridDel(sg: TStringGrid; id: integer;iDelRows: integer);
    var
        iRow,iCol:integer;
    begin
        if iDelRows < 1 then
            exit;    if id < 1 then
            exit;    with sg do
        begin
            for iRow := id to RowCount-1-iDelRows do
                for iCol :=0 to ColCount-1 do
                    Cells[iCol,iRow] := Cells[iCol,iRow + iDelRows];        if (RowCount - iDelRows) > 1 then
                RowCount := RowCount - iDelRows
            else
            begin
                RowCount := 2;
                sg.Rows[1].Text := '';
            end;
        end;
    end;{
    编号:
    函数名:    procedure gvStrGridClear(sg: TStringGrid);
    文件名:    PubFunUnit.pas
    入口参数:  sg:目标StringGrid
    出口参数:
    引用全局变量:
    修改全局变量:                                                                            
    父函数:
    子函数:
    引用数据:
    函数功能:  在StringGrid中删除所有记录
    }
    procedure gvStrGridClear(sg: TStringGrid);
    var
        iTmp: integer;
    begin
        sg.RowCount := 2;
        sg.Rows[1].Text := '';
        sg.Row := 1;
    end;