ListView虚拟模式封装
public class ListViewAH : ListViewEx
{
#region 虚拟模式相关操作 ///<summary>
/// 前台行集合
///</summary>
public List<ListViewItem> CurrentCacheItemsSource
{ get; private set; } public ListViewAH()
{
this.CurrentCacheItemsSource = new List<ListViewItem>();
this.VirtualMode = true;
this.RetrieveVirtualItem += new RetrieveVirtualItemEventHandler(listView_RetrieveVirtualItem);
} ///<summary>
/// 重置listview集合
///</summary>
///<param name="items"></param>
public void ReSet(IList<ListViewItem> items)
{
this.CurrentCacheItemsSource.Clear();
this.CurrentCacheItemsSource = new List<ListViewItem>();
if ( null != items )
{
if ( items.Count > )
{
foreach ( var item in items )
{
this.CurrentCacheItemsSource.Add(item);
}
}
this.VirtualListSize = this.CurrentCacheItemsSource.Count;
}
else
this.VirtualListSize = ;
} /// <summary>
/// 追加项
/// </summary>
/// <param name="l"></param>
public void AppendListViewItem(IList<ListViewItem> l)
{
if ( this.CurrentCacheItemsSource == null || this.CurrentCacheItemsSource.Count <= )
{
ReSet(l);
return;
}
foreach ( var item in l )
{
this.CurrentCacheItemsSource.Add(item);
}
this.VirtualListSize = this.CurrentCacheItemsSource.Count;
} /// <summary>
/// 清空所有项
/// </summary>
public void ClearAllItem()
{
if ( this.CurrentCacheItemsSource == null || this.CurrentCacheItemsSource.Count <= )
{
return;
}
this.CurrentCacheItemsSource.Clear();
this.VirtualListSize = this.CurrentCacheItemsSource.Count;
} /// <summary>
/// 移除当前选中的项
/// </summary>
/// <param name="l"></param>
public void RemoveListViewItem(IList<ListViewItem> l)
{
if ( this.CurrentCacheItemsSource == null || this.CurrentCacheItemsSource.Count <= )
{
return;
}
foreach ( var item in l )
{
this.CurrentCacheItemsSource.Remove(item);
}
this.VirtualListSize = this.CurrentCacheItemsSource.Count;
} ///<summary>
/// 虚拟模式事件
///</summary>
///<param name="sender"></param>
///<param name="e"></param>
private void listView_RetrieveVirtualItem(object sender , RetrieveVirtualItemEventArgs e)
{
if ( this.CurrentCacheItemsSource == null || this.CurrentCacheItemsSource.Count == )
{
return;
} ListViewItem lv = this.CurrentCacheItemsSource[e.ItemIndex];
e.Item = lv;
} ///<summary>
/// 获取选中的第一行的文本值
///</summary>
///<param name="key"></param>
///<returns></returns>
public string FirstSelectItemValue(string key)
{
int i = GetColumnsIndex(key); return this.CurrentCacheItemsSource[this.SelectedIndices[]].SubItems[i].Text;
} ///<summary>
/// 获取列名的索引
///</summary>
///<param name="key"></param>
///<returns></returns>
public int GetColumnsIndex(string key)
{
int i = ;
for ( ; i < this.Columns.Count ; i++ )
{
if ( this.Columns[i].Name == key )
{
break;
}
} return i;
} ///<summary>
/// 获取选中项
///</summary>
///<returns></returns>
public List<ListViewItem> GetSelectItem()
{
List<ListViewItem> l = new List<ListViewItem>();
if ( this.SelectedIndices.Count <= ) return null;
foreach ( var item in this.SelectedIndices )
{
l.Add(this.CurrentCacheItemsSource[int.Parse(item.ToString())]);
}
return l;
} ///<summary>
/// 获取选中行的某列集合
///</summary>
///<param name="key"></param>
///<returns></returns>
public List<string> GetListViewField(string key)
{
List<string> ids = new List<string>(); foreach ( var item in this.SelectedIndices )
{
string id = this.CurrentCacheItemsSource[int.Parse(item.ToString())].SubItems[GetColumnsIndex(key)].Text;
ids.Add(id);
}
return ids;
} private ListViewItemComparer mySorter;
///<summary>
/// 排序
///</summary>
///<param name="e"></param>
protected override void OnColumnClick(ColumnClickEventArgs e)
{
base.OnColumnClick(e); if ( this.mySorter == null )
{
this.mySorter = new ListViewItemComparer(e.Column , SortOrder.Ascending);
}
else
{
if ( this.mySorter.SortColumn == e.Column )
{
if ( this.mySorter.Order == SortOrder.Ascending )
{
this.mySorter.Order = SortOrder.Descending;
}
else
{
this.mySorter.Order = SortOrder.Ascending;
}
}
else
{
this.mySorter.SortColumn = e.Column;
this.mySorter.Order = SortOrder.Ascending;
} this.CurrentCacheItemsSource.Sort(this.mySorter); this.Invalidate();
}
}
#endregion #region ListView排序逻辑
///<summary>
/// ListView排序逻辑
///</summary>
private class ListViewItemComparer : System.Collections.Generic.IComparer<ListViewItem>
{
public ListViewItemComparer()
{
this.SortColumn = ;
this.Order = SortOrder.None;
} public ListViewItemComparer(int column)
: this()
{
this.SortColumn = column;
} ///<summary>
///
///</summary>
///<param name="column">哪列</param>
///<param name="sortOrder">排序方式</param>
public ListViewItemComparer(int column , SortOrder sortOrder)
: this(column)
{
Order = sortOrder;
} #region IComparer 成员
public int Compare(ListViewItem x , ListViewItem y)
{
int result = ;
string c1 = "";
string c2 = ""; try
{
c1 = x.SubItems[this.SortColumn].Text;
c2 = y.SubItems[this.SortColumn].Text;
}
catch ( Exception ex )
{
// MessageBox.Show(ex.Message);
return ;
}
result = string.Compare(c1 , c2);
if ( this.Order == SortOrder.Ascending )
{
return result;
}
else if ( this.Order == SortOrder.Descending )
{
return ( -result );
}
else
{
return ;
}
}
#endregion ///<summary>
/// 当前排序列
///</summary>
public int SortColumn
{
get;
set;
} ///<summary>
/// 当前列排序方式
///</summary>
public SortOrder Order
{
get;
set;
}
}
#endregion
}
使用listView普通模式加载大数据,会比较慢,即时使用线程加载,也会出现不断的刷新,故用虚拟模式下加载,速度快
本例中继承第三方控件的ListViewEx控件,也可以继承系统自带的ListView
ListView虚拟模式封装的更多相关文章
- WinForm ListView虚拟模式加载数据 提高加载速度
将VirtualMode 属性设置为 true 会将 ListView 置于虚拟模式.控件不再使用Collection.Add()这种方式来添加数据,取而代之的是使用RetrieveVirtualIt ...
- 实现虚拟模式的动态数据加载Windows窗体DataGridView控件 .net 4.5 (一)
实现虚拟模式的即时数据加载Windows窗体DataGridView控件 .net 4.5 原文地址 :http://msdn.microsoft.com/en-us/library/ms171624 ...
- Skyline基本操作模式封装
skyline基本操作模式 项目中基于skyline的浏览器插件进行二次开发,基本的业务操作模式如下: 工具栏:点击工具栏某个功能,开启操作模式. onFrame:鼠标移动预选对象,在能够拾取或者选定 ...
- HeadFirst设计模式笔记:(六)命令模式 —— 封装调用
1.概念 将来自客户端的请求传入一个对象,从而使你可用不同的请求对客户进行参数化.用于“行为请求者”与“行为实现者”解耦,可实现二者之间的松耦合,以便适应变化.分离变化与不变的因素. 在面向对象的程序 ...
- 【Android】策略模式封装百度地图路线规划模块
百度地图的Demo里有个路线规划的功能,但是,这个功能和Activity耦合性太高,所以需要单独抽离出路径规划功能,进行"解耦". 注:由于项目原因,本文只针对驾车路线规划进行封装 ...
- js原生设计模式——9外观模式封装
1.事件处理程序兼容性封装 <!DOCTYPE html><html lang="en"><head> <meta charset= ...
- js原生设计模式——3简单工厂模式\简单工厂模式封装简单对象
1.Factory基本写法 <!DOCTYPE html><html lang="en"><head> <meta charset= ...
- PO页面对象模式封装
PO的主要价值体现在对界面交互细节的封装,这样可以使测试案例可以更关注与业务而非界面细节,提高测试案例的可读性. 以传统的登陆页面为例實現PO模式,因为每个用例中都需要登陆. 其中需要使用Page ...
- Facade 门面模式 封装 MD
门面模式 简介 作用:封装系统功能,简化系统调用 门面模式要求一个系统的外部与其内部的通信必须通过一个统一的门面(Facade)对象进行.门面模式提供一个高层次的接口,使得系统更易于使用. 门面模式的 ...
随机推荐
- python简单脚本-sql字符提取
a="""dr.GetStr("kh"), dr.GetStr("xm"), dr.GetStr("xh"), ...
- FreeMusic项目优化(一)——flex布局学习记录
参考博客:http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html flex布局是w3c于09年提出的,用于简便,整洁,响应式地解决布局问题的手 ...
- jquery mobile 的手指上下滑动文章、导航栏
导航栏的js设置 <!--导航栏的滚动 --> <script type="text/javascript"> var myScroll, pullDown ...
- 解决常见SVN冲突问题(转)
转自:http://www.w3cfuns.com/blog-5443287-5403523.html 一个大项目在开发中可能会拆分成几个小项目,分别分去,同时共通的部分再由人做,做完后再统一合并.同 ...
- explian使用介绍
1).id列数字越大越先执行,如果说数字一样大,那么就从上往下依次执行,id列为null的就表是这是一个结果集,不需要使用它来进行查询. 2).select_type列常见的有:A:simple:表示 ...
- KissXML的XPath选取问题
XMPPFramework用的XML解析库还是大神自己写的KissXML,有些人生下来就是让人仰望的,哎. 进入主题,如下一段XML: <paramsxmlns="namespace& ...
- 树形DP 统计树中长度为K的路径数量——Distance in Tree
一.问题描述 给出一棵n个节点的树,统计树中长度为k的路径的条数(1<=n<=50000 , 1<=k<=500). 二.解题思路 设d[i][k]表示以i为根节点长度为k的路 ...
- 快学UiAutomator创建第一个实例
工具准备 一.准备好java环境(JDK)和安卓环境(SDK.ADT)jdk1.6+ \eclipse\SDK \ADT详情百度,安装java环境 二.打开eclipse 三.创建步骤: 右键新建== ...
- No-12.函数进阶
函数进阶 目标 函数参数和返回值的作用 函数的返回值 进阶 函数的参数 进阶 递归函数 01. 函数参数和返回值的作用 函数根据 有没有参数 以及 有没有返回值,可以 相互组合,一共有 4 种 组合形 ...
- 6.python
最早的'密码本' ascii 涵盖了英文字母大小写,特殊字符,数字.01010101ascii 只能表示256种可能,太少,创办了万国码 unicode 16表示一个字符不行,32位表示一个字符. A ...