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

Programmatically created grid, EnableColumnsViewstate=false and filtering

15 Answers 94 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ferdinand
Top achievements
Rank 1
Ferdinand asked on 12 Dec 2011, 03:50 PM
Hi,
I have a similar problem as mentioned in the following thread:
http://www.telerik.com/community/forums/aspnet-ajax/grid/issue-with-filtering-on-multiple-columns-when-mastertableview-enablecolumnsviewstate-false.aspx
I need to have the EnableColumnsViewState disabled and make the grid to be filtered by two columns. At the end of the thread mentioned it is said to create the FilterExpression over and over again. I tried to do that by saving the CurrentFilterValue and the CurrentFilterFunction and using the EvaluateFilterExpression method of the column.
But most of the time when the EvaluateFilterExpression is called it returns string.Empty even after I have reset the CurrentFilterValue and the CurrentFilterFunction of the column.

So my question is if there is another way to evaluate the FilterExpression. Or are there other properties to be set to evaluate a column's FilterExpression correctly?

Best regards

Ferdinand

15 Answers, 1 is accepted

Sort by
0
Tsvetina
Telerik team
answered on 15 Dec 2011, 09:11 AM
Hi Ferdinand,

As my colleague said, you should construct the filter expression manually, so you can take a look at the following help article and see the syntax and coding needed, depending on your grid's datasource:
Operating with the FilterExpression of Telerik RadGrid Manually

Also, you can consider using a RadFilter for filtering if you do not want to build the filter expression manually:
RadGrid filtering with RadFilter

Greetings,
Tsvetina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Ferdinand
Top achievements
Rank 1
answered on 19 Dec 2011, 11:59 AM
Hi Tsvetina,
I tried to recreate the filter expression manually. The expression itself looks fine and should work. But whenever I set the MaterTableView's FilterExpression property I cannot get a correct output. I tried to set it at several points of the page lifecycle: in ItemCommand event, in PageLoad and in Init event but nothing seemed to have any effect. 
Is there any special trick how I can activate a newly set FilterExpression?

Best regards
Ferdinand
0
Tsvetina
Telerik team
answered on 19 Dec 2011, 02:47 PM
Hello Ferdinand,

You can try using the ItemCommand event and on Filter command to cancel it out, set the new FilterExpression and then call Rebind() for the grid control.

Kind regards,
Tsvetina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Ferdinand
Top achievements
Rank 1
answered on 19 Dec 2011, 03:15 PM
Well, I already tried that out but without any success...It seems like the filter expression is completly ignored. First filter works but when trying to apply another one both of them are ignored.
Any other idea?
0
Tsvetina
Telerik team
answered on 22 Dec 2011, 09:24 AM
Hi Ferdinand,

Can you please share the code which you use on ItemCommand? Usually this approach should work, but it depends on the custom logic applied.

Kind regards,
Tsvetina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Sudip
Top achievements
Rank 1
answered on 15 May 2014, 10:46 PM
It is not working. Please help.

please see the logic below

void RadGridItemCommand(object source, GridCommandEventArgs e)
            if (commandName == RadGrid.FilterCommandName)
            {
e.Canceled = true;

                    string filterExpress = ClearFilterExpression(e.Item.OwnerTableView.FilterExpression, gridBoundColumn.UniqueName);
                    BuildFilterExpression(colName, value, gridBoundColumn.DataTypeName, ref filterExpress);
                    e.Item.OwnerTableView.FilterExpression = filterExpress;

                    e.Item.OwnerTableView.OwnerGrid.Rebind();
}
}

private static string ClearFilterExpression(string filterExpression, string columnName)
        {
            filterExpression = filterExpression.Replace("(", string.Empty);
            filterExpression = filterExpression.Replace(")", string.Empty);

            string[] filterExpressionParts = filterExpression.Split(new string[] { " AND " }, StringSplitOptions.None);
            ArrayList list = new ArrayList(filterExpressionParts);
            for (int i = list.Count - 1; i >= 0; i--)
            {
                if (list[i].ToString().StartsWith("[" + columnName +"]"))
                {
                    list.Remove(list[i]);
                }
            }

            filterExpressionParts = (string[])list.ToArray(typeof(string));

            return string.Join(" AND ", filterExpressionParts);
        }

