示例#1
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();
        }
示例#2
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());
        }