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)对象进行.门面模式提供一个高层次的接口,使得系统更易于使用. 门面模式的 ...
随机推荐
- 《超实用的HTML代码段》阅读笔记1——HTML5自动聚焦
在页面加载完成后自动将输入焦点定位到需要的元素,用户就可以直接在改元素中进行输入而不需要手动选择它. 通过autofocus的属性就可以指定这种自动聚焦的功能,示例代码如下: <form nam ...
- LightOJ 1422 Halloween Costumes (区间DP,经典)
题意: 有个人要去参加万圣节趴,但是每到一个趴都要换上特定的服装,给定一个序列表示此人要穿的衣服编号(有先后顺序的),他可以套很多件衣服在身上,但此人不喜欢再穿那些脱下的衣服(即脱下后就必须换新的), ...
- 4个Linux服务器监控工具
下面是我想呈现给你的4个强大的监控工具. htop – 交互式进程查看器 你可能知道在机器上查看实时进程的标准工具top.如果不知道,请运行$ top看看,运行$ man top阅读帮助手册. hto ...
- IOS Array 排序方法
NSArray *sortedArray = [array sortedArrayUsingComparator: ^(id obj1, id obj2) { if ([obj1 integerVal ...
- Scroller 界面滑动
Scroller 滑动之后滑动器子标签里面的控件,其自身是不动的.这里点在下面的进阶会体现出来. 1. 继承LinearLayout, 相对布局也可以. public class MyLinearL ...
- LeetCode || 大杂烩w
454. 4Sum II 题意:给四个数组,每个数组内取一个数使得四个数和为0,问有多少种取法 思路:枚举为On4,考虑两个数组,On2枚举所有可能的和,将和的出现次数存入map中,On2枚举另两个数 ...
- Electric Motor Manufacturer - Motor Protection: 5 Questions, 5 Answers
I. Selection principle of motor protectorThe Electric Motor Manufacturer stated that the reasonab ...
- GloVe:另一种Word Embedding方法
若想深层地理解GloVe和本文,最好了解SVD, word2vec(skip-gram为主)的相关知识.若仅寻求一种新的word embedding方法,可以不必了解以上前置知识. 一言以蔽之,Glo ...
- 冒泡法排序参考(Java)
package com.swift; public class Maopao { //冒泡法 public static void main(String[] args) { int[] arr= { ...
- Java 的访问权限
public>protected>默认(包访问权限)>private,因为protected除了可以被同一包访问,还可以被包外的子类所访问