private static void BuildFilterExpression(string field, string value, string dataTypeName, ref string filterExpression)
        {
            string filter = "";


            //http://www.telerik.com/help/aspnet-ajax/grid-operate-with-filter-expression-manually.html
            if (dataTypeName == "System.String")
            {
                //filter = "(iif(" + field + "==null,\"\", " +
                //                 field + ").ToString().Contains(\"" + value +
                //                 "\"))";
                
                filter = "([" + field + "] LIKE '%" + value + "%')";
            }
            else if (dataTypeName.Contains("System.Int"))
            {
                //if (!string.IsNullOrEmpty(field) && !string.IsNullOrEmpty(value))
                //    filter = "(" + field + "==" + value + ")";
                filter = "([" + field + "] = " + value + ")";
            }

            if (filterExpression == "")
            {
                filterExpression = filter;
            }
            else
            {
                filterExpression = "((" + filterExpression + ") AND " + filter + ")";
            }
        }
0
Princy
Top achievements
Rank 1
answered on 16 May 2014, 08:10 AM
Hi Sudip,

I guess you want to have a Common Filter Button so as to Filter all the columns. Please take a look at the sample code snippet.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" DataSourceID="SqlDataSource1"    AllowPaging="true" AllowSorting="true" EnableLinqExpressions="false" AllowFilteringByColumn="true" OnItemCommand="RadGrid1_ItemCommand">
    <MasterTableView CommandItemDisplay="Top">   
        <CommandItemTemplate>
          <asp:Button ID="btnFilter" runat="server" Text="Apply Filter" CommandName="FilterAll" />
          <asp:Button ID="btnClear" runat="server" Text="Clear Filter" CommandName="ClearFilter" OnClientClick="clearFilter" />
        </CommandItemTemplate>
        <Columns>
            <telerik:GridBoundColumn UniqueName="OrderID" DataField="OrderID" HeaderText="OrderID" ShowFilterIcon="false"/>          
            <telerik:GridBoundColumn DataField="ShipCity" HeaderText="ShipCity" UniqueName="ShipCity" ShowFilterIcon="false" />
            <telerik:GridBoundColumn DataField="ShipVia" HeaderText="ShipVia" UniqueName="ShipVia" ShowFilterIcon="false" />
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

JS:
<script type="text/javascript">
    function clearFilter() {
        var grid = $find("<%= RadGrid1%>")
        var tableView = grid.get_masterTableView();
        tableView.get_filterExpressions().clear();
        tableView.rebind()
    }
</script>

C#:
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
    string filterExpression = string.Empty;
    if (e.CommandName == "FilterAll")
    {
        GridFilteringItem filterItem = RadGrid1.MasterTableView.GetItems(GridItemType.FilteringItem)[0] as GridFilteringItem;
        foreach (GridColumn col in RadGrid1.MasterTableView.Columns)
        {
            TextBox txtfilter = (TextBox)filterItem[col.UniqueName].Controls[0];
            string value = txtfilter.Text;
            if (value != "")
            {
                filterExpression += "(" + col.UniqueName + " = '" + value + "')";
                filterExpression += "AND";
                string newfilterString = filterExpression.Remove(filterExpression.Length - 3);
                RadGrid1.MasterTableView.FilterExpression = newfilterString;
                GridColumn column = RadGrid1.MasterTableView.GetColumnSafe(col.UniqueName);
                column.CurrentFilterValue = value;
            }
        }
        RadGrid1.MasterTableView.Rebind();
    }
}

Thanks,
Princy
0
Sudip
Top achievements
Rank 1
answered on 16 May 2014, 04:27 PM
Hi, thanks for your response.

