简体   繁体   中英

Combine DataGrid filter string from CheckBoxes and TextBoxes WPF MVVM

I have 2 CheckBoxes and 4 TextBoxes in my application. I am trying to build filter string based on these 6 parameters.

Here is my current code:

   // Binding checkbox FIANDSE Bool
    private bool _FIANDSEBool;
    public bool FIANDSEBool
    {
        get => this._FIANDSEBool;
        set
        {
            this._FIANDSEBool = value;
            OnPropertyChanged();

            // Refresh the DataTable filter expression
            EnableRowFiltering();
        }
    }

    // Binding checkbox ACTIVE Bool
    private bool _ACTIVEBool;
    public bool ACTIVEBool
    {
        get => this._ACTIVEBool;
        set
        {
            this._ACTIVEBool = value;
            OnPropertyChanged();

            // Refresh the DataTable filter expression
            EnableRowFiltering();
        }
    }

    public void EnableRowFiltering()
    {
        try
        {
            string FIANDSEBoolquery = FIANDSEBool ? " AND YRNRO LIKE '6*' OR YRNRO LIKE '7*'" : "";
            string ACTIVEBoolquery = ACTIVEBool ? " AND KAYTOSSA='1'" : "";

            this.MainDataTable.DefaultView.RowFilter =
              $"YRNRO LIKE '{this.YRNROSearchKey}*'" +
              $"AND HAKUNIMI LIKE '{this.HAKUNIMISearchKey}*'" +
              $"AND KONSERNI LIKE '{this.GROUPSearchKey}*'" +
              $"AND LY LIKE '{this.BUSINESSIDSearchKey}*'" +
              FIANDSEBoolquery + ACTIVEBoolquery;

        }
        catch (System.Exception)
        {

            // Do nothing
        }
    } 

It looks like string ACTIVEBoolquery = ACTIVEBool ? " AND KAYTOSSA='1'" : ""; string ACTIVEBoolquery = ACTIVEBool ? " AND KAYTOSSA='1'" : ""; is working fine, but string FIANDSEBoolquery = FIANDSEBool ? " AND YRNRO LIKE '6*' OR YRNRO LIKE '7*'" : ""; FIANDSEBoolquery = FIANDSEBool ? " AND YRNRO LIKE '6*' OR YRNRO LIKE '7*'" : ""; is probably somehow overriding with $"YRNRO LIKE '{this.YRNROSearchKey}*'" ?

Currently if I apply FIANDSEBoolquery and input some text to other TextBoxes, my filter does not work = query is messed up?. However if I apply ACTIVEBoolquery and deactivate FIANDSEBoolquery , everything seems to be working fine.

Intention of CheckBox string ACTIVEBoolquery = ACTIVEBool ? " AND KAYTOSSA='1'" : ""; string ACTIVEBoolquery = ACTIVEBool ? " AND KAYTOSSA='1'" : ""; is to filter out all unnecessary rows with values starting with 1, 5, 9 etc. and leave only starting with 6 and 7. Intention of TextBox $"YRNRO LIKE '{this.YRNROSearchKey}*'" is to filter out more precise if user inputs for example 74003 or 63243 etc.

Any suggestions how get this working?


EDIT:

在此处输入图片说明

Try to add some parentheses around the expression in FIANDSEBoolquery :

string FIANDSEBoolquery = FIANDSEBool ? " AND (YRNRO LIKE '6*' OR YRNRO LIKE '7*') " 
    : string.Empty;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM