以下内容均来自网上,个人收集整理,具体出处也难确认了,就没写出处了;

一、基本使用:

listView.View = View.Details;//设置视图
listView.SmallImageList = imageList;//设置图标 //添加列
listView.Columns.Add("本地路径", 150, HorizontalAlignment.Left);
listView.Columns.Add("远程路径", 150, HorizontalAlignment.Left);
listView.Columns.Add("上传状态", 80, HorizontalAlignment.Left);
listView.Columns.Add("耗时", 80, HorizontalAlignment.Left); //添加行
var item = new ListViewItem();
item.ImageIndex = 1;
item.Text = name; //本地路径
item.SubItems.Add(path); //远程路径
item.SubItems.Add("ok"); //执行状态
item.SubItems.Add("0.5"); //耗时统计 listView.BeginUpdate();
listView.Items.Add(item);
listView.Items[listView.Items.Count - 1].EnsureVisible();//滚动到最后
listView.EndUpdate();

二、动态添加记录,ListView不闪烁:

1.新建一个C# 类,命名为ListViewNF(NF=Never/No Flickering)

2.复制如下代码

class ListViewNF : System.Windows.Forms.ListView
{
public ListViewNF()
{
// Activate double buffering
this.SetStyle(ControlStyles.OptimizedDoubleBuffer | ControlStyles.AllPaintingInWmPaint, true); // Enable the OnNotifyMessage event so we get a chance to filter out
// Windows messages before they get to the form's WndProc
this.SetStyle(ControlStyles.EnableNotifyMessage, true);
} protected override void OnNotifyMessage(Message m)
{
//Filter out the WM_ERASEBKGND message
if (m.Msg != 0x14)
{
base.OnNotifyMessage(m);
}
}
}

3.修改你的WinForm对应的xxxx.Design.cs,将系统默认生成的System.Windows.Forms.ListView改为ListViewNF即可。

三、动态添加记录,跳转到最后行:

实现代码:

ListViewItem Item = new ListViewItem();
Item.SubItems.Clear();
.....相关其他代码
this.listView1.Items.Add(Item);
Item.EnsureVisible(); //关键的实现函数

四、点击表头实现排序:

1.增加自定义排序类:

using System;
using System.Collections;
using System.Windows.Forms; namespace Whir.Software.Framework.UI
{
public class ListViewSort : IComparer
{
private readonly int _col;
private readonly bool _descK; public ListViewSort()
{
_col = 0;
} public ListViewSort(int column, object desc)
{
_descK = (bool)desc;
_col = column; //当前列,0,1,2...,参数由ListView控件的ColumnClick事件传递
} public int Compare(object x, object y)
{
int tempInt = String.CompareOrdinal(((ListViewItem)x).SubItems[_col].Text,
((ListViewItem)y).SubItems[_col].Text);
if (_descK)
{
return -tempInt;
}
return tempInt;
}
}
}

2.给ListView增加点击表头事件:

private void listView_ColumnClick(object sender, ColumnClickEventArgs e)
{
if (listView.Columns[e.Column].Tag == null)
{
listView.Columns[e.Column].Tag = true;
}
var tabK = (bool)listView.Columns[e.Column].Tag;
listView.Columns[e.Column].Tag = !tabK;
listView.ListViewItemSorter = new ListViewSort(e.Column, listView.Columns[e.Column].Tag);
//指定排序器并传送列索引与升序降序关键字
listView.Sort();//对列表进行自定义排序
}
作者:a497785609 发表于2014-6-3 20:32:53 原文链接
阅读:12 评论:0 查看评论

