日历

2008 9.6 Sat
 123456
78910111213
14151617181920
21222324252627
282930    
«» 2008 - 9 «»

日志分类

文章搜索

日志文章

2007年07月06日 08:39:57

c# listView 鼠标点击交换SubItem的小程序

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication2
{
public partial class Form1 : Form
{
#region 元素
int iOld = -1;
bool b = false;
#endregion

#region 构造函数
public Form1()
{
InitializeComponent();

this.FormatLsv(this.listView1);
this.LoadData();
}
#endregion

#region 格式化列表
private void FormatLsv(System.Windows.Forms.ListView lst)
{
lst.Columns.Clear();
lst.Items.Clear();

lst.View = View.Details;
lst.AllowColumnReorder = true;
lst.FullRowSelect = true;

lst.GridLines = true;
lst.Sorting = SortOrder.Ascending;
lst.Columns.Add("id", 0, HorizontalAlignment.Left);

lst.Columns.Add("id", 50, HorizontalAlignment.Left);
lst.Columns.Add("name", 80, HorizontalAlignment.Left);

lst.Font = new Font("宋体", 10);
lst.HeaderStyle = System.Windows.Forms.ColumnHeaderStyle.Clickable;
lst.BackColor = Color.White;
lst.ForeColor = Color.Black;
lst.Sort();
}
#endregion

#region 加载数据
private void LoadData()
{
this.listView1.Items.Clear();

for (int j = 10; j > 0; j--)
{
ListViewItem item1 = new ListViewItem();
item1.SubItems.Add((j.ToString()));
item1.SubItems.Add(string.Format("第{0}个", j.ToString()));
this.listView1.Items.Add(item1);
}
}
#endregion

#region 事件
private void listView1_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.listView1.SelectedItems.Count == 1)
{
int i = this.listView1.SelectedItems[0].Index;
if (this.iOld != -1)
{
ListViewItem.ListViewSubItem obj = this.listView1.SelectedItems[0].SubItems[2];
this.listView1.SelectedItems[0].SubItems[2] = this.listView1.Items[iOld].SubItems[2];
this.listView1.Items[iOld].SubItems[2] = obj;
}
if (b)
{
this.iOld = -1;
}
else
{
this.iOld = i;
}
b = !b;
}
}
#endregion
}
}

类别: 无分类 |  评论(0) |  浏览(6052) |  收藏
发表评论