有谁可以告诉我,当我在dbgrid已经查出数据了,但我想实现,点击每列的标题字段时,就可以按该列现在的数据排序呢?

解决方案 »

  1.   

    字段是固定的吗?
    如果是那样就好说了
    可以加个菜单,分升、降序
    设置Tcolumn的popupMenu
    这样就方面多了
      

  2.   


    procedure TForm1.DBGrid1MouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    begin
    edit1.text:=adoquery1.fieldbyname('你要排序的‘).asstring ;
    end;
    procedure TForm1.Button1Click(Sender: TObject);
    begin
    adoquery1.close;
    adoquery1.clear;
    adoquery1.add('select * from 你的表'); 
    adoquery1.add('order by '''+你的字段+''''); 
    end;
    呵呵,你试一下,我也没试过
      

  3.   

    Use Sort propertySpecifies the sort order of the recordset.property Sort: WideString;DescriptionSet Sort to establish or change the list of fields on which the recordset is sorted. Set sort to the name of a single field or to a comma-separated list of fields. Each field may optionally be followed by the keyword ASCENDING or DESCENDING to specify a sort direction for the field. If one of these keywords is not used, the default sort direction for the field is ascending. Set Sort to an empty string to reset the recordset to the sort order originally used when the recordset抯 data was first retrieved.ADOQuery1.Sort := 'LastName ASC, DateDue DESC'Read Sort to determine the field (or fields) on which the recordset is sorted.Note: If the cursor is client-side (the dataset component抯 CursorLocation property or that of an associated TADOConnection component is clUseClient) and no index already exists matching the requested field sort order, a temporary index is created. Resetting the sort order by setting Sort to an empty string automatically deletes the temporary index.
      

  4.   

    您也可以使用第三方组件:EhLib,它支持列标题排序,不需要写代码。