using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.ComponentModel; using System.Data; using System.Globalization; using System.Linq; using System.Text; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Data; using ExtendedGrid.ExtendedGridControl; using ExtendedGrid.Styles; namespace ExtendedGrid.Classes { internal sealed class AutoFilterHelper { public AutoFilterHelper(ExtendedDataGrid grid) { CurrentGrid = grid; } public ListBox CurrentSigmaListBox{ get; set; } public ObservableCollection CurrentDistictValues { get; set; } public ListBox CurrentListBox { get; set; } public string CurrentColumName { get; set; } public ExtendedDataGrid CurrentGrid { get;private set; } public string CurrentSigmaColumn { get; set; } public ObservableCollection GetDistictValues(DataGrid grid, string columnName) { if(grid.ItemsSource is DataView) { var itemSource = (DataView)grid.ItemsSource; var ditictValues = new ObservableCollection { new CheckedListItem {IsChecked = false, Name = "(Select All)", IsSelectAll = "(Select All)"} }; DataTable table = itemSource.Table; List filteredValues = GetFilteredColumnValues(columnName, itemSource); foreach (DataRow row in table.Rows) { object value = row[columnName]; if (ditictValues.Count(c => Convert.ToString(c.Name) == Convert.ToString(value) && c.IsSelectAll != "(Select All)") == 0) { if (!string.IsNullOrEmpty(Convert.ToString(value))) { ditictValues.Add(new CheckedListItem { Name = value, IsChecked = filteredValues.Contains("'" + value + "'") }); } else if (value == null) { ditictValues.Add(new CheckedListItem { Name = null, IsChecked = filteredValues.Contains(null) }); } else if (string.IsNullOrEmpty(Convert.ToString(value))) { ditictValues.Add(new CheckedListItem { Name = value, IsChecked = filteredValues.Contains("") }); } } CurrentDistictValues = ditictValues; } if (ditictValues.Count(c => c.IsChecked) == ditictValues.Count - 1) ditictValues[0].IsChecked = true; return ditictValues; } else if (CollectionViewSource.GetDefaultView(grid.ItemsSource) != null) { var ditictValues = new ObservableCollection { new CheckedListItem {IsChecked = false, Name = "(Select All)", IsSelectAll = "(Select All)"} }; ICollectionView view = CollectionViewSource.GetDefaultView(grid.ItemsSource); _view = view; var unquieValues=new HashSet(); foreach (var rowData in grid.ItemsSource) { var propertyValue = rowData.GetType().GetProperty(columnName); if(propertyValue!=null) { var data = propertyValue.GetValue(rowData, null) == null ? null : Convert.ToString(propertyValue.GetValue(rowData, null)); if(!unquieValues.Contains(data)) { unquieValues.Add(data); } } } List filteredValues = string.IsNullOrEmpty(FilterExpression)?new List() : GetFilteredColumnValues(columnName, FilterExpression); foreach(var value in unquieValues) { if (ditictValues.Count(c => Convert.ToString(c.Name) == value && c.IsSelectAll != "(Select All)") == 0) { if (!string.IsNullOrEmpty(Convert.ToString(value))) { ditictValues.Add(new CheckedListItem { Name = value, IsChecked = filteredValues.Contains("'" + value + "'") }); } else if (value == null) { ditictValues.Add(new CheckedListItem { Name = null, IsChecked = filteredValues.Contains(null) }); } else if (string.IsNullOrEmpty(Convert.ToString(value))) { ditictValues.Add(new CheckedListItem { Name = value, IsChecked = filteredValues.Contains("") }); } } } CurrentDistictValues = ditictValues; return ditictValues; } return null; } public void ApplyFilters(DataGrid grid, string columnName, object value) { value = AddEscapeSequece(value); if(grid.ItemsSource is DataView) { var itemSource = (DataView)grid.ItemsSource; if (itemSource == null) return; if (!string.IsNullOrEmpty(itemSource.RowFilter)) { string filter = FilterGenerator(itemSource.RowFilter, columnName, value); if (filter != itemSource.RowFilter) { itemSource.RowFilter = CorrectRowFilter(filter); grid.Items.Refresh(); } } else { if (string.IsNullOrEmpty(Convert.ToString(value))) { string dummy = value == null ? null : ""; switch (dummy) { case "": itemSource.RowFilter = "[" + columnName + "]" + " = ''"; break; case null: itemSource.RowFilter = "[" + columnName + "]" + " IS Null"; break; } } else if(itemSource.Table.Columns[columnName].DataType.BaseType.ToString()=="System.Enum") { var value1 = Convert.ToInt32(Enum.Parse(itemSource.Table.Columns[columnName].DataType, Convert.ToString(value))); itemSource.RowFilter = "[" + columnName + "]" + " " + " IN " + "(" + value1 + ")"; } else itemSource.RowFilter = "[" + columnName + "]" + " " + " IN " + "(" + "'" + value + "'" + ")"; } int count = CurrentDistictValues.Count(c => c.IsChecked); if (count == CurrentDistictValues.Count - 1) { CurrentDistictValues[0].IsChecked = true; if (CurrentListBox != null) { CurrentListBox.ItemsSource = CurrentDistictValues; CurrentListBox.UpdateLayout(); CurrentListBox.Items.Refresh(); } } if (CurrentListBox != null) { var clearButton = FindControls.FindChild