For me, MaintainColumnsViewState=FALSE

The above code while filtering first column, but for multicolumn the sort is not working.
Please suggest, an solution to this problem

Thanks
Sudip


0
Princy
Top achievements
Rank 1
answered on 17 May 2014, 04:02 AM
Hi Sudip,

With the above code my filtering and sorting all works fine. Since I'm unable to replicate the issue, please paste your full code snippet for further help.

Thanks,
Princy
0
Sudip
Top achievements
Rank 1
answered on 19 May 2014, 08:42 PM
Hi Princy,
thanks for your response.

For fitering, do need to use colu's UniqueName of DataField name to build Filter expression?

Thanks
Sudip
0
Princy
Top achievements
Rank 1
answered on 20 May 2014, 03:51 AM
Hi Sudip,

For the FilterExpression we should use DataField value. Since in my example I have set UniqueName same as DataField name, hence its easy to access it in code behind. You can take a look at this article to know more about FilterExpression :
Operating with the FilterExpression of Telerik RadGrid Manually.

Thanks,
Princy
0
Sudip
Top achievements
Rank 1
answered on 20 May 2014, 11:12 PM
Hi Princy,

thanks for your response.

What will happend, if you have same datafield bound to 2 columns? How will it determine what column it should filter?

Regards
Sudip
0
Princy
Top achievements
Rank 1
answered on 21 May 2014, 03:20 AM
Hi Sudip,

When two columns in RadGrid are bound to the same data field the data in them is populated from the same source. This means that when the data in one of the columns is filtered the same result will be shown in both.

If you would like to have different data in the columns you would need to bind them to different fields in the data source.

Thanks,
Princy
0
Sudip
Top achievements
Rank 1
answered on 21 May 2014, 05:10 PM
Hi Princy,

My question was, if one data field is bound to 2 columns, how can i perform filter?

which column (out of 2 columns) the below code will return? 

GridColumn column = RadGrid1.MasterTableView.GetColumnSafe(col.DataField);

-------------------------------------------------------------

protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e){    string filterExpression = string.Empty;    if (e.CommandName == "FilterAll")    {        GridFilteringItem filterItem = RadGrid1.MasterTableView.GetItems(GridItemType.FilteringItem)[0] asGridFilteringItem;        foreach (GridColumn col in RadGrid1.MasterTableView.Columns)        {            TextBox txtfilter = (TextBox)filterItem[col.UniqueName].Controls[0];            string value = txtfilter.Text;            if (value != "")            {                filterExpression += "(" + col.UniqueName + " = '" + value +"')";                filterExpression += "AND";                string newfilterString = filterExpression.Remove(filterExpression.Length - 3);                RadGrid1.MasterTableView.FilterExpression = newfilterString;                GridColumn column = RadGrid1.MasterTableView.GetColumnSafe(col.UniqueName);                column.CurrentFilterValue = value;            }        }        RadGrid1.MasterTableView.Rebind();    }}

0
Princy
Top achievements
Rank 1
answered on 22 May 2014, 03:36 AM
Hi Sudip,

The filtering is done on the datafields available in your datasource. If you have two columns with same datafield, both will be display same result after filtering. From the code I used, I'll explain:

C#:
foreach (GridColumn col in RadGrid1.MasterTableView.Columns)
//This will loop through all the columns
     
filterExpression += "(" + col.UniqueName + " = '" + value + "')";
//here the first column's filter value will be accepted,then second and so on..
 
GridColumn column = RadGrid1.MasterTableView.GetColumnSafe(col.UniqueName);
//gets the first column, then second and so on..           

Hence if you use same DataField, filter will be applied on the first column which enters the loop, and hence same result will be shown in both columns.

Thanks,
Princy
Tags
Grid
Asked by
Ferdinand
Top achievements
Rank 1
Answers by
Tsvetina
Telerik team
Ferdinand
Top achievements
Rank 1
Sudip
Top achievements
Rank 1
Princy
Top achievements
Rank 1
Share this question
or