/// <summary> /// Toggle all or none checked in the main list view /// </summary> /// <param name="checkAll">flag indicating whether to check all items in the ListView</param> private void CheckAllNone(bool checkAll) { _performingBulkSelection = true; ListViewMain.SuspendLayout(); // if check all and we have checkboxes enabled, then do some work if (checkAll && Checkboxes) { foreach (ListViewItem item in ListViewMain.Items) { item.Checked = true; } } else { foreach (ListViewItem item in ListViewMain.CheckedItems) { item.Checked = false; } } ListViewMain.ResumeLayout(); _performingBulkSelection = false; // now that we have an updated list view, udpate the list of selected items UpdateSelectedItemsList(); }
/// <summary> /// Load the Items into the list view, binding the columns based on the control properties /// </summary> internal void PopulateListView() { ListViewMain.SuspendLayout(); ListViewMain.Enabled = false; ListViewMain.Items.Clear(); ListViewMain.Refresh(); // persist the list of list view items for the filtering _listViewItemsColl = new List <ListViewItem>(); if (_allItems != null && _allItems?.Count > 0) { var cols = ListViewMain.Columns; var props = ListItemType.GetProperties().ToDictionary(p => p.Name, p => p); foreach (var item in _allItems) { var col = cols[0]; var colDef = col.Tag as ListViewColumnDef; // get the ListView group for the current row/ListViewItem var group = GetListItemGroup(item, props); var text = Utility.GetPropertyValue <string>(item, props[col.Name]); // new list view item var lvItem = new ListViewItem() { Name = cols[0].Name, ImageIndex = 0, StateImageIndex = 0, Text = text, Tag = item, // stash the template here so we can view details later Group = group }; for (var i = 1; i < cols.Count; i++) { var prop = props[cols[i].Name]; var colVal = Utility.GetPropertyValue <string>(item, prop); var subitem = new ListViewItem.ListViewSubItem(lvItem, colVal) { Name = cols[i].Name }; lvItem.SubItems.Add(subitem); } // add to the internal collection of ListView Items and the external list _listViewItemsColl.Add(lvItem); } ListViewMain.Items.AddRange(_listViewItemsColl.ToArray <ListViewItem>()); // now auto size using the values specified ListViewMain.AutoResizeColumns(AutosizeColumns); } ListViewMain.ResumeLayout(); ListViewMain.Enabled = true; }
/// <summary> /// Helper method for setting the fore and back color for the selected row /// </summary> /// <param name="backColor"></param> /// <param name="foreColor"></param> private void SetSelectedHighlight(Color backColor, Color foreColor) { ListViewMain.SuspendLayout(); if (ListViewMain.SelectedItems.Count > 0) { var selItem = ListViewMain.SelectedItems[0]; selItem.ForeColor = foreColor; selItem.BackColor = backColor; } ListViewMain.ResumeLayout(); }
/// <summary> /// Set up the ListView Columns either using the Col Def list provided, /// or generically with the object property definitions from object metadata /// </summary> private void SetUpListViewColumns() { var cols = new List <ColumnHeader>(); if (ListViewColDefs.Length > 0) { // use the list of predefined columns for the list view. foreach (var colDef in ListViewColDefs.OrderBy(o => o.Order)) { cols.Add(new ColumnHeader() { Name = colDef.Name, Text = colDef.DisplayName, DisplayIndex = colDef.Order, Width = colDef.Width, Tag = colDef }); } } else if (ListItemType != null) { // render all of the properties for the bound list object type PropertyInfo[] props = ListItemType.GetProperties(); foreach (PropertyInfo p in props) { cols.Add(new ColumnHeader() { Name = p.Name, Text = p.Name, Width = 100 }); } } // if the two are the same, then no need to reset if (ListViewMain.Columns.Count == cols.Count) { var listCols = ListViewMain.Columns.Cast <ColumnHeader>().Select(c => c.Name); if (cols.Select(c => c.Name).SequenceEqual(listCols)) { return; } } ListViewMain.SuspendLayout(); ListViewMain.Columns.Clear(); ListViewMain.Columns.AddRange(cols.ToArray()); ListViewMain.ResumeLayout(); }
/// <summary> /// Sort the current list of items in the ListView /// </summary> /// <param name="sortColumn">ListView column index to be sorted</param> /// <param name="sortOrder">Sort order for the selected column</param> public void SortList(int sortColumn, SortOrder?sortOrder = null) { // toggle the sort order if not passed as a param if (sortOrder == null) { sortOrder = (ListViewMain.Sorting == SortOrder.Ascending) ? SortOrder.Descending : SortOrder.Ascending; } _performingBulkSelection = true; ListViewMain.SuspendLayout(); // update the main list and save the values to properties ListViewMain.Sorting = sortOrder.Value; ListSortOrder = ListViewMain.Sorting; ListSortColumn = sortColumn; // now apply the sorter helper ListViewMain.ListViewItemSorter = new ListViewItemComparer(ListSortColumn, ListViewMain.Sorting); _performingBulkSelection = false; ListViewMain.ResumeLayout(); }
/// <summary> /// Filter the list using the text in the text box. /// </summary> private void FilterList() { string filterText = textFilterList.Text.ToLower(); List <ListViewItem> newList = null; _performingBulkSelection = true; ListViewMain.SuspendLayout(); // filter the cols if (filterText.Length > 3) { // get the filter cols... this allows us to check the correct col for the value // if none found, default to first col var filterCols = ListViewColDefs.Where(c => c.IsFilterColumn == true).ToList(); if (filterCols.Count == 0) { filterCols = ListViewColDefs.OrderBy(c => c.Order).Take(1).ToList(); } newList = new List <ListViewItem>(); foreach (var col in filterCols) { // filter the master list and bind it to the list view. Col 0 is the text while the rest are subitems/cols var curr = _listViewItemsColl .Where(i => (col.Order == 0) ? i.Text.ToLower().Contains(filterText) : i.SubItems[col.Name].Text.ToLower().Contains(filterText) ); // add to the current list newList.AddRange(curr.Except(newList)); } } else { if (ListViewMain.Items.Count != _listViewItemsColl.Count) { newList = _listViewItemsColl; } } // if we have a new list to be set, clear and reset groups if (newList != null) { ListViewMain.Items.Clear(); ListViewMain.Items.AddRange(newList.ToArray <ListViewItem>()); var props = ListItemType.GetProperties().ToDictionary(p => p.Name, p => p); // now reset the group for each list item foreach (var item in newList) { item.Group = GetListItemGroup(item.Tag, props); } // now that we have an updated list view, udpate the list of selected items UpdateSelectedItemsList(); } _performingBulkSelection = false; ListViewMain.ResumeLayout(); FilterListComplete?.Invoke(this, new EventArgs()); }