在SQL查询里面输入
ORDER BY NAME就可以了啊

解决方案 »

  1.   

    zhuchuanming:
    可以呀!详细说说好吗
      

  2.   

    可以对指定的列排序,如果你用数据库就更简单了,举个例子:
    name, id, address, mail, ....
    你想让address相似的人都靠在一起,就按address列进行排序
    select name,id,address,mail,... from tablename where ...orderby address
    然后逐条显示在stringgrid中(当然dbgrid更好)
      

  3.   

    如果没用数据库
    stringgrid没有提供排序方法,代码要自己写
      

  4.   

    Procedure GridSort(StrGrid: TStringGrid; NoColumn: Integer);
      Var Line, PosActual: Integer;
          Row: TStrings;
      begin
        //Renglon := TStringList.Create;
        For Line := 1 to StrGrid.RowCount-1 do
        Begin
          PosActual := Line;
          Row.Assign(TStringlist(StrGrid.Rows[PosActual]));
          While True do
          Begin
            If (PosActual = 0) Or (StrToInt(Row.Strings[NoColumn-1]) >=
                StrToInt(StrGrid.Cells[NoColumn-1,PosActual-1])) then
              Break;
            StrGrid.Rows[PosActual] := StrGrid.Rows[PosActual-1];
            Dec(PosActual);
          End;
          If StrToInt(Row.Strings[NoColumn-1]) < StrToInt(StrGrid.Cells[NoColumn-1,PosActual]) then
            StrGrid.Rows[PosActual] := Row;
        End;
        //Renglon.Free;
      end;
      

  5.   

    inbud(清风侠) :
    对不起,忘记说了,我没有用数据库,因为用数据库,程序的体积就大了,还要装数据引擎和组件。
    我的意思是数据都已经在stringgrid上显示好了,然后用代码如何让stringgrid直接按照相同的地址排列,谢谢!!!!!
      

  6.   

    jiaorg(jiaorg) :谢谢,可是我在用您的代码的时候GridSort(StringGrid1,2);
    ,程序执行到
          Row.Assign(TStringlist(StrGrid.Rows[PosActual]));
    的时候出错???
      

  7.   

    //将收录
    http://kingron.myetang.com/zsfunc15.htm(*//
    标题:字符网格排序
    说明:升序、降序;示例点击标题排序
    设计:Zswang
    日期:2002-04-27
    支持:[email protected]
    //*)///////Begin Source
    function StringGridRowSwap(mStringGrid: TStringGrid;
      mFromRow, mToRow: Integer): Boolean;
    var
      S: string;
    begin
      Result := False;
      if (mToRow = mFromRow) then Exit;
      if not Assigned(mStringGrid) then Exit;
      if (mFromRow < 0) or (mFromRow >= mStringGrid.RowCount) then Exit;
      if (mToRow < 0) or (mToRow >= mStringGrid.RowCount) then Exit;
      try
        S := mStringGrid.Rows[mFromRow].Text;
        mStringGrid.Rows[mFromRow].Text := mStringGrid.Rows[mToRow].Text;
        mStringGrid.Rows[mToRow].Text := S;
      except
        Exit;
      end;
      Result := True;
    end; { StringGridRowSwap }function StringGridRowSort(mStringGrid: TStringGrid;
      mColIndex: Integer; mDesc: Boolean = False): Boolean;
    var
      I, J: Integer;
    begin
      Result := False;
      if not Assigned(mStringGrid) then Exit;
      if (mColIndex < 0) or (mColIndex >= mStringGrid.ColCount) then Exit;
      for I := mStringGrid.FixedRows to mStringGrid.RowCount - 2 do
        for J := I + 1 to mStringGrid.RowCount - 1 do
          if mDesc then
            if mStringGrid.Cells[mColIndex, I] < mStringGrid.Cells[mColIndex, J] then
              StringGridRowSwap(mStringGrid, I, J)
            else
          else if mStringGrid.Cells[mColIndex, I] > mStringGrid.Cells[mColIndex, J] then
            StringGridRowSwap(mStringGrid, I, J);
      Result := True;
    end; { StringGridRowSort }
    ///////End Source///////Begin Demo
    procedure TForm1.StringGrid1MouseDown(Sender: TObject;
      Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
    {$J+}
    const
      vOldCol: Integer = -1;
    {$J-}
    var
      vCol, vRow: Integer;
    begin
      if Button = mbRight then Exit;
      TStringGrid(Sender).MouseToCell(X, Y, vCol, vRow);
      if (vRow < 0) or (vRow >= TStringGrid(Sender).FixedRows) then Exit;
      StringGridRowSort(TStringGrid(Sender), vCol, vOldCol = vCol);
      if vOldCol = vCol then
        vOldCol := - vOldCol
      else vOldCol := vCol;
    end;
    ///////End Demo