814123 2017-12-18 14:18 采纳率: 25%
浏览 1100
已采纳

delphi 单击单选让LISTVIEW按时间排序?如图

图片说明

让 2017-12-18 在最上面
我现在的代码是这样的:

  lv1.Clear;
  With qry1 do
  begin
    Close;
    SQL.Clear;
    SQL.Add('select * from FJR order by fjr_dysj');
    Open;
  end;
//  qry1.First;
  while not qry1.eof do begin
    Newitem(qry1ID.Value, qry1fjr_name.Value, qry1fjr_xxdz.Value, qry1fjr_dh.Value, qry1fjr_yb.Value, qry1fjr_dysj.Value, qry1fjr_bz.Value);
    qry1.Next;    //显示窗体时候的代码LISTVIEW

  end;
  qry1.Close; 
  • 写回答

2条回答 默认 最新

  • beyoos 2017-12-20 08:02
    关注

    //ListView排序的回调函数,默认的是快速排序法,也可以自己在这里做算法

    function CustomSortProc(Item1, Item2: TListItem; ParamSort: integer): integer; stdcall;
    var
    txt1, txt2: string;
    begin
    if ParamSort <> 0 then
    begin
    try
    txt1 := Item1.SubItems.Strings[ParamSort - 1];
    txt2 := Item2.SubItems.Strings[ParamSort - 1];
    if m_bSort then
    begin
    Result := CompareText(txt1, txt2);
    end
    else
    begin
    Result := -CompareText(txt1, txt2);
    end;
    except
    end;

    end
    else
    begin
    if m_bSort then
    begin
    Result := CompareText(Item1.Caption, Item2.Caption);
    end
    else
    begin
    Result := -CompareText(Item1.Caption, Item2.Caption);
    end;
    end;
    end;

    //ListView.OnColumnClick事件
    procedure TForm1.lv1ColumnClick(Sender: TObject;
    Column: TListColumn);
    begin
    lv1.CustomSort(@CustomSortProc, Column.Index);
    //m_bSort := not m_bSort; 自动升序或降序,点一下升序,再点一下降序
    end;

    搞定!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?