Exemplo n.º 1
0
        /// <summary>
        /// Get the group for the current ListViewItem based on the current properties
        /// If the group is not present, it will be dynamically added
        /// </summary>
        /// <param name="item"></param>
        /// <param name="props"></param>
        /// <returns></returns>
        private ListViewGroup GetListItemGroup(object item, Dictionary <string, PropertyInfo> props = null)
        {
            if (props == null)
            {
                props = ListItemType.GetProperties().ToDictionary(p => p.Name, p => p);
            }
            // set up group if a group by col has been set
            ListViewGroup group = null;

            if (GroupByCol != null)
            {
                // only add new group if not already there
                var label    = Utility.GetPropertyValue <string>(item, props[GroupByCol.Name]) ?? "(no value)";
                var groupVal = $"{GroupByCol.DisplayName}: {label}";

                group = ListViewMain.Groups[groupVal];

                if (group == null)
                {
                    group = new ListViewGroup(groupVal, HorizontalAlignment.Left)
                    {
                        Header = groupVal,
                        Name   = groupVal,
                        Tag    = groupVal
                    };
                    ListViewMain.Groups.Add(group);
                }
            }
            return(group);
        }
Exemplo n.º 2
0
        /// <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;
        }
Exemplo n.º 3
0
        /// <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();
        }
Exemplo n.º 4
0
        /// <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());
        }