大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。
Jetbrains全系列IDE稳定放心使用
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);
}
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/186309.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...