[转]C# Winform ListView使用的更多相关文章

  1. winform ListView应用之分组、重绘图标、网格线 (c# .net winform)

    最近在winform应用中需要用到可分组的数据列表功能,DataGridView默认没有提供分组的功能,而OutlookGrid(http://www.codeproject.com/KB/grid/ ...

  2. C# Winform ListView使用

    以下内容均来自网上,个人收集整理,具体出处也难确认了,就没写出处了: 一.基本使用: listView.View = View.Details;//设置视图 listView.SmallImageLi ...

  3. 陈年佳酿之 - Winform ListView 控件 double click 事件中获取选中的row与column

    背景 最近收到了一个关于以前项目的维护请求,那时的楼主还是刚刚工作的小青年~~~ 项目之前使用的是.net/winform.今天重新打开代码,看着之前在FrameWork2.0下面的代码, 满满的回忆 ...

  4. winform listview用法

    资源收集 C#winform中ListView的使用 C# WinForm开发系列 - ListBox/ListView/Panel(介绍了一些listview的高级用法) 直接上代码 示例一: th ...

  5. WinForm ListView不分页加载大量数据

    WinForm的ListView在加载大量数据时会出现闪烁的问题,同时数据加载很慢.如果你的列表中有超过千条的数据且不做特殊处理还是用普通的ListView.Items.Add(),估计你的用户得抱怨 ...

  6. Winform listview控件、 容器控件

    1.常用的基本属性: (1)FullRowSelect:设置是否行选择模式.(默认为false) 提示:只有在Details视图该属性才有意义. (2) GridLines:设置行和列之间是否显示网格 ...

  7. winform listview控件、容器控件

    ListVies控件主要用于展示数据 常用属性: FullRowSelect:设置是否行选择模式.(默认为false) (开启之后一下选中一行数据) GridLines:设置行和列之间是否显示网格线. ...

  8. winform listview控件

    ListView控件 1.常用的基本属性: (1)FullRowSelect:设置是否行选择模式.(默认为false) 提示:只有在Details视图该属性才有意义. (2) GridLines:设置 ...

  9. Winform ListView 元素拖动

    //ListView 属性 /* AllowDrop : True */ ListView objLVDrag; private void listView_DragDrop(object sende ...

随机推荐

  1. eclipse 左边目录结构下五referenced library解决办法

    没有referenced library,加入多个jar包,导致项目看上去庞大 解决办法: 在Package Explorer窗口中会出现Referenced Libraries,但Java EE 透 ...

  2. Dedecms v5.7 最新注入分析

    该漏洞是cyg07在乌云提交的, 漏洞文件: plus\feedback.php.存在问题的代码: view source 01 ... 02 if($comtype == 'comments') 0 ...

  3. JavaScriptSerializer中日期序列化问题解决方案

    JavaScriptSerializer中日期序列化问题解决方案 直接进入主题: class Student { public int age { get; set; } public DateTim ...

  4. How can I style a JavaFX SplitMenuButton in CSS

    0 down vote favorite I try to style a SplitMenuButton in JavaFX. I've got a menuButton and a SplitMe ...

  5. Json.NET 利用ContractResolver解决命名不一致问题

    今天在遇到这么个问题,项目上有一部分功能需要访问web api, 这个api请求和相应的数据格式都是使用JSON,JSON中的field命名方式是以下划线分割的,比如"project_nam ...

  6. Java并发编程笔记—基础知识—实用案例

    如何正确停止一个线程 1)共享变量的使用 中断线程最好的,最受推荐的方式是,使用共享变量(shared variable)发出信号,告诉线程必须停止正在运行的任务.线程必须周期性的核查这一变量(尤其在 ...

  7. HNU 12827 NASSA’s Robot

    题目链接:http://acm.hnu.cn/online/?action=problem&type=show&id=12827&courseid=268 #include&l ...

  8. SGU 179 Brackets light(生成字典序的下一个序列)

    题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=179 解题报告:输入一个合法的括号串,求出这个括号串的字典序的下一个串.(认为'(' ...

  9. each-Select

    While Ruby’s each method is useful, it also comes with an awesome extended family of methods that ar ...

  10. python学习之最简单的获取本机ip信息的小程序

    文章是从我的个人博客粘贴过来的,大家可以直接访问我的个人博客哦 http://www.iwangzheng.com 获取本机ip信息的命令ifconfig总是在用,这次拿到pyhton代码里,感觉py ...