ListView灵活的用法
以下是示例的效果图:
WinForm的ListView控件是可以分组显示的,还可排序。
可以把ListView的View属性设置为Details
完整项目请到下面网址查找下载
http://hovertree.com/hovertreescj/
或者:
http://hovertree.com/h/bjaf/scjyuanma.htm
具体实现在项目 HoverTreeWindowsFormsDemo 中,位于HtDemo文件夹下。
以下是代码:
/*
http://hovertree.com/hovertreescj/
本示例展示如何使用ListView分组显示数据。
h : hovertree
*/
using System;
using System.Collections;
using System.Windows.Forms; namespace HoverTreeWindowsFormsDemo.HtFormSet
{
public partial class Form_ListView : Form
{
public Form_ListView()
{
InitializeComponent();
} // Determine whether Windows XP or a later
// operating system is present.
private bool _isRunningXPOrLater =
OSFeature.Feature.IsPresent(OSFeature.Themes); // Declare a Hashtable array in which to store the groups.
private Hashtable[] _groupTables; // Declare a variable to store the current grouping column.
int _groupColumn = ; private void Form_ListView_Load(object sender, EventArgs e)
{ ColumnHeader h_columnHeader0 = new ColumnHeader();
h_columnHeader0.Text = "Title";
// columnHeader0.Width = -1;
h_columnHeader0.Width = ;
ColumnHeader h_columnHeader1 = new ColumnHeader();
h_columnHeader1.Text = "Info";
//columnHeader1.Width = -1;
h_columnHeader1.Width = ; ColumnHeader h_columnHeader2 = new ColumnHeader();
h_columnHeader2.Text = "Year";
// columnHeader2.Width = -1;
h_columnHeader2.Width = ;
// Add the column headers to listView_HoverTree.
listView_HoverTree.Columns.AddRange(new ColumnHeader[]
{h_columnHeader0, h_columnHeader1, h_columnHeader2}); // Add a handler for the ColumnClick event.
listView_HoverTree.ColumnClick +=
new ColumnClickEventHandler(listView_HoverTree_ColumnClick); // Create items and add them to listView_HoverTree.
ListViewItem item0 = new ListViewItem(new string[]
{"HoverTreeSCJ",
"Hewenqi",
""});
ListViewItem item1 = new ListViewItem(new string[]
{"Keleyi: jQuery and HTML5",
"柯乐义",
""});
ListViewItem item2 = new ListViewItem(new string[]
{"hwq2.com",
"A Good Site",
""});
ListViewItem item3 = new ListViewItem(new string[]
{"何问起收藏夹",
"HT",
""});
ListViewItem item4 = new ListViewItem(new string[]
{"HoverClock",
"HTML5 Clock",
""});
ListViewItem item5 = new ListViewItem(new string[]
{"EasySector",
"HTML5 canvas",
""});
listView_HoverTree.Items.AddRange(
new ListViewItem[] { item0, item1, item2, item3, item4, item5 }); if (_isRunningXPOrLater)
{
// Create the groupsTable array and populate it with one
// hash table for each column.
_groupTables = new Hashtable[listView_HoverTree.Columns.Count];
for (int column = ; column < listView_HoverTree.Columns.Count; column++)
{
// Create a hash table containing all the groups
// needed for a single column.
_groupTables[column] = CreateGroupsTable(column);
//groupTables[column]
} // Start with the groups created for the Title column.
SetGroups();
} // Initialize the form.
this.Controls.Add(listView_HoverTree);
this.Size = new System.Drawing.Size(, );
this.Text = "ListView Groups Example_何问起";
} // Groups the items using the groups created for the clicked
// column.
private void listView_HoverTree_ColumnClick(
object sender, ColumnClickEventArgs e)
{
// Set the sort order to ascending when changing
// column groups; otherwise, reverse the sort order.
if (listView_HoverTree.Sorting == SortOrder.Descending ||
(_isRunningXPOrLater && (e.Column != _groupColumn)))
{
listView_HoverTree.Sorting = SortOrder.Ascending;
}
else
{
listView_HoverTree.Sorting = SortOrder.Descending;
}
_groupColumn = e.Column; // Set the groups to those created for the clicked column.
if (_isRunningXPOrLater)
{
SetGroups(e.Column);
}
} // Sets listView_HoverTree to the groups created for the specified column.
private void SetGroups(int column)
{
// Remove the current groups.
listView_HoverTree.Groups.Clear(); // Retrieve the hash table corresponding to the column.
Hashtable groups = (Hashtable)_groupTables[column]; // Copy the groups for the column to an array.
ListViewGroup[] h_groupsArray = new ListViewGroup[groups.Count];
groups.Values.CopyTo(h_groupsArray, ); // Sort the groups and add them to listView_HoverTree.
Array.Sort(h_groupsArray, new ListViewGroupSorter(listView_HoverTree.Sorting));
listView_HoverTree.Groups.AddRange(h_groupsArray); // Iterate through the items in listView_HoverTree, assigning each
// one to the appropriate group.
foreach (ListViewItem item in listView_HoverTree.Items)
{
// Retrieve the subitem text corresponding to the column.
string h_subItemText = item.SubItems[column].Text; // For the Title column, use only the first letter.
if (column == )
{
h_subItemText = h_subItemText.Substring(, );
} // Assign the item to the matching group.
item.Group = (ListViewGroup)groups[h_subItemText];
}
} // Creates a Hashtable object with one entry for each unique
// subitem value (or initial letter for the parent item)
// in the specified column.
private Hashtable CreateGroupsTable(int column)
{
// Create a Hashtable object.
Hashtable h_groups = new Hashtable(); // Iterate through the items in listView_HoverTree.
foreach (ListViewItem item in listView_HoverTree.Items)
{
// Retrieve the text value for the column.
string h_subItemText = item.SubItems[column].Text; // Use the initial letter instead if it is the first column.
if (column == )
{
h_subItemText = h_subItemText.Substring(, );
} // If the groups table does not already contain a group
// for the subItemText value, add a new group using the
// subItemText value for the group header and Hashtable key.
if (!h_groups.Contains(h_subItemText))
{
h_groups.Add(h_subItemText, new ListViewGroup(h_subItemText,
HorizontalAlignment.Left));
}
} // Return the Hashtable object.
return h_groups;
} // Sorts ListViewGroup objects by header value.
private class ListViewGroupSorter : IComparer
{
private SortOrder h_order; // Stores the sort order.
public ListViewGroupSorter(SortOrder theOrder)
{
h_order = theOrder;
} // Compares the groups by header value, using the saved sort
// order to return the correct value.
public int Compare(object x, object y)
{
int result = String.Compare(
((ListViewGroup)x).Header,
((ListViewGroup)y).Header
);
if (h_order == SortOrder.Ascending)
{
return result;
}
else
{
return -result;
}
}
} }
}
转自:http://hovertree.com/h/bjaf/jynj6isd.htm
推荐:http://www.cnblogs.com/roucheng/p/csgeshi.html
ListView灵活的用法的更多相关文章
- Android listview与adapter用法
listview与adapter用法 博客分类: android 一个ListView通常有两个职责. (1)将数据填充到布局. (2)处理用户的选择点击等操作. 第一点很好理解,ListView ...
- Android listview与adapter用法(BaseAdapter + getView)
Android listview与adapter用法http://www.cnblogs.com/zhengbeibei/archive/2013/05/14/3078805.html package ...
- listview与adapter用法
Android listview与adapter用法 listview与adapter用法 博客分类: android 一个ListView通常有两个职责. (1)将数据填充到布局. (2)处理用 ...
- Android—— ListView 的简单用法及定制ListView界面
一.ListView的简单用法 2. 训练目标 1) 掌握 ListView 控件的使用 2) 掌握 Adapter 桥梁的作用 实现步骤: 1)首先新建一个项目, 并让ADT 自动帮我们创建好活动. ...
- ListView 和 Adapter用法
一个ListView通常有两个职责. (1)将数据填充到布局. (2)处理用户的选择点击等操作. 第一点很好理解,ListView就是实现这个功能的.第二点也不难做到,在后面的学习中读者会发现,这非常 ...
- 对于ListView的一些用法(一)
ScrollView:只能用于控件比较少的界面,如果数据有上千上万条,那么使用ScrollView就不好了,因为ScrollView就把所有的控件进行初始化,这是非常消耗性能的操作,所以android ...
- 【转】Android listview与adapter用法
一个ListView通常有两个职责. (1)将数据填充到布局. (2)处理用户的选择点击等操作. 第一点很好理解,ListView就是实现这个功能的.第二点也不难做到,在后面的学习中读者会发现,这非常 ...
- Django的ListView超详细用法(含分页paginate功能)
开发环境: python 3.6 django 1.11 场景一 经常有从数据库中获取一批数据,然后在前端以列表的形式展现,比如:获取到所有的用户,然后在用户列表页面展示. 解决方案 常规写法是,我们 ...
- Android之ListView——ArrayAdapter的用法学习
当我们使用ListView时,必不可少的便会使用到adapter,adapter的用处就像是一个水管接口,把你想展现的数据与你希望展现的布局样式通过某种协定结合起来. ArrayAdapter针对每个 ...
随机推荐
- Nodejs之MEAN栈开发(二)----视图与模型
上一节做了对Express做了简单的介绍,提出了controller,介绍了路由.这一节将重点放到视图和模型上,完成几个静态页面并部署到heroku上. 导航 前端布局使用bootstrap,从官网下 ...
- JavaEE连接数据库练习
登录端 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncod ...
- java,H5微信蓝牙设备开发教程申请设备和添加设备(2)
转载地址 http://www.vxzsk.com/76.html 申请设备功能 a. 登录公众平台,点击左边功能栏的"添加功能插件",选择"设备功能". b ...
- setValue:forUndefinedKey this class is not key value coding-compliant for the key
下午开发过程中遇到一个错误,结果被的真惨,从上午 11 点查错一直查到下午 2 点才找到错误的原因,真的郁闷的不行. 关于查错这么久,主要的原因是: 1. 自己对 IOS 开发还不熟悉2. 不知道 ...
- 让hammer完美支持Pixi.js - 2D webG库
由于项目改造,采用2D webG的pixi库,那么基于canvas的结构上,事件就是最大的一个问题了 改造的原理很简单,把hammer里面的addEventListeners事件绑定给第三方库代替,事 ...
- OpenCASCADE6.8.0 Reference Manual Serach Problem
OpenCASCADE6.8.0 Reference Manual Serach Problem eryar@163.com 1. Problem 有网友反映OpenCASCADE6.8.0的Refe ...
- WPF DatePicker只显示年和月 修改:可以只显示年
最近的项目,查询时只需要年和月,不需要日,因此需要对原有的DatePicker进行修改,查询了网上的内容,最终从一篇帖子里看到了添加附加属性的方法,地址是http://stackoverflow.co ...
- python统计某一个进程名所占用的内存
设计思路: 通过python,执行cmd中tasklist命令,获取要统计的进程的相关信息:通过正则表达式,查找出进程名称.进程pid.内存使用,然后打印出来. 作为pythoner,有时候需要统计p ...
- python调取C/C++的dll生成方法
本文针对Windows平台下,python调取C/C++的dll文件. 1.如果使用C语言,代码如下,文件名为test.c. __declspec(dllexport) int sum(int a,i ...
- 深入理解this机制系列第一篇——this的4种绑定规则
× 目录 [1]默认绑定 [2]隐式绑定 [3]隐式丢失[4]显式绑定[5]new绑定[6]严格模式 前面的话 如果要问javascript中哪两个知识点容易混淆,作用域查询和this机制绝对名列前茅 ...