如何更改TListGroups中的组顺序?

时间:2018-05-07 17:24:59

标签: listview delphi delphi-2009

我的TListView样式为vsReport,可见群组。这些组按创建顺序显示。但是我想制作两个按钮(向上和向下)并将所选组移动到另一个位置(在运行时)。有可能吗?

2 个答案:

答案 0 :(得分:5)

您可以通过更改组项的tables <- c("dt1","dt2","dt3","dt4") dflist <- lapply(mget(tables),function(x)x[ID %in% vec,]) dflist <- setNames(dflist, tables) 属性来执行此操作。以下代码演示了用法:

Index

脚注:当然,这可以(应该)重构如下:

procedure TForm1.btnMoveUpClick(Sender: TObject);
var
  itm: TListItem;
  i: Integer;
begin
  itm := ListView1.Selected;
  if Assigned(itm) then
    for i := 0 to ListView1.Groups.Count - 1 do
      if ListView1.Groups[i].GroupID = itm.GroupID then
      begin
        if ListView1.Groups[i].Index > 0 then
          ListView1.Groups[i].Index := ListView1.Groups[i].Index - 1;
        break;
      end;
end;

procedure TForm1.btnMoveDownClick(Sender: TObject);
var
  itm: TListItem;
  i: Integer;
begin
  itm := ListView1.Selected;
  if Assigned(itm) then
    for i := 0 to ListView1.Groups.Count - 1 do
      if ListView1.Groups[i].GroupID = itm.GroupID then
      begin
        if ListView1.Groups[i].Index < ListView1.Groups.Count - 1 then
          ListView1.Groups[i].Index := ListView1.Groups[i].Index + 1;
        break;
      end;
end;

答案 1 :(得分:4)

是。通过更改组项Index