ListView列排序功能实现
2007-01-25 15:11
316 查看
#region/***lv列排序***/
private void ColumnClick(object o, ColumnClickEventArgs e)
{
try
{
lv_examView.ListViewItemSorter = new ListViewItemComparer(e.Column,lv_examView.Sorting);
lv_examView.Sort();
if(lv_examView.Sorting == SortOrder.Ascending)
lv_examView.Sorting = SortOrder.Descending;
else
lv_examView.Sorting = SortOrder.Ascending;
}
catch(Exception err)
{
MessageBox.Show(this,err.Message,"列排序",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
#endregion
#region/***列排序类***/
class ListViewItemComparer : IComparer
{
private int col;
private SortOrder is_sortOrder = SortOrder.Ascending;
public ListViewItemComparer()
{
col=0;
}
public ListViewItemComparer(int column, SortOrder sortOrder)
{
col=column;
is_sortOrder = sortOrder;
}
public int Compare(object x, object y)
{
int result=0;
try
{
result= String.Compare(((ListViewItem)x).SubItems[col].Text,((ListViewItem)y).SubItems[col].Text);
// switch (col)
// {
//
// case 3 :
// string text=((ListViewItem)x).SubItems[col].Text.ToString();
// string texty=((ListViewItem)y).SubItems[col].Text.ToString();
// decimal dm1=ChangeBytesBack(text);
// decimal dm2=ChangeBytesBack(texty);
// result= Decimal.Compare(dm1,dm2);
// break;
//
// default :
// result= String.Compare(((ListViewItem)x).SubItems[col].Text,((ListViewItem)y).SubItems[col].Text);
// break;
// }
}
catch(Exception err)
{
throw new Exception(err.Message);
}
if (is_sortOrder == SortOrder.Descending)
return result;
else
return -result;
}
//字节转换
private decimal ChangeBytesBack(string oldstr)
{
decimal filelength=0;
try
{
string strExplain=oldstr.Substring(oldstr.Length-2);
decimal lDSize = Convert.ToDecimal(oldstr.Substring(0,oldstr.Length-3));
try
{
switch(strExplain)
{
case "KB":
filelength=lDSize*1024;
break;
case "MB":
filelength=lDSize*1024*1024;
break;
case "GB":
filelength=lDSize*1024*1024*1024;
break;
}
}
catch (Exception err)
{
throw new Exception(err.Message);
}
}
catch(Exception err)
{
throw new Exception(err.Message);
}
return filelength;
}
#endregion
}
private void ColumnClick(object o, ColumnClickEventArgs e)
{
try
{
lv_examView.ListViewItemSorter = new ListViewItemComparer(e.Column,lv_examView.Sorting);
lv_examView.Sort();
if(lv_examView.Sorting == SortOrder.Ascending)
lv_examView.Sorting = SortOrder.Descending;
else
lv_examView.Sorting = SortOrder.Ascending;
}
catch(Exception err)
{
MessageBox.Show(this,err.Message,"列排序",MessageBoxButtons.OK,MessageBoxIcon.Error);
}
}
#endregion
#region/***列排序类***/
class ListViewItemComparer : IComparer
{
private int col;
private SortOrder is_sortOrder = SortOrder.Ascending;
public ListViewItemComparer()
{
col=0;
}
public ListViewItemComparer(int column, SortOrder sortOrder)
{
col=column;
is_sortOrder = sortOrder;
}
public int Compare(object x, object y)
{
int result=0;
try
{
result= String.Compare(((ListViewItem)x).SubItems[col].Text,((ListViewItem)y).SubItems[col].Text);
// switch (col)
// {
//
// case 3 :
// string text=((ListViewItem)x).SubItems[col].Text.ToString();
// string texty=((ListViewItem)y).SubItems[col].Text.ToString();
// decimal dm1=ChangeBytesBack(text);
// decimal dm2=ChangeBytesBack(texty);
// result= Decimal.Compare(dm1,dm2);
// break;
//
// default :
// result= String.Compare(((ListViewItem)x).SubItems[col].Text,((ListViewItem)y).SubItems[col].Text);
// break;
// }
}
catch(Exception err)
{
throw new Exception(err.Message);
}
if (is_sortOrder == SortOrder.Descending)
return result;
else
return -result;
}
//字节转换
private decimal ChangeBytesBack(string oldstr)
{
decimal filelength=0;
try
{
string strExplain=oldstr.Substring(oldstr.Length-2);
decimal lDSize = Convert.ToDecimal(oldstr.Substring(0,oldstr.Length-3));
try
{
switch(strExplain)
{
case "KB":
filelength=lDSize*1024;
break;
case "MB":
filelength=lDSize*1024*1024;
break;
case "GB":
filelength=lDSize*1024*1024*1024;
break;
}
}
catch (Exception err)
{
throw new Exception(err.Message);
}
}
catch(Exception err)
{
throw new Exception(err.Message);
}
return filelength;
}
#endregion
}
相关文章推荐
- Android 仿美团网,探索ListView的A-Z字母排序功能实现选择城市
- 获取手机通讯录放入PinnedSectionListView中,按名字首字母排序,并且实现拨打电话功能。
- Android联系人列表,实现ListView的A-Z字母排序和过滤搜索功能,字母条目上顶
- Android 仿美团网,探索ListView的A-Z字母排序功能实现选择城市
- 在C#中实现对ListView点击列标题自动排序功能
- Android 实现ListView的A-Z字母排序和过滤搜索功能,实现汉字转成拼音
- Android中ListView字母排序,实现字母挤压效果以及右侧快速选中字母,搜索关键字功能
- Android 实现ListView的A-Z字母排序和过滤搜索功能,实现汉字转成拼音
- Android实现ListView的A-Z字母排序和过滤搜索功能 实现汉字转成拼音
- C# ListView 点击表头对数据进行排序功能的实现代码
- Android 实现ListView的A-Z字母排序和过滤搜索功能,实现汉字转成拼音
- Android 实现ListView的A-Z字母排序和过滤搜索功能,实现汉字转成拼音
- 在C#中实现对ListView点击列标题自动排序功能
- 单击ListView列标题实现项排序功能
- [WPF]ListView点击列头排序功能实现
- Android 实现ListView的A-Z字母排序和过滤搜索功能,实现汉字转成拼音
- Android 实现ListView的A-Z字母排序和过滤搜索功能,实现汉字转成拼音
- Android 仿联系人列表 实现ListView的A-Z字母排序和过滤搜索功能
- Android 实现ListView的A-Z字母排序和过滤搜索功能,实现汉字转成拼音
- Android 实现ListView的A-Z字母排序和过滤搜索功能,实现汉字转成拼音