Flexible usage of ListView and flexible usage of ListView
The following is an example:
The ListView control of WinForm can be displayed in groups and sorted.
You can set the View attribute of ListView to Details.
For a complete project, go to the following URL to find and download
Http://hovertree.com/hovertreescj/
Or:
Http://hovertree.com/h/bjaf/scjyuanma.htm
The specific implementation is in the HoverTreeWindowsFormsDemo project, which is located in the HtDemo folder.
The following code is used:
/* http://hovertree.com/hovertreescj/ This example shows how to use the ListView group to display data. 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 ColumnHea Der (); 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 "," Ke leyi "," 2012 "}); ListViewItem item2 = new ListV IewItem (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, ite M3, 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 (col Umn); // 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, ColumnClickE VentArgs 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 ListViewGro UpSorter (listView_HoverTree.Sorting); iterator (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 n Ot 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 ;}}}}}