namespace WindowsFormsTest
{
partial class FormListView
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.listView1 = new System.Windows.Forms.ListView();
this.cmsOne = new System.Windows.Forms.ContextMenuStrip(this.components);
this.toolStripMenuItem1 = new System.Windows.Forms.ToolStripMenuItem();
this.修改时间ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.cmsOne.SuspendLayout();
this.SuspendLayout();
//
// listView1
//
this.listView1.ContextMenuStrip = this.cmsOne;
this.listView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.listView1.Location = new System.Drawing.Point(0, 0);
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(357, 255);
this.listView1.TabIndex = 0;
this.listView1.UseCompatibleStateImageBehavior = false;
this.listView1.View = System.Windows.Forms.View.Details;
//
// cmsOne
//
this.cmsOne.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripMenuItem1});
this.cmsOne.Name = "cmsOne";
this.cmsOne.Size = new System.Drawing.Size(125, 26);
this.cmsOne.Text = "排列图标";
//
// toolStripMenuItem1
//
this.toolStripMenuItem1.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.修改时间ToolStripMenuItem});
this.toolStripMenuItem1.Name = "toolStripMenuItem1";
this.toolStripMenuItem1.Size = new System.Drawing.Size(124, 22);
this.toolStripMenuItem1.Text = "排列图标";
//
// 修改时间ToolStripMenuItem
//
this.修改时间ToolStripMenuItem.Name = "修改时间ToolStripMenuItem";
this.修改时间ToolStripMenuItem.Size = new System.Drawing.Size(124, 22);
this.修改时间ToolStripMenuItem.Text = "修改时间";
this.修改时间ToolStripMenuItem.Click += new System.EventHandler(this.修改时间ToolStripMenuItem_Click);
//
// FormListView
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(357, 255);
this.Controls.Add(this.listView1);
this.Name = "FormListView";
this.Text = "FormListView";
this.Load += new System.EventHandler(this.FormListView_Load);
this.cmsOne.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.ContextMenuStrip cmsOne;
private System.Windows.Forms.ToolStripMenuItem toolStripMenuItem1;
private System.Windows.Forms.ToolStripMenuItem 修改时间ToolStripMenuItem;
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
namespace WindowsFormsTest
{
public partial class FormListView : Form
{
public FormListView()
{
InitializeComponent();
}
private void 修改时间ToolStripMenuItem_Click(object sender, EventArgs e)
{
listView1.ListViewItemSorter = new SortByTime();
}
private void FormListView_Load(object sender, EventArgs e)
{
this.listView1.Columns.Add("测试", 80, HorizontalAlignment.Left);
this.listView1.Columns.Add("修改时间", 100, HorizontalAlignment.Center);
this.listView1.Items.AddRange(
new ListViewItem[]{new ListViewItem(new string[]{"A",DateTime.Now.ToString()}),
new ListViewItem(new string[]{"B",DateTime.Now.AddDays(-1.00).ToString()})});
}
}
public class SortByTime : IComparer
{
public int Compare(object x, object y)
{
if (DateTime.Parse(((ListViewItem)x).SubItems[1].Text) > DateTime.Parse(((ListViewItem)y).SubItems[1].Text))
{
return 1;
}
else if (DateTime.Parse(((ListViewItem)x).SubItems[1].Text) < DateTime.Parse(((ListViewItem)y).SubItems[1].Text))
{
return -1;
}
else
return 0;
}
}
}
至于你说的下次打开保持原来的按修改时间排序,你可以把用户最后操作的方式记在一个配置文件中,软件启动时读取配置文件。