分享

C# ListView Group用法

 梨园暴风 2010-09-30
C# ListView Group用法

2009-11-11 11:06//部分代码来自msdn,转载请注明出处

//列表分组

        ListView lvGroup = new ListView();
        private bool isRunningXPOrLater = OSFeature.Feature.IsPresent(OSFeature.Themes);
        int colGroupIndex = 0;
        Hashtable[] lvGroupTab;
        void InitListView(DataTable dt)
        {
            lvGroup.View = View.Details;
            lvGroup.Sorting = SortOrder.Ascending;
            lvGroup.FullRowSelect = true;
            //lvGroup.GridLines = true;
            //lvGroup.CheckBoxes = true;
            lvGroup.Groups.Clear();
            lvGroup.Items.Clear();
            lvGroup.Columns.Clear();
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                ColumnHeader colHead = new ColumnHeader();
                colHead.Text = dt.Columns[i].Caption.ToString().Trim();
                lvGroup.Columns.Add(colHead);
            }
            if (dt.Rows.Count <= 0) return;

            //事件只能增加一次,所以把这句放在加载窗体的地方
            lvGroup.ColumnClick += new ColumnClickEventHandler(lvGroup_ColumnClick);

           /////------------
            ListViewItem[] lvItems = new ListViewItem[dt.Rows.Count];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string[] tmpVal = new string[dt.Columns.Count];
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    tmpVal[j] = dt.Rows[i][j].ToString().Trim();
                }
                lvItems[i] = new ListViewItem(tmpVal);
            }
            lvGroup.Items.AddRange(lvItems);

            if (isRunningXPOrLater)
            {
                // Create the groupsTable array and populate it with one
                // hash table for each column.
                lvGroupTab = new Hashtable[lvGroup.Columns.Count];
                for (int column = 0; column < lvGroup.Columns.Count; column++)
                {
                    // Create a hash table containing all the groups
                    // needed for a single column.
                    lvGroupTab[column] = CreateGroupsTable(column);
                }

                // Start with the groups created for the Title column.
                lvGroupSetGroups(0);
            }


        }

        void lvGroup_ColumnClick(object sender, ColumnClickEventArgs e)
        {
            try
            {
                if (lvGroup.Sorting == SortOrder.Descending || (isRunningXPOrLater && (e.Column != colGroupIndex)))
                {
                    lvGroup.Sorting = SortOrder.Ascending;
                }
                else
                {
                    lvGroup.Sorting = SortOrder.Descending;
                }
                colGroupIndex = e.Column;              
                if (isRunningXPOrLater)
                {
                    lvGroupSetGroups(e.Column);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("错误信息: " + ex.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        void lvGroupSetGroups(int column)
        {
            lvGroup.Groups.Clear();
            Hashtable groupHashTab = (Hashtable)lvGroupTab[column];

            ListViewGroup[] groupsArray = new ListViewGroup[groupHashTab.Count];
            groupHashTab.Values.CopyTo(groupsArray, 0);
            Array.Sort(groupsArray, new ListViewGroupSorter(lvGroup.Sorting));
            lvGroup.Groups.AddRange(groupsArray);

            // Iterate through the items in myListView, assigning each
            // one to the appropriate group.
            foreach (ListViewItem item in lvGroup.Items)
            {
                // Retrieve the subitem text corresponding to the column.
                string subItemText = item.SubItems[column].Text;

                // For the Title column, use only the first letter.
                //if (column == 0)
                //{
                //    subItemText = subItemText.Substring(0, 1);
                //}

                // Assign the item to the matching group.
                item.Group = (ListViewGroup)groupHashTab[subItemText];
            }
        }

        private class ListViewGroupSorter : IComparer
        {
            private SortOrder order;

            // Stores the sort order.
            public ListViewGroupSorter(SortOrder theOrder)
            {
                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 (order == SortOrder.Ascending)
                {
                    return result;
                }
                else
                {
                    return -result;
                }
            }
        }

        private Hashtable CreateGroupsTable(int column)
        {
            // Create a Hashtable object.
            Hashtable groups = new Hashtable();

            // Iterate through the items in myListView.

            foreach (ListViewItem item in lvGroup.Items)
            {
                // Retrieve the text value for the column.
                string subItemText = item.SubItems[column].Text;

                // Use the initial letter instead if it is the first column.
                //if (column == 0)
                //{
                //    subItemText = 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 (!groups.Contains(subItemText))
                {
                    groups.Add(subItemText, new ListViewGroup(subItemText,
                        HorizontalAlignment.Left));
                }
            }

            // Return the Hashtable object.
            return groups;
        }

        //--------------------------------
 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多