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针对每个 ...
随机推荐
- Hadoop学习笔记—13.分布式集群中节点的动态添加与下架
开篇:在本笔记系列的第一篇中,我们介绍了如何搭建伪分布与分布模式的Hadoop集群.现在,我们来了解一下在一个Hadoop分布式集群中,如何动态(不关机且正在运行的情况下)地添加一个Hadoop节点与 ...
- TODO:Ubuntu下安装Node
TODO:Ubuntu下安装Node Node.js 是一个基于 Chrome V8 引擎的 JavaScript 运行环境.Node.js 使用了一个事件驱动.非阻塞式 I/O 的模型,使其轻量又高 ...
- 《Entity Framework 6 Recipes》中文翻译系列 (46) ------ 第八章 POCO之领域对象测试和仓储测试
翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 8-8 测试领域对象 问题 你想为领域对象创建单元测试. 这主要用于,测试特定的数 ...
- css水平居中那点事
昨晚深夜写了css垂直居中那点事,今晚该写他的兄弟篇:css水平居中那点事了..…^^ 其实本来这两个可以连在一起写,可是为了不要搞混,为了让思路更清晰,最后决定还是分开来些比较好...这样以后也有利 ...
- Redis初级介绍
1 什么是Redis Redis(REmote DIctionary Server,远程数据字典服务器)是开源的内存数据库,常用作缓存或者消息队列. Redis的特点: Redis存在于内存,使用硬盘 ...
- JS函数 -- 功能,语法,返回值,匿名函数,自调用匿名函数,全局变量与局部变量,arguments的使用
“JavaScript设计得最出色的就是它的函数的实现.” -- <JavaScript语言精粹> 函数包含一组语句,它们是JS的基础模块单元,用于指定对象的行为.一般来说,所谓编程,就是 ...
- 回发或回调参数无效。在配置中使用 pages enableEventValidation=true 或在页面中使用 %@ Page EnableEventValidation=true % 启用了事件验证
WebForm中回发或回调参数无效问题的解决 解决 .NET中回发或回调参数无效问题的解 该错误的详细提示信息为: 回发或回调参数无效.在配置中使用 <pages enableEventVali ...
- OpenCASCADE Curve Length Calculation
OpenCASCADE Curve Length Calculation eryar@163.com Abstract. The natural parametric equations of a c ...
- 在usercontrol里实现导航
(Application.Current.RootVisual as PhoneApplicationFrame).Navigate(new Uri("/NewContent.xaml&qu ...
- MVC5 网站开发实践 2.1、管理员登陆
目录 MVC5 网站开发实践 概述 MVC5 网站开发实践 1.建立项目 MVC5 网站开发实践 2.后台管理 1. 创建SHA256加密方法. 在Data项目中添加文件夹[Security ...