public void Clear() { super.Clear(); }
/// <summary> /// Populates the filters dictionary with formatted and unformatted string /// representations of each unique value in the column, accounting for all /// filters except the current column's. Also adds special filter options. /// </summary> private void PopulateFilters() { ArrayList list = null; Boolean containsBlanks; Boolean containsNonBlanks; String oldFilter = ""; // Continue only if there is a DataGridView. if (this.DataGridView == null) { return; } // Cast the data source to a BindingSource. BindingSource data = this.DataGridView.DataSource as BindingSource; Debug.Assert((data != null && data.SupportsFiltering && OwningColumn != null) || (Retriever != null), "DataSource is not a BindingSource, or does not support filtering, or OwningColumn is null"); containsBlanks = false; containsNonBlanks = false; // Reset the filters dictionary and initialize some flags // to track whether special filter options are needed. filters.Clear(); if (data != null) { // Prevent the data source from notifying the DataGridView of changes. data.RaiseListChangedEvents = false; // Cache the current BindingSource.Filter value and then change // the Filter property to temporarily remove any filter for the // current column. oldFilter = data.Filter; //data.Filter = FilterWithoutCurrentColumn(oldFilter); // Initialize an ArrayList to store the values in their original // types. This enables the values to be sorted appropriately. list = new ArrayList(data.Count); // Retrieve each value and add it to the ArrayList if it isn't // already present. foreach (Object item in data) { Object value = null; // Use the ICustomTypeDescriptor interface to retrieve properties // if it is available; otherwise, use reflection. The // ICustomTypeDescriptor interface is useful to customize // which values are exposed as properties. For example, the // DataRowView class implements ICustomTypeDescriptor to expose // cell values as property values. // // Iterate through the property names to find a case-insensitive // match with the DataGridViewColumn.DataPropertyName value. // This is necessary because DataPropertyName is case- // insensitive, but the GetProperties and GetProperty methods // used below are case-sensitive. ICustomTypeDescriptor ictd = item as ICustomTypeDescriptor; if (ictd != null) { PropertyDescriptorCollection properties = ictd.GetProperties(); foreach (PropertyDescriptor property in properties) { if (String.Compare(this.OwningColumn.DataPropertyName, property.Name, true /*case insensitive*/, System.Globalization.CultureInfo.InvariantCulture) == 0) { value = property.GetValue(item); break; } } } else { PropertyInfo[] properties = item.GetType().GetProperties( BindingFlags.Public | BindingFlags.Instance); foreach (PropertyInfo property in properties) { if (String.Compare(this.OwningColumn.DataPropertyName, property.Name, true /*case insensitive*/, System.Globalization.CultureInfo.InvariantCulture) == 0) { value = property.GetValue(item, null /*property index*/); break; } } } // Skip empty values, but note that they are present. if (value == null || value == DBNull.Value) { containsBlanks = true; continue; } // Add values to the ArrayList if they are not already there. if (!list.Contains(value)) { list.Add(value); } } } else { // Initialize an ArrayList to store the values in their original // types. This enables the values to be sorted appropriately. DataTable dataTable = new DataTable(); try { IBE.Program.DBCon.Execute(String.Format("{0} {1}", RetrieverSQLSelect, Retriever.BaseStatement), dataTable); list = new ArrayList(dataTable.Rows.Count); foreach (DataRow selectableItem in dataTable.Rows) { list.Add(selectableItem[0]); } } catch (Exception) { list = new ArrayList(); } } // Sort the ArrayList. The default Sort method uses the IComparable // implementation of the stored values so that string, numeric, and // date values will all be sorted correctly. list.Sort(); // Convert each value in the ArrayList to its formatted representation // and store both the formatted and unformatted string representations // in the filters dictionary. foreach (Object value in list) { // Use the cell's GetFormattedValue method with the column's // InheritedStyle property so that the dropDownListBox.FilterListBox format // will match the display format used for the column's cells. String formattedValue = null; DataGridViewCellStyle style = OwningColumn.InheritedStyle; formattedValue = (String)GetFormattedValue(value, -1, ref style, null, null, DataGridViewDataErrorContexts.Formatting); if (String.IsNullOrEmpty(formattedValue)) { // Skip empty values, but note that they are present. containsBlanks = true; } else if (!filters.Contains(formattedValue)) { // Note whether non-empty values are present. containsNonBlanks = true; // For all non-empty values, add the formatted and // unformatted string representations to the filters // dictionary. filters.Add(formattedValue, value.ToString()); } } if (data != null) { // Restore the filter to the cached filter string and // re-enable data source change notifications. if (oldFilter != null) { data.Filter = oldFilter; } data.RaiseListChangedEvents = true; } // Add special filter options to the filters dictionary // along with null values, since unformatted representations // are not needed. if (containsBlanks && containsNonBlanks) { filters.Add("(Blanks)", null); filters.Add("(NonBlanks)", null); } }
/// <summary> /// Удалить все фильтры /// </summary> public void DeleteAllFilters() { filters.Clear(); Invalidate(); }