⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 listviewhelper.cs

📁 汽车销售公司ERP进销存系统 汽车销售公司ERP进销存系统
💻 CS
字号:
using System;
using System.Collections;
using System.Reflection;
using System.Windows.Forms;

namespace CallCenter.BusinessInterfaces.BaseForms {
	/// <summary>
	/// 继承ListView的自制控件 
	/// 功能1:动态将列名添加到列头上
	/// 功能2:动态将数据放到listview里
	/// author:yanqingxi
	/// </summary>
	public class ListViewHelper:System.Windows.Forms.ListView {
		public ListViewHelper() {
			//
			// TODO: 在此处添加构造函数逻辑
			//
			this.View = View.Details;
			this.GridLines = true;
			this.FullRowSelect = true;
			this.HideSelection = false;
			this.BackColor = System.Drawing.Color.Lavender;
			
			//this.Dock=DockStyle.Fill;
		}

		/// <summary>
		/// 初始化列名
		/// </summary>
		/// <param name="o">model类型</param>
		public void InitializeListViewCol(object o){
			string tempStr=null;
			System.Reflection.PropertyInfo[] p;
			p = o.GetType().GetProperties();
			for(int i=0;i<p.Length;i++){
				tempStr = p[i].GetValue(o,null).ToString();
				this.Columns.Add(tempStr,tempStr.Length*18, HorizontalAlignment.Left);
			}
		}
		/// <summary>
		/// 自定义初始化列表,默认添加第一列
		/// </summary>
		/// <param name="o">model类型</param>
		/// <param name="Display">要显示的列,在model类型里属性的索引值</param>
		public void InitLV_UserDefined(object o,int[] Display){
			string tempStr=null;
			int displayIndex =1;
			System.Reflection.PropertyInfo[] p;
			p = o.GetType().GetProperties();
			for(int i=0;i<Display.Length;i++){
				displayIndex = Display[i];
				if(displayIndex>-1 && displayIndex<p.Length){
					tempStr = p[displayIndex].GetValue(o,null).ToString();
					this.Columns.Add(tempStr,tempStr.Length*18, HorizontalAlignment.Left);
				}
			}
		}

		/// <summary>
		/// 刷新列表(将model里的所有属性都显示出来)
		/// </summary>
		/// <param name="list">list列表</param>
		/// <param name="o">list列表里装的类型</param>
		public void Refresh(IList list,object o){
			System.Reflection.PropertyInfo[] p;
			try{
				this.Items.Clear();
				if(o!=null){
					for(int i=0;i<list.Count;i++){
						//通过映射得到属性
						o=(object)list[i];
						p = o.GetType().GetProperties();
						ListViewItem item=null;
						//将属性的值导出
						for(int j=0;j<p.Length;j++){
							if(j==0){
								item = new ListViewItem(p[j].GetValue(o,null).ToString());
							}
							else{
								item.SubItems.Add(p[j].GetValue(o,null).ToString());
							}
						}
						if(item!=null){
							this.Items.Add(item);
						}
					}
					this.Items[0].Selected = true;
				}
			}catch(Exception ex){
				MessageBox.Show(ex.Message);
			}
		}

		/// <summary>
		/// 刷新列表(将model里的属性个别都显示出来)
		/// </summary>
		/// <param name="list">传进来的数据列表</param>
		/// <param name="o">model的类型</param>
		/// <param name="NDisplayCol">不显示列的数据,装的是model属性的序号</param>
		public void Refresh(IList list,object o,int[] NDisplayCol){
			System.Reflection.PropertyInfo[] p;
			try{
				this.Items.Clear();
				if(o!=null){
					for(int i=0;i<list.Count;i++){
						//通过映射得到属性
						ListViewItem item=null;
						o=(object)list[i];
						p = o.GetType().GetProperties();
						//将属性的值导出
						for(int j=0;j<p.Length;j++){
							if(j==0){
								item = new  ListViewItem(p[0].GetValue(o,null).ToString());
							}
							else{
								//将不显示的列刨除
								if(haveItem(j,NDisplayCol)){
									continue;
								}
								item.SubItems.Add(p[j].GetValue(o,null).ToString());
							}
						}
						if(item!=null){
							this.Items.Add(item);
						}
					}
					this.Items[0].Selected = true;
				}
			}catch(Exception ex){
				MessageBox.Show(ex.Message);
			}
		}

