delphi6.0和sql server 2000环境下,通过table组件连接一个sql数据库的表,再通过dbgrid组件显示表中的记录,怎样在dbgrid显示的时候自动按表的某一字段排序呢?请高手指点!

解决方案 »

  1.   

    设置索引 indexfieldname如果用query,就用order by
      

  2.   

    一般在查询语句中用order by [要排序的字段名]
      

  3.   

    如果前台排序,可以写代码设置sort属性
    如果后台排序,可以在sql于今中增加order by
      

  4.   

    我都说了是用table组件,怎么用sql语言?还有,我也找过dbgrid和table的属性,但就是找不到sort属性啊
      

  5.   

    楼主﹐那就设置索引吧﹐可以直接在SQL里面設置﹐也可以代碼設置﹗代码设置例子:
    ...
      private
        { Private declarations }
        procedure setindex;
    ...
    procedure TPMMtInfo.setindex;
    begin
     //设置排序字段
       table1.indexfieldnames:='newitemcode';
       table1.IndexFieldNames:='itemcode';
       table2.indexfieldnames:='newmtcode';
     end;
      

  6.   

    再SQL语句里面加上 order by [字段] 就要得了
      

  7.   

    在DBGrid的TitleClick事件中写代码.var
      i : integer;
      sCondition : string;
    begin
      if not self.aqMaster.Active then exit;  i := Column.Index;
      if trim(Dbgrid1.Columns[i].FieldName) = 'memo' then exit;
      if bSort then
      begin
        sCondition := trim(DBGrid1.Columns[i].FieldName) + ' DESC';
        bSort := false;
      end
      else
      begin
        sCondition := trim(DBGrid1.Columns[i].FieldName) + ' ASC';
        bSort := true;
      end;  aqMaster.Sort := sCondition;
      aqMaster.First;