814123 2017-12-18 14:18 采纳率: 33.3%
浏览 1096
已采纳

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条)

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格