This is a migrated thread and some comments may be shown as answers.

[Solved] Filter by a regular expresion

2 Answers 548 Views
Grid
This is a migrated thread and some comments may be shown as answers.
maria mazilu
Top achievements
Rank 1
maria mazilu asked on 12 Feb 2010, 09:20 AM
I have a grid in which the records have special characters like "ă" , "â", "î", "ÅŸ", "Å£". When I filter a certain column I want as response the records containing these special characters although in the corresponding filter textbox I type "a" instead-of "ă" or "â".  In other word in column I have the records "mamă" and "mama", when I type in the filter text-box "mama" I want to get both records.
I figured that the best method would be to use regular expressions to ignore the a and t and s and i.

2 Answers, 1 is accepted

Sort by
0
maria mazilu
Top achievements
Rank 1
answered on 14 Feb 2010, 10:49 AM
Hi there,
I am stuck with this filtering problem. In order to do the filtering with a regular expression I figured that the only solution is to alter the Filtering expression so, in order to get that done wright I want to understand better how the filtering expression works if I can integrate there a regular expression match a certain pattern. Because so far I managed to do this:
 protected void grdAgenda_OnItemCommand(object sender, GridCommandEventArgs e)
 {
  if
((e.CommandName=="Filter")) 
            { 
                wholeFilter = ""
                foreach (GridColumn column in e.Item.OwnerTableView.Columns) 
                { 
                    newFilter = column.CurrentFilterValue; 
                    if (column.UniqueName == "Nume"
                    { 
 
 
                        if (newFilter != ""
                        { 
                            Regex RE = new Regex("a"); 
                            newFilter = RE.Replace(newFilter, @"[aăâ]"); 
                            RE = null; RE = new Regex("t"); 
                            newFilter = RE.Replace(newFilter, @"[tÅ£]"); 
                            RE = null; RE = new Regex("s"); 
                            newFilter = RE.Replace(newFilter, @"[sÅŸ]"); 
                            RE = null; RE = new Regex("i"); 
                            newFilter = RE.Replace(newFilter, @"[iî]"); 
                            pattern = new Regex(newFilter); 
                            if (wholeFilter != ""
                            { 
                                wholeFilter = wholeFilter + " AND Regex.IsMatch(Nume.ToString(),"+'"'+pattern+'"'+", RegexOptions.IgnoreCase)";
                            } 
                            else 
                            { 
                                wholeFilter =  "Regex.IsMatch(Nume.ToString(),"+'"'+pattern+'"'+", RegexOptions.IgnoreCase)";
                            } 
                        } 
                    } 
                    else 
                        if (newFilter!=""
                    { 
                     
                        if (wholeFilter != ""
                        { 
                            wholeFilter = wholeFilter + " AND (iif(" + column.UniqueName + " == null, " + '"' + '"' + ", " + column.UniqueName + ").ToString().ToUpper().Contains(" + '"' + newFilter + '"' + ".ToUpper()))"
                        } 
                        else 
                        { 
                            wholeFilter = "(iif(" + column.UniqueName + " == null, " + '"' + '"' + ", " + column.UniqueName + ").ToString().ToUpper().Contains(" + '"' + newFilter + '"' + ".ToUpper()))"
                        } 
                    } 
                     
                } 
                 
            } 
             
        } 
        protected void grdAgenda_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e) 
        { 
            List<AgendaTelefon> ListAg = null
            ListAg = AgendaTelefonSrvr.Load(); 
            string filtru = grdAgenda.MasterTableView.FilterExpression; 
            grdAgenda.MasterTableView.FilterExpression = wholeFilter; 
             grdAgenda.DataSource = ListAg; 
              
        } 
         
        public void ConfigureExport() 
        { 
            grdAgenda.ExportSettings.ExportOnlyData = true
            grdAgenda.ExportSettings.IgnorePaging = true
        } 
        protected void Button1_Click(object sender, System.EventArgs e) 
        { 
            ConfigureExport(); 
            grdAgenda.MasterTableView.ExportToExcel(); 
        } 
 
 but unfortunately when I do this when filtering for "Nume" I doesn't happen anythig. This seams to me that the grid doesn't know how to handle the IsMatch.
Another question is why if I set the FilterExpression in OnItemCommand event handler it doesn't remains the same and in OnNeedDataSource event handler it has the predefined value
(iif(Nume == null, \"\", Nume).ToString().ToUpper().Contains(\"mad\".ToUpper()))
and not the one set by me with regex? In want moment does it buid again the filterexpression string and why a regex doesn't suits the situation? Please help!
Any suggestions?
Thank you
0
maria mazilu
Top achievements
Rank 1
answered on 15 Feb 2010, 09:58 PM
After attentively reading the demos and the discussions on this forum I realized that a regular expression isn't suitable as the FilterExpression must be compatible Linq expression. Therefore, I had to rethink the situation and have found the following workaround: in order to avoid the special characters I am building a custom LinqExpression where every special character is replaced by it's match in the Latin alphabet. Also I am rewriting the whole FilterExpression. Bellow is the listed workaround that I found suitable for me (with two comboboxes as filtertemplates in grid):
protected void grdAgenda_OnItemCommand(object sender, GridCommandEventArgs e) 
        { 
            if (e.CommandName == RadGrid.FilterCommandName) 
            { 
                e.Canceled = true
                grdAgenda.MasterTableView.FilterExpression = ""
                string wholeFilter = ""
                string currentFilterValue = ""
                foreach (GridColumn column in e.Item.OwnerTableView.Columns) 
                { 
                    string colName = column.UniqueName; 
                    GridKnownFunction columncolumnFilterFunction = column.CurrentFilterFunction; 
                    currentFilterValue = e.Item.OwnerTableView.GetColumn(colName).CurrentFilterValue; 
                    if ((colName=="State") || (colName=="City")) 
                    {     
                        columnFilterFunction = GridKnownFunction.EqualTo; 
                    } 
                     
 
                    if (currentFilterValue != "") 
                    { 
                        string newFilter=""
                        switch (columnFilterFunction) 
                        { 
                            case GridKnownFunction.Custom: 
                                newFilter = "(iif(" + colName + "==null,"+'"'+'"'+"," + colName + ")).ToString().ToLower().Replace('ă','a').Replace('â','a').Replace('î','i').Replace('ÅŸ','s').Replace('Å£','t').Contains(" + '"' + currentFilterValue.ToLower() + '"' + ')'; 
                                if (wholeFilter == "") 
                                { 
                                    wholeFilter = newFilter
                                } 
                                else 
                                { 
                                    wholeFilter = "((" + wholeFilter + ") AND (" + newFilter + "))"; 
                                } 
                                break; 
                            case GridKnownFunction.Contains: 
                                newFilter = "(iif(" + colName + "==null," + '"' + '"' + ',' + colName + ").ToString().ToUpper().Contains(" + '"' + currentFilterValue.ToUpper() + '"' + "))"; 
                                if (wholeFilter == "") 
                                { 
                                    wholeFilter = newFilter
                                } 
                                else 
                                { 
                                    wholeFilter = "((" + wholeFilter + ") AND (" + newFilter + "))"; 
                                } 
                                break; 
                            case GridKnownFunction.StartsWith: 
                                newFilter = "(iif( Nume" + colName + "==null," + '"' + '"' + ", Nume" + colName + ").ToString().ToUpper().StartsWith(" + '"' + currentFilterValue.ToUpper() + '"' + "))"; 
                                if (wholeFilter == "") 
                                { 
                                    wholeFilter = newFilter
                                } 
                                else 
                                { 
                                    wholeFilter = "((" + wholeFilter + ") AND (" + newFilter + "))"; 
                                } 
                                break; 
                            case GridKnownFunction.EqualTo: 
                                newFilter = "Nume"+colName+"==" + '"' + currentFilterValue + '"'; 
                                if (wholeFilter == "") 
                                { 
                                    wholeFilter = newFilter
                                } 
                                else 
                                { 
                                    wholeFilter = "((" + wholeFilter + ") AND (" + newFilter + "))"; 
                                } 
                                break; 
                        } 
                    } 
                } 
                grdAgenda.MasterTableView.FilterExpression = wholeFilter
                grdAgenda.Rebind(); 
            } 
        } 
 
Sorry for the bad posts earlier!

Cheers!
Tags
Grid
Asked by
maria mazilu
Top achievements
Rank 1
Answers by
maria mazilu
Top achievements
Rank 1
Share this question
or