public bool IsWithinConstraint(int?value) { if (value == null) { return(false); } switch (OperatorName) { case TypeOperatorName.Eq: return(value == ConstraintValues[0]); case TypeOperatorName.Lt: return(value < ConstraintValues[0]); case TypeOperatorName.Gt: return(value > ConstraintValues[0]); case TypeOperatorName.LtEq: return(value <= ConstraintValues[0]); case TypeOperatorName.GtEq: return(value >= ConstraintValues[0]); case TypeOperatorName.In: return(ConstraintValues.Contains(value.Value)); default: throw new ArgumentOutOfRangeException($"TypeOperatorName \"{OperatorName}\" doesn't exist."); } }
/// <summary> /// Resets the cached filter values if the filter has been removed. /// </summary> override public void ResetFilter() { if (this.DataGridView == null) { return; } BindingSource source = this.DataGridView.DataSource as BindingSource; if (source == null || String.IsNullOrEmpty(source.Filter)) { filtered = false; m_SelectedConstraintIndex = ConstraintValues.cv_filter_off; m_SelectedFilterText = ""; UpdateFilter(true); } }
///// <summary> ///// Returns a copy of the specified filter string after removing the part that filters the current column, if present. ///// </summary> ///// <param name="filter">The filter string to parse.</param> ///// <returns>A copy of the specified filter string without the current column's filter.</returns> //private String FilterWithoutCurrentColumn(String filter) //{ // // If there is no filter in effect, return String.Empty. // if (String.IsNullOrEmpty(filter)) // { // return String.Empty; // } // // If the column is not filtered, return the filter string unchanged. // if (!filtered) // { // return filter; // } // if (filter.IndexOf(currentColumnFilter) > 0) // { // // If the current column filter is not the first filter, return // // the specified filter value without the current column filter // // and without the preceding " AND ". // return filter.Replace( // " AND " + currentColumnFilter, String.Empty); // } // else // { // if (filter.Length > currentColumnFilter.Length) // { // // If the current column filter is the first of multiple // // filters, return the specified filter value without the // // current column filter and without the subsequent " AND ". // return filter.Replace( // currentColumnFilter + " AND ", String.Empty); // } // else // { // // If the current column filter is the only filter, // // return the empty string. // return String.Empty; // } // } //} /// <summary> /// Updates the BindingSource.Filter value based on a user selection /// from the drop-down filter list. /// </summary> override protected void UpdateFilter(Boolean onlyRefresh = false) { if (!onlyRefresh) { // Continue only if the selection has changed. if ((filterWindow.cmbConstraint.SelectedIndex == (Int32)m_SelectedConstraintIndex) && (filterWindow.txtFilterText.Text.Equals(m_SelectedFilterText, StringComparison.InvariantCultureIgnoreCase))) { return; } // Store the new filter value m_SelectedConstraintIndex = (ConstraintValues)filterWindow.cmbConstraint.SelectedIndex; m_SelectedFilterText = filterWindow.txtFilterText.Text; } // Cast the data source to an IBindingListView. IBindingListView data = this.DataGridView.DataSource as IBindingListView; if ((data == null) && (Retriever == null)) { return; } Debug.Assert((data != null && data.SupportsFiltering) || (Retriever != null), "DataSource is not an IBindingListView or does not support filtering"); if (data != null) { throw new NotImplementedException(); // If the user selection is (All), remove any filter currently // in effect for the column. //if(selectedFilterValue.Count == filters.Count) //{ // //data.Filter = FilterWithoutCurrentColumn(data.Filter); // filtered = false; // //currentColumnFilter.Clear(); // return; //} // Declare a variable to store the filter string for this column. //List<String> newColumnFilter = new List<string>(); // Store the column name in a form acceptable to the Filter property, // using a backslash to escape any closing square brackets. String columnProperty = OwningColumn.DataPropertyName.Replace("]", @"\]"); // Determine the column filter string based on the user selection. // For (Blanks) and (NonBlanks), the filter string determines whether // the column value is null or an empty string. Otherwise, the filter // string determines whether the column value is the selected value. //switch (selectedFilterValue) //{ // case "(Blanks)": // newColumnFilter = String.Format( // "LEN(ISNULL(CONVERT([{0}],'System.String'),''))=0", // columnProperty); // break; // case "(NonBlanks)": // newColumnFilter = String.Format( // "LEN(ISNULL(CONVERT([{0}],'System.String'),''))>0", // columnProperty); // break; // default: //newColumnFilter = String.Format("[{0}]='{1}'", // columnProperty, // ((String)filters[selectedFilterValue]) // .Replace("'", "''")); // break; //} // Determine the new filter string by removing the previous column // filter string from the BindingSource.Filter value, then appending // the new column filter string, using " AND " as appropriate. //String newFilter = FilterWithoutCurrentColumn(data.Filter); //if (String.IsNullOrEmpty(newFilter)) //{ // newFilter += newColumnFilter; //} //else //{ // newFilter += " AND " + newColumnFilter; //} // Set the filter to the new value. try { //data.Filter = newFilter; } catch (InvalidExpressionException ex) { //throw new NotSupportedException( //"Invalid expression: " + newFilter, ex); } // Indicate that the column is currently filtered // and store the new column filter for use by subsequent // calls to the FilterWithoutCurrentColumn method. filtered = true; //currentColumnFilter = newColumnFilter; } else { StringBuilder filterString = new StringBuilder(); switch (m_SelectedConstraintIndex) { case ConstraintValues.cv_filter_off: // leave the string empty filtered = false; break; case ConstraintValues.cv_equals: filterString.AppendFormat("({0} = {1})", this.OwningColumn.DataPropertyName, IBE.SQL.DBConnector.SQLAEscape(m_SelectedFilterText)); filtered = true; break; case ConstraintValues.cv_contains: filterString.AppendFormat("({0} like '%{1}%')", this.OwningColumn.DataPropertyName, IBE.SQL.DBConnector.SQLEscape(m_SelectedFilterText)); filtered = true; break; case ConstraintValues.cv_starts_with: filterString.AppendFormat("({0} like '{1}%')", this.OwningColumn.DataPropertyName, IBE.SQL.DBConnector.SQLEscape(m_SelectedFilterText)); filtered = true; break; case ConstraintValues.cv_ends_with: filterString.AppendFormat("({0} like '%{1}')", this.OwningColumn.DataPropertyName, IBE.SQL.DBConnector.SQLEscape(m_SelectedFilterText)); filtered = true; break; } Retriever.SetFilter(this.OwningColumn.Name, filterString.ToString()); } }
/// <summary> /// Resets the cached filter values if the filter has been removed. /// </summary> private void ResetFilter() { if (this.DataGridView == null) return; BindingSource source = this.DataGridView.DataSource as BindingSource; if (source == null || String.IsNullOrEmpty(source.Filter)) { filtered = false; m_ConstraintIndex = ConstraintValues.cv_filter_off; m_FilterText = ""; //selectedFilterValue.Clear(); //currentColumnFilter.Clear(); } }
///// <summary> ///// Returns a copy of the specified filter string after removing the part that filters the current column, if present. ///// </summary> ///// <param name="filter">The filter string to parse.</param> ///// <returns>A copy of the specified filter string without the current column's filter.</returns> //private String FilterWithoutCurrentColumn(String filter) //{ // // If there is no filter in effect, return String.Empty. // if (String.IsNullOrEmpty(filter)) // { // return String.Empty; // } // // If the column is not filtered, return the filter string unchanged. // if (!filtered) // { // return filter; // } // if (filter.IndexOf(currentColumnFilter) > 0) // { // // If the current column filter is not the first filter, return // // the specified filter value without the current column filter // // and without the preceding " AND ". // return filter.Replace( // " AND " + currentColumnFilter, String.Empty); // } // else // { // if (filter.Length > currentColumnFilter.Length) // { // // If the current column filter is the first of multiple // // filters, return the specified filter value without the // // current column filter and without the subsequent " AND ". // return filter.Replace( // currentColumnFilter + " AND ", String.Empty); // } // else // { // // If the current column filter is the only filter, // // return the empty string. // return String.Empty; // } // } //} /// <summary> /// Updates the BindingSource.Filter value based on a user selection /// from the drop-down filter list. /// </summary> private void UpdateFilter() { // Continue only if the selection has changed. if((textSelectBox.cmbConstraint.SelectedIndex == (Int32)m_ConstraintIndex) && (textSelectBox.txtFilterText.Text.Equals(m_FilterText, StringComparison.InvariantCultureIgnoreCase))) { return; } // Store the new filter value m_ConstraintIndex = (ConstraintValues)textSelectBox.cmbConstraint.SelectedIndex; m_FilterText = textSelectBox.txtFilterText.Text; // Cast the data source to an IBindingListView. IBindingListView data = this.DataGridView.DataSource as IBindingListView; Debug.Assert((data != null && data.SupportsFiltering) || (Retriever != null), "DataSource is not an IBindingListView or does not support filtering"); if(data != null) { throw new NotImplementedException(); // If the user selection is (All), remove any filter currently // in effect for the column. //if(selectedFilterValue.Count == filters.Count) //{ // //data.Filter = FilterWithoutCurrentColumn(data.Filter); // filtered = false; // //currentColumnFilter.Clear(); // return; //} // Declare a variable to store the filter string for this column. //List<String> newColumnFilter = new List<string>(); // Store the column name in a form acceptable to the Filter property, // using a backslash to escape any closing square brackets. String columnProperty = OwningColumn.DataPropertyName.Replace("]", @"\]"); // Determine the column filter string based on the user selection. // For (Blanks) and (NonBlanks), the filter string determines whether // the column value is null or an empty string. Otherwise, the filter // string determines whether the column value is the selected value. //switch (selectedFilterValue) //{ // case "(Blanks)": // newColumnFilter = String.Format( // "LEN(ISNULL(CONVERT([{0}],'System.String'),''))=0", // columnProperty); // break; // case "(NonBlanks)": // newColumnFilter = String.Format( // "LEN(ISNULL(CONVERT([{0}],'System.String'),''))>0", // columnProperty); // break; // default: //newColumnFilter = String.Format("[{0}]='{1}'", // columnProperty, // ((String)filters[selectedFilterValue]) // .Replace("'", "''")); // break; //} // Determine the new filter string by removing the previous column // filter string from the BindingSource.Filter value, then appending // the new column filter string, using " AND " as appropriate. //String newFilter = FilterWithoutCurrentColumn(data.Filter); //if (String.IsNullOrEmpty(newFilter)) //{ // newFilter += newColumnFilter; //} //else //{ // newFilter += " AND " + newColumnFilter; //} // Set the filter to the new value. try { //data.Filter = newFilter; } catch (InvalidExpressionException ex) { //throw new NotSupportedException( //"Invalid expression: " + newFilter, ex); } // Indicate that the column is currently filtered // and store the new column filter for use by subsequent // calls to the FilterWithoutCurrentColumn method. filtered = true; //currentColumnFilter = newColumnFilter; } else { StringBuilder filterString = new StringBuilder(); switch (m_ConstraintIndex) { case ConstraintValues.cv_filter_off: // leave the string empty filtered = false; break; case ConstraintValues.cv_equals: filterString.AppendFormat("({0} = {1})",this.OwningColumn.DataPropertyName, IBE.SQL.DBConnector.SQLAEscape(m_FilterText)); filtered = true; break; case ConstraintValues.cv_contains: filterString.AppendFormat("({0} like '%{1}%')",this.OwningColumn.DataPropertyName, IBE.SQL.DBConnector.SQLEscape(m_FilterText)); filtered = true; break; case ConstraintValues.cv_starts_with: filterString.AppendFormat("({0} like '{1}%')",this.OwningColumn.DataPropertyName, IBE.SQL.DBConnector.SQLEscape(m_FilterText)); filtered = true; break; case ConstraintValues.cv_ends_with: filterString.AppendFormat("({0} like '%{1}')",this.OwningColumn.DataPropertyName, IBE.SQL.DBConnector.SQLEscape(m_FilterText)); filtered = true; break; } Retriever.SetFilter(this.OwningColumn.Name, filterString.ToString()); } }
public TypeOperator(string typeOperatorName, IEnumerable <int> constraintValues) { ConstraintValues.AddRange(constraintValues); OperatorName = Convert(typeOperatorName); }