zoukankan
html css js c++ java
在Winform中如何实现ListView排序
Winform
中的
ListView
排序是一种常用的功能,下面是例子代码,放上来留个备份
using
System;
using
System.Windows.Forms;
using
System.Drawing;
using
System.Collections;
namespace
ListViewSortFormNamespace
{
public
class
ListViewSortForm : Form
{
private
ListView listView1;
public
ListViewSortForm()
{
ListViewItem listViewItem1
=
new
ListViewItem(
new
string
[]
{
"
Banana
"
,
"
a
"
,
"
b
"
,
"
c
"
}
,
-
1
, Color.Empty, Color.Yellow,
null
);
ListViewItem listViewItem2
=
new
ListViewItem(
new
string
[]
{
"
Cherry
"
,
"
v
"
,
"
g
"
,
"
t
"
}
,
-
1
, Color.Empty, Color.Red,
new
Font(
"
Microsoft Sans Serif
"
,
8.25F
, FontStyle.Regular, GraphicsUnit.Point, ((System.Byte)(
0
))));
ListViewItem listViewItem3
=
new
ListViewItem(
new
string
[]
{
"
Apple
"
,
"
h
"
,
"
j
"
,
"
n
"
}
,
-
1
, Color.Empty, Color.Lime,
null
);
ListViewItem listViewItem4
=
new
ListViewItem(
new
string
[]
{
"
Pear
"
,
"
y
"
,
"
u
"
,
"
i
"
}
,
-
1
, Color.Empty, Color.FromArgb(((System.Byte)(
192
)), ((System.Byte)(
128
)), ((System.Byte)(
156
))),
null
);
this
.listView1
=
new
ListView();
this
.listView1.Sorting
=
SortOrder.None;
this
.listView1.View
=
View.Details;
this
.listView1.Columns.Add(
new
ColumnHeader());
this
.listView1.Columns[
0
].Text
=
"
Column 1
"
;
this
.listView1.Columns[
0
].Width
=
100
;
listView1.Columns.Add(
new
ColumnHeader());
listView1.Columns[
1
].Text
=
"
Column 2
"
;
listView1.Columns.Add(
new
ColumnHeader());
listView1.Columns[
2
].Text
=
"
Column 3
"
;
listView1.Columns.Add(
new
ColumnHeader());
listView1.Columns[
3
].Text
=
"
Column 4
"
;
this
.SuspendLayout();
this
.listView1.Items.AddRange(
new
ListViewItem[]
{listViewItem1,
listViewItem2,
listViewItem3,
listViewItem4}
);
this
.listView1.Location
=
new
Point(
10
,
10
);
this
.listView1.Name
=
"
listView1
"
;
this
.listView1.Size
=
new
Size(
300
,
100
);
this
.listView1.TabIndex
=
0
;
this
.listView1.LabelEdit
=
true
;
this
.listView1.ColumnClick
+=
new
ColumnClickEventHandler(ColumnClick);
this
.ClientSize
=
new
Size(
400
,
400
);
this
.Controls.AddRange(
new
Control[]
{
this
.listView1}
);
this
.Name
=
"
ListViewSortForm
"
;
this
.Text
=
"
Sorted ListView Control
"
;
this
.ResumeLayout(
false
);
}
//
ColumnClick event handler.
private
void
ColumnClick(
object
o, ColumnClickEventArgs e)
{
this
.listView1.ListViewItemSorter
=
new
ListViewItemComparer(e.Column);
}
[System.STAThreadAttribute()]
public
static
void
Main()
{
Application.Run(
new
ListViewSortForm());
}
}
//
自定义排序算法
class
ListViewItemComparer : IComparer
{
private
int
col;
public
ListViewItemComparer()
{
col
=
0
;
}
public
ListViewItemComparer(
int
column)
{
col
=
column;
}
public
int
Compare(
object
x,
object
y)
{
return
String.Compare(((ListViewItem)x).SubItems[col].Text, ((ListViewItem)y).SubItems[col].Text);
}
}
}
查看全文
相关阅读:
内联元素间的间隔
事件处理程序DOM0,DOM2,IE的区别总结
open live writer下载安装
sublime3下载安装及常用插件、浏览器预览设置
常用的清除浮动的方法
input中的name,value以及label中的for
利用fiddler将本地网页放到某个域下
Date类型常用概念及方法总结(1)
构建之法 第六章 敏捷流程
javascript 入门之 新窗口打开网站
原文地址:https://www.cnblogs.com/dahuzizyd/p/Winform_ListView_Sort.html
最新文章
ELK 日志系统入门及通过 Docker 部署
Linux shell 入门
Harbor
ping, telnet, tcping 命令使用及对比
下载工具 qBittorrent 使用
Git
Git
Nginx 官网文档翻译汇总
Jenkins 官网文档翻译汇总
Docker 官网文档翻译汇总
热门文章
go tour
反射工具类(调用父类的方法和字段)
父类如何获取子类传递的泛型
Logger.getLogger和LogFactory.getLog的区别
线程流量控制工具之Semaphore
线程同步工具之CountDownLatch
threadlocal
将本地jar包安装进入maven仓库
每天一个linux命令1之scp
简单易懂的排序算法演示
Copyright © 2011-2022 走看看