ListView灵活的用法

以下是示例的效果图:WinForm的ListView控件是可以分组显示的,还可排序。可以把ListView的View属性设置为Details完整项目请到下面网址查找下载http://hovertre

大家好,又见面了,我是全栈君,今天给大家准备了Idea注册码。

以下是示例的效果图:
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 = 0;

        private void Form_ListView_Load(object sender, EventArgs e)
        {

            ColumnHeader h_columnHeader0 = new ColumnHeader();
            h_columnHeader0.Text = "Title";
            // columnHeader0.Width = -1;
            h_columnHeader0.Width = 200;
            ColumnHeader h_columnHeader1 = new ColumnHeader();
            h_columnHeader1.Text = "Info";
            //columnHeader1.Width = -1;
            h_columnHeader1.Width = 150;

            ColumnHeader h_columnHeader2 = new ColumnHeader();
            h_columnHeader2.Text = "Year";
            // columnHeader2.Width = -1;
            h_columnHeader2.Width = 100;
            // 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",
            "2016"});
            ListViewItem item1 = new ListViewItem(new string[]
                {"Keleyi: jQuery and HTML5",
            "柯乐义",
            "2012"});
            ListViewItem item2 = new ListViewItem(new string[]
                {"hwq2.com",
            "A Good Site",
            "2015"});
            ListViewItem item3 = new ListViewItem(new string[]
                {"何问起收藏夹",
            "HT",
            "2012"});
            ListViewItem item4 = new ListViewItem(new string[]
                {"HoverClock",
            "HTML5 Clock",
            "2016"});
            ListViewItem item5 = new ListViewItem(new string[]
                {"EasySector",
            "HTML5 canvas",
            "2016"});
            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 = 0; 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(0);
            }

            // Initialize the form.
            this.Controls.Add(listView_HoverTree);
            this.Size = new System.Drawing.Size(550, 330);
            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, 0);

            // 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 == 0)
                {
                    h_subItemText = h_subItemText.Substring(0, 1);
                }

                // 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 == 0)
                {
                    h_subItemText = h_subItemText.Substring(0, 1);
                }

                // 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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/120305.html原文链接:https://javaforall.net

(0)
上一篇 2021年12月27日 下午7:00
下一篇 2021年12月27日 下午7:00


相关推荐

  • iOS开发-用户定位获取-CoreLocation的实际应用-CLLocationManger获取定位权限-CLLocation详细使用方式

    iOS开发-用户定位获取-CoreLocation的实际应用-CLLocationManger获取定位权限-CLLocation详细使用方式iOS提供了两个框架用来定位以及地图显示。CoreLocation框架包含的类可以帮助设备确定位置和航向以及使用基于位置的有效信息。MapKit框架未定位提供了户用页面的支持(地图显示),里面包含了地图视图、卫星地图视图以及2D、3D混合视图,并且能够让开发人员管理地图标注和地图覆盖层,前者用于标注地点(常见的地图大头针),后者用来突出某区域或者路线等。本期内容:CLLocationMan…

    2022年7月26日
    6
  • 分布式id解决方案

    分布式id解决方案文章目录 1 分布式 id 实现方案 1 1 uuid1 2 数据库主键自增 1 3Redis 自增 1 4 号段模式 1 5 雪花算法 snowflake 1 5 1 百度 uid generator 1 5 2 美团 Leaf 所谓 id 就是能够用作唯一标识的记号 在我们日常的设计中 对于单体架构 我们一般使用数据库的自增 Id 来作为表的主键 但是对于一个分布式系统 就会出现 ID 冲突 所以对于分布式 ID 而言 也需要具备分布式系统的特点 高并发 高可用 高性能等特点 1 分布式 id 实现方案我们先看看常见的分布 id 解

    2026年3月19日
    2
  • Vuex入门(3)—— getters,mapGetters,…mapGetters详解[通俗易懂]

    Vuex入门(3)—— getters,mapGetters,…mapGetters详解[通俗易懂]Vuex提供了state这样的状态统一管理树,你可以在vue中用computed计算属性接收这些公共状态,以便使用,当然你也可以在接收原值的基础上对这个值做出一些改造,如computed:{sex:function(){returnthis.$store.state.sex+’加个字符串,算是改造’}}但是如果你的其他组件也要使用这…

    2022年4月27日
    65
  • 【Android小应用】颈椎保健操Android开源项目

    【Android小应用】颈椎保健操Android开源项目

    2021年12月14日
    48
  • Db4o数据库:细说查询[通俗易懂]

    Db4o数据库:细说查询[通俗易懂]通过第一篇的介绍,相信大家也对Db4o有一定的了解,接下来就详细说一下有关查询的话题。Db4o原生支持3中查询模式:Query-By-Example:简称QBE,根据模板类进行匹配查询,这是最简单的一种模式NativeQuery:简称NQ,Db4o推荐的查询模式TheSODAAPI:这是Db4o底层查询API,官网文档解释,此API提供向后的兼容性,适用于动态

    2022年7月21日
    15
  • NET命令的基本用法[通俗易懂]

    NET命令的基本用法[通俗易懂] 本文文章综合了WINDOWS98,WINDOWSWORKSTATION和WINDOWSSERVER这三个操作系统关于NET命令的解释,相信对大家会有所帮助。(1)NET命令是一个命令行命令。(2)管理网络环境、服务、用户、登陆……等本地信息(3)WIN98,WINWORKSTATION和WINNT都内置了NET命令。(4)但WIN98的NET命令和WORKS

    2022年5月28日
    42

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号