日期:2014-05-20  浏览次数:20951 次

ListView控件问题(在线等)
Winform   窗体上有一个ListView   2个Buttons   1个label  
//****************
private   void   button2_Click(object   sender,   System.EventArgs   e)
{
try
{
this.listView1   .Clear   ();
this.listView1.Columns   .Add   ( "名 ",50,HorizontalAlignment.Left);
this.listView1.Columns   .Add   ( "姓 ",50,HorizontalAlignment.Left);
this.listView1.Columns   .Add   ( "级 ",50,HorizontalAlignment.Left);
this.listView1.Columns   .Add   ( "tt ",50,HorizontalAlignment.Left);
for(int   i=0;i <5;i++)
{
string   []sub={ "ww "+i, "ee "+i, "rr "+i, "ss "+i};
this.listView1   .Items   .Insert   (this.listView1.Items   .Count   ,new   ListViewItem   (sub));
}
}
catch(Exception   Error)
{
this.label1   .Text   =Error.Message   ;
}
}
//***************
private   void   button1_Click(object   sender,   System.EventArgs   e)
{
try
{
this.listView1   .Clear   ();
this.listView1.Columns   .Add   ( "名称 ",50,HorizontalAlignment.Left);
this.listView1.Columns   .Add   ( "姓名 ",50,HorizontalAlignment.Left);
this.listView1.Columns   .Add   ( "级别 ",50,HorizontalAlignment.Left);
for(int   i=0;i <5;i++)
{
string   []sub={ "Ni "+i, "Wo "+i, "Ta "+i};
this.listView1   .Items   .Insert   (this.listView1.Items   .Count   ,new   ListViewItem   (sub));
}
}
catch(Exception   Error)
{
this.label1   .Text   =Error.Message   ;
}
}
//****************
private   int   CurrentColumn   =   -1;   //记录上次点击的列号
private   bool   b_Convert   =   false;   //记录正逆序信息
//****************
private   void   listView1_ColumnClick_1(object   sender,   System.Windows.Forms.ColumnClickEventArgs   e)
{
if(e.Column   ==   CurrentColumn)
{
b_Convert   =   !   b_Convert;

}
else
{
CurrentColumn   =   e.Column;
b_Convert   =   false;

}
this.listView1   .ListViewItemSorter   =   new   ListSortBySubItem(this.CurrentColumn   ,b_Convert);
}
//******************排序
public   class   ListSortBySubItem     :   IComparer
{
private   int   m_Column   =   0;
private   int   m_SortType   =   0;//排序类型
private   bool   m_asc   =   true;
public   ListSortBySubItem(int   Column,bool   bAsc)
{
m_Column   =   Column;
m_asc   =   bAsc;
}
public   ListSortBySubItem(int   Column,bool   bAsc,   int   SortType)
{
m_Column   =   Column;
m_SortType   =   SortType;
m_asc   =   bAsc;
}
int   IComparer.Compare(object   x,   object   y)
{
string   item1   =   ((ListViewItem)x).SubItems[m_Column].Text.Trim();
string   item2   =   ((ListViewItem)y).SubItems[m_Column].Text.Trim();
int   intSort   =   0;
if(!m_asc)//反序
{