listview按列自动排序的一点补充

摘要:   使listview按列自动排序的例子网上有很多,MSDN上面也有,但是似乎都没指出这样一种情况:当我们点击列头进行排序时,如果里面有一子项不存在或者多项不存在,该怎么比较?前两天我就碰到了这样的情况,现在,我把自己的一些心得拿出来和大家分享.

         大的就不用说了,我们都知道要实现listview的自定义排序,必须编写一个实现 接口的类,并将
ListViewItemSorter 属性设置为该类的一个对象.起初,我按照示例编写了一个自定义排序的类,并在listview的ColumnClick 事件下添加了相应代码.代码如下:
      1.实现排序的类:
None.gif public   class  ColumnSort:IComparer
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif
InBlock.gif        
private int colNum;
InBlock.gif        
private bool bAscend = true;
InBlock.gif
InBlock.gif        
public bool Ascend
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
get
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
return bAscend;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
set
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                bAscend 
= value;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif        
public ColumnSort(int sortColun)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.colNum = sortColun;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public int Compare(object A, object B)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            System.Windows.Forms.ListViewItem listItemA 
= (System.Windows.Forms.ListViewItem)A;
InBlock.gif            System.Windows.Forms.ListViewItem listItemB 
= (System.Windows.Forms.ListViewItem)B;
InBlock.gif            
int result;           
InBlock.gif            
//对相邻的两项进行比较
InBlock.gif
            result = String.Compare(listItemA.SubItems[colNum].ToString(), listItemB.SubItems[colNum].ToString());
InBlock.gif            
if (bAscend == true)
InBlock.gif                
return result;
InBlock.gif            
else
InBlock.gif                
return (-1* result;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
ExpandedBlockEnd.gif    }

    2.主窗体      
None.gif public  partial  class  Form1 : Form
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public Form1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitializeComponent();
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void Form1_Load(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.listView1.Items.Add("kandy");
InBlock.gif            
this.listView1.Items[0].SubItems.Add("0");
InBlock.gif            
this.listView1.Items[0].SubItems.Add("11");
InBlock.gif            
this.listView1.Items[0].SubItems.Add("10");
InBlock.gif
InBlock.gif            
this.listView1.Items.Add("list");
InBlock.gif            
this.listView1.Items[1].SubItems.Add("1");
InBlock.gif            
this.listView1.Items[1].SubItems.Add("22");
InBlock.gif            
this.listView1.Items[1].SubItems.Add("44");
InBlock.gif
InBlock.gif            
this.listView1.Items.Add("jsjlili");
InBlock.gif            
this.listView1.Items[2].SubItems.Add("1");
InBlock.gif            
this.listView1.Items[2].SubItems.Add("33");
InBlock.gif
InBlock.gif            
this.listView1.Items.Add("ff");
InBlock.gif            
this.listView1.Items[3].SubItems.Add("99");
InBlock.gif
InBlock.gif            
InBlock.gif            
//this.listView1.Items[4].SubItems.Add("0");
InBlock.gif            
//this.listView1.Items
InBlock.gif            
//this.listView1.Items[4].SubItems.Add("77");
InBlock.gif

InBlock.gif           
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
private void listView1_ColumnClick(object sender, ColumnClickEventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            ColumnSort colSort 
= new ColumnSort(e.Column);
InBlock.gif            
int iRow = this.listView1.Columns.Count;
InBlock.gif            
int iCol = this.listView1.Items.Count;
InBlock.gif
InBlock.gif
InBlock.gif            colSort.Ascend 
= (this.listView1.Sorting == SortOrder.Ascending);
InBlock.gif            
if (colSort.Ascend == true)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.listView1.Sorting = SortOrder.Descending;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.listView1.Sorting = SortOrder.Ascending;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//this.listView1.Sort();
InBlock.gif
            this.listView1.ListViewItemSorter = colSort;
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

None.gif

     
      大家仔细看这个Form1类,会发现listview1第四行只有两项,运行程序时当我们点击第一列,第二列进行排序是不会出问题,但是如果我们点击了第三列或者第四列的时候就会抛出 ArgumentOutOfRangeException异常,仔细一看我们就明白了,原来第四项根本就只有2个子项,点击第三列或者第四列的时候,listItemA.SubItems[colNum]是根本不存在的,所以程序报错了.经过考虑,我的做法如下,首先判断listviewitem中是否有空项(即不存在的项),如果有则增加一项并将内容初始化为空,然后再进行比较.
   在ColumnSort类中的Compare函数中插入如下代码:

None.gif int  iColA  =  listItemA.SubItems.Count;
None.gif            
int  iColB =  listItemB.SubItems.Count;
None.gif            
// 判断listviewitem中是否有空项,如果有空项则将其置为空,然后再进行比较
None.gif
             if  (iColA.CompareTo(iColB)  >   0 )
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                
for (int i = 1; i <= iColA - iColB; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    listItemB.SubItems.Add(
"");
ExpandedSubBlockEnd.gif                }

ExpandedBlockEnd.gif            }

None.gif            
else
ExpandedBlockStart.gifContractedBlock.gif            
dot.gif {
InBlock.gif                
for (int j = 1; j <= iColB - iColA; j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    listItemA.SubItems.Add(
"");
ExpandedSubBlockEnd.gif                }

ExpandedBlockEnd.gif            }

现在我们就可以进行比较而不会出现刚才那个错误了.

小结:不知道大家看懂了不,多提点意见了,个人觉得这个还是有点实用的,欢迎指点.
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值