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针对每个 ...
随机推荐
- ASP.NET MVC 从零开始 - Web.config
这篇文章是从我的 github 博客 http://lxconan.github.io 导入的. 在上一篇中,我们从零开始创建了一个非常简单的 ASP.NET MVC 应用程序.接下来,你是不是期望我 ...
- Mongoose Schemas定义中timestamps选项的妙用
在Node.js中使用MongoDB少不了Mongoose. 假设有如下Mongoose Schemas的定义: var ItemSchema = new mongoose.Schema({ biz: ...
- Android开发学习之路-回调机制学习笔记
不知道是我学Java的时候没有认真听还是怎么的,曾经一直不知道什么是“回调”,它有什么用,百度一大堆,都太复杂看不明白(好吧是我笨),所以想把自己理解的分享给其他看到的人,大家都真正认识一下这个重要的 ...
- 微信蓝牙设备开发教程之获取设备deviceid和二维码(3)
文章转载地址 http://www.vxzsk.com/87.html 设备授权 调用 设备授权新接口 ,获取deviceid和二维码,然后利用获取到的deviceid更新设备属性(如mac地址, ...
- 传智播客--WPF基础视频学习--sender解释(小白内容)
sender是激发该事件的对象,如果用在Button的双击点击事件上的话,就是只当前点击的对象 用例子来说明一下,有两个Button控件,分别为1和2,同时绑定一个Button_Click事件 pri ...
- PHP浅复制与深复制
原文链接:http://www.orlion.ga/731/ php用clone复制对象有一个问题,下面用代码来说明问题: class Foo{ public $bar; public $name; ...
- 不使用session,借助redis实现验证码
1.首先看一下基本的流程 2.看一下代码 注:其中用到的一些工具类,可以到我的github上去下载 https://github.com/hjzgg/usually_util/tree/master ...
- Util应用程序框架公共操作类(三):数据类型转换公共操作类(扩展篇)
上一篇以TDD方式介绍了数据类型转换公共操作类的开发,并提供了单元测试和实现代码,本文将演示通过扩展方法来增强公共操作类,以便调用时更加简化. 下面以字符串转换为List<Guid>为例进 ...
- Objective-C中的属性机制
Objective-C 2.0中的属性机制为我们提供了便捷的获取和设置实例变量的方式,也可以说属性为我们提供了一个默认的设置器和访问器的实现.在学习OC中属性之前我们先要知道为什么要为变量实现gett ...
- MySQL Range Optimization
8.2.1.3 Range Optimization MYSQL的Range Optimization的目的还是尽可能的使用索引 The range access method uses a sing ...