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);
}
}
}
查看全文
相关阅读:
python join的用法
python json中的 dumps loads函数
ubuntu 初始配置
如何为ubuntu配置java环境
Ubuntu系统如何安装软件
取模与取余
基本数据类型
js面试题——作用域和闭包
js面试题-原型和原型链
js面试题-变量类型和计算
原文地址:https://www.cnblogs.com/dahuzizyd/p/Winform_ListView_Sort.html
最新文章
RGB与HSV
机试笔记6--查找
HDOJ5540 Secrete Master Plan
HDOJ5551 Huatuo's Medicine
HDOJ5547 SudoKu
hiho1259 A Math Problem (数位dp)
hiho1258 Osu! Master
hiho1257 Snake Carpet
hiho1255 Mysterious Antiques in Sackler Museum
hiho1249 Xiongnu's Land
热门文章
Codeforces Round #375 (Div. 2)
2016大连网络赛
Kafka与Spark的集成
Kafka 消费者组示例
Kafka 消费者组示例
日期时间API
Optional类
访问者模式
Stream
maven01-配置
Copyright © 2011-2022 走看看