		/// <summary>
		/// 自定义显示列
		/// </summary>
		/// <param name="list">要显示的列表</param>
		/// <param name="o">model类型</param>
		/// <param name="DisplayCol">要显示的列,model类属性的索引值</param>
		public void Refresh_UserDefine(IList list,object o,int[] DisplayCol){
			int displayIndex=-1;
			System.Reflection.PropertyInfo[] p;
			try{
				this.Items.Clear();
				if(o!=null && list!=null){
					for(int i=0;i<list.Count;i++){
						o=(object)list[i];
						//通过映射得到属性
						p = o.GetType().GetProperties();
						ListViewItem item=null;
						//将属性的值导出
						for(int j=0;j<DisplayCol.Length;j++){
							displayIndex = DisplayCol[j];
							if(displayIndex>-1 && displayIndex<p.Length){
								if(j==0){
									item = new ListViewItem(p[displayIndex].GetValue(o,null).ToString());
								}
								else{
									item.SubItems.Add(p[displayIndex].GetValue(o,null).ToString());
								}
							}
						}
						if(item!=null){
							this.Items.Add(item);
						}
					}
					if(this.Items.Count>0){
						this.Items[0].Selected = true;
					}
				}
			}catch(Exception ex){
				MessageBox.Show(ex.Message);
			}
		}
		/// <summary>
		/// 自定义显示列,通过属性名称选择添加数据
		/// </summary>
		/// <param name="list">要显示的列表</param>
		/// <param name="o">model类型</param>
		/// <param name="DisplayCol">属性名称</param>
		public void Refresh_UserDefine(IList list,object o,string[] DisplayCol){
			string displayProp=string.Empty;
			try{
				this.Items.Clear();
				if(o!=null && list!=null){
					for(int i=0;i<list.Count;i++){
						o=(object)list[i];
						ListViewItem item=null;
						//将属性的值导出
						for(int j=0;j<DisplayCol.Length;j++){
							displayProp = DisplayCol[j];
							System.Reflection.PropertyInfo p = o.GetType().GetProperty(displayProp);
							if(p!=null){
								if(j==0){
									item = new ListViewItem(p.GetValue(o,null).ToString());
								}
								else{
									item.SubItems.Add(p.GetValue(o,null).ToString());
								}
							}
						}
						if(item!=null){
							this.Items.Add(item);
						}
					}
					if(this.Items.Count>0){
						this.Items[0].Selected = true;
					}
				}
			}catch(Exception ex){
				MessageBox.Show(ex.Message);
			}
		}


		private bool haveItem(int currentItem,int[] NDisplayCol){
			
			for(int i=0;i<NDisplayCol.Length;i++){
				if(currentItem==NDisplayCol[i]){
					return true;
				}
			}
			return false;
		}
		//删除单行上的刷新
		public void DeleteRefresh(IList list,object o,int beginIndex){

			Refresh(list,o);
			setCursorLocation(beginIndex);
		}

		//设置光标选中位置:外边掉的方法
		public void setCursorLocation(int beginIndex){
			int nowListViewCount;
			int nowIndex=0;
			nowListViewCount = this.Items.Count;
			if(beginIndex>-1 && nowListViewCount>0){
				nowIndex = this.GetCursorLocation(beginIndex,nowListViewCount);
				if(nowIndex>-1){
					this.Items[nowIndex].Selected = true;
				}
			}
		}
		public int GetCursorLocation(int beginIndex,int nowListViewCount){
			int nowIndex=0;
		
			if(nowListViewCount>0){
				if(beginIndex<nowListViewCount){
					//删除的是最后一行
					nowIndex=beginIndex;
				}
				if(beginIndex==nowListViewCount){
					//最后行以上任意行
					nowIndex=beginIndex-1;
				}
				return nowIndex;
			}
			return -1;
			
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -