代码雪儿的问题:谁有排序ListView中数据的代码?麻烦给贴上来,谢谢~ (分数不多了~~抱歉!) (10分)

  • 主题发起人 主题发起人 代码雪儿
  • 开始时间 开始时间

代码雪儿

Unregistered / Unconfirmed
GUEST, unregistred user!
·ListView是vsReport的排列方式~
·点击Column的时候,实现此列排序,在点又反着排序。
·像QQ里查询好友的界面一样,最好在加上格格。
·最好还有字体不同颜色的显示
·分数不多了,谢谢回答
·签名:美女雪儿
 
我有,呵呵,不过除非你说你是恐龙,或者把你的照片公开,我是不会给你的,
其他的DFW认为这个主意如何?
 
小张,这是技术论坛,你不回答总会有好心人回答了--不要逼我呀~呵呵
 
如此简单的代码都懒得写你还叫什么“代码雪儿”?
同意张无忌 [:D][:D][:D]
 
看你可怜,给你一份,是我的代码里的,我留了个BUG给你,
function SortByA(Item1, Item2: TListItem; //从小到大进行排序
ColumnIndex: integer): integer; stdcall;
begin
if ColumnIndex = 0 then
Result := CompareText(Item1.Caption,Item2.Caption)
else
if Item1.SubItems[ColumnIndex-1] <= Item2.SubItems[ColumnIndex-1]
then Result := -1
else Result := 1;
end;

function SortByB(Item1, Item2: TListItem; //从大到下进行排序
ColumnIndex: integer): integer; stdcall;
begin
if ColumnIndex = 0 then
Result := CompareText(Item1.Caption,Item2.Caption)
else
if Item1.SubItems[ColumnIndex-1] <= Item2.SubItems[ColumnIndex-1]
then Result := 1
else Result := -1;
end;

procedure TfmLogForm.recordListColumnClick(Sender: TObject;
Column: TListColumn);
begin
if reCordList.Items.Count<1 then exit; //如果无记录不排序

recordList.Items.BeginUpdate; //锁定ListView的刷新

if Column.Index=iList then //如果还是点排序的Column
begin
if Column.ImageIndex=4 then //根据不同的图片头来确定如何调用函数
begin
recordList.CustomSort(@SortByA,Column.Index);
Column.ImageIndex:=5;
end
else
begin
recordList.CustomSort(@SortByB,Column.Index);
Column.ImageIndex:=4;
end;
end
else
begin //如果点了新的Column头
if iList<>-1 then
recordList.Columns.Items[iList].ImageIndex:=-1; //还原原来的Column的图片索引
recordList.CustomSort(@SortByA,Column.Index); //进行排序
Column.ImageIndex:=5;

end;
iList:=Column.Index;

recordList.Items.EndUpdate; //锁定ListView的刷新
end;
 
嘻嘻~~,小张真好,,,谢谢~~~
·什么?还留Bug给我?那别的DFW怎么用呀?
·晕~你怎么这样呀?
·不光排序,还有其他的条件呢?
 
还有其他条件如何实现?
 
·ListView是vsReport的排列方式~
//ListView.ViewStyle := vsReport;

·点击Column的时候,实现此列排序,在点又反着排序。
function CustSortAsc(Item1, Item2: TListItem;
ColIndex: Integer): Integer; stdcall;
var
Item1Str: string;
Item2Str: string;
// TmpExt: Extended;
begin
Result := 0;
Item1Str := '';
Item2Str := '';

if ColIndex < 0 then
Exit;

if ColIndex = 0 then
begin
Item1Str := Item1.Caption;
Item2Str := Item2.Caption;
end
else
begin
if ColIndex <= Item1.SubItems.Count then
Item1Str := Item1.SubItems[ColIndex - 1];

if ColIndex <= Item2.SubItems.Count then
Item2Str := Item2.SubItems[ColIndex - 1];
end;

{if ColIndex = 1 then //此列是自定义格式字符串,用UnFormatSize函数格式化后比较
Result := UnFormatSize(Item1Str) - UnFormatSize(Item2Str)
else if ColIndex = 5 then //此列是日期,故转成时间再比较
begin
TmpExt := StrToDateTime(Item1Str) - StrToDateTime(Item2Str);
if TmpExt > 0 then
Result := 1
else if TmpExt < 0 then
Result := -1
else
Result := 0;
end
else } //默认值是文本比较
Result := CompareText(Item1Str, Item2Str);

if (Result > 0) and (Item2Str = '') then
Result := -1
else if (Result < 0) and (Item1Str = '') then
Result := 1;
end;

function CustSortDesc(Item1, Item2: TListItem;
ColIndex: Integer): Integer; stdcall;
begin
Result := CustSortAsc(Item1, Item2, ColIndex);
Result := Result * -1;
end;

procedure TForm1.ListView1ColumnClick(Sender: TObject;
Column: TListColumn);
begin
if Column.ImageIndex = 1 then
begin
ListView1.CustomSort(@CustSortDesc, Column.Index);
Column.ImageIndex := 2;
end
else
begin
ListView1.CustomSort(@CustSortAsc, Column.Index);
Column.ImageIndex := 1;
end;
end;


·像QQ里查询好友的界面一样,最好在加上格格。
//ListView1.GridLines := True;

·最好还有字体不同颜色的显示
// ???

·分数不多了,谢谢回答
·签名:美女雪儿
//[:D][:D][:D]
 
To xianjun:
字体颜色不同是各列的字体颜色不同,就像QQ查询好友一样的界面:)
谢谢~
 
哦!
procedure TForm1.ListView1CustomDrawItem(Sender: TCustomListView;
Item: TListItem; State: TCustomDrawState; var DefaultDraw: Boolean);
begin
Sender.Canvas.Font.Color := clBlack;
end;

procedure TForm1.ListView1CustomDrawSubItem(Sender: TCustomListView;
Item: TListItem; SubItem: Integer; State: TCustomDrawState;
var DefaultDraw: Boolean);
begin
case SubItem of
1:
Sender.Canvas.Font.Color := clGreen;
2:
Sender.Canvas.Font.Color := clNavy;
3:
Sender.Canvas.Font.Color := clYellow;
else
Sender.Canvas.Font.Color := clGray;
end;
end;

 
xianjun人真不错,比那姓张的强多了,谢谢~~~~((啵))
 
这年头什么鸟儿都有
 
你们两个的代码执行起来都有一个毛病:点击之后,Column的Caption就成空了。
怎么回事情?
 
这就是所谓的bug了
 
xianjun也给我留bug了?
 
反复看,反复测试,就是Caption总是给去掉。。。。????????
 
有这回事? 偶看看
 
是你看错了吧, 把Column拉长一点看看, 那是因为设置了ItemIndex,前面多了图标位置
看来你没有设置ListView的StateImages
那就这样吧,换成下面这种方式:
if Column.Tag = 1 then
begin
ListView1.CustomSort(@CustSortDesc, Column.Index);
Column.Tag := 2;
end
else
begin
ListView1.CustomSort(@CustSortAsc, Column.Index);
Column.Tag := 1;
end;
 
TMD,一个心理变态的家伙的帖子都有人回
 
后退
顶部