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

Default filter on initial Load with EnableLinqExpressions=true

9 Answers 161 Views
Grid
This is a migrated thread and some comments may be shown as answers.
ck
Top achievements
Rank 1
ck asked on 18 Oct 2013, 01:55 PM
I am trying to setup a default EqualTo filter on one of the string columns of a RadGrid on initial load. TI am binding the grid using the NeedDataSource event in the code behind.

The column i am trying to filter with is below
<telerik:GridBoundColumn DataField="Data.SubCategoryName" DataType="System.String" UniqueName="SubCategoryName" HeaderText="IMDIS Category" ReadOnly="true" ItemStyle-Width="250" HeaderStyle-HorizontalAlign="Center">
                    <FilterTemplate>
                        <telerik:RadComboBox ID="RC1" runat="server" DataValueField="SubCategoryName" AppendDataBoundItems="true" DataTextField="SubCategoryName" SelectedValue='<%# ((GridItem)Container).OwnerTableView.GetColumn("SubCategoryName").CurrentFilterValue %>'
                            OnClientSelectedIndexChanged="categoryIndexChanged" DataSourceID="EntityDataSource1" Width="310">
                            <Items>
                                <telerik:RadComboBoxItem Text="All" />
                            </Items>
                        </telerik:RadComboBox>
                        <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
                            <script type="text/javascript">
                                function categoryIndexChanged(sender, args) {
                                    var tableView = $find("<%# ((GridItem)Container).OwnerTableView.ClientID %>");
                                    tableView.filter("SubCategoryName", args.get_item().get_value(), "EqualTo");
                                }
                            </script>
                        </telerik:RadScriptBlock>
                    </FilterTemplate>
                </telerik:GridBoundColumn>

in my code behind,
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            if (!Page.IsPostBack && InitialFilter != null)
            {
                RadGrid1.MasterTableView.FilterExpression = "([" + InitialFilter.ColumnName + "])=\"" + InitialFilter.Value + "\") ";
                GridColumn col = RadGrid1.MasterTableView.GetColumnSafe(InitialFilter.ColumnName);
                col.CurrentFilterFunction = InitialFilter.Operator;
                col.CurrentFilterValue = InitialFilter.Value;
            }
            RadGrid1.DataSource = DataSource;
        }
 
public OnLoadFilter InitialFilter { get; set; }
 
        public class OnLoadFilter
        {
            public string ColumnName { get; set; }
            public GridKnownFunction Operator { get; set; }
            public string Value { get; set; }
        }

InitialFilter is a property set in the Page_Load event, it's an object containing the three properties used to filter.
I am following the example from http://www.telerik.com/help/aspnet-ajax/grid-applying-default-filter-on-initial-load.html but i keep getting this  a ParseException saying Expression expected.
If i set EnableLinqExpressions to false, it renders but filtering does not work.
following instructions from http://www.telerik.com/help/aspnet-ajax/grid-operate-with-filter-expression-manually.html, I tried to statically set the filter 
RadGrid1.MasterTableView.FilterExpression = "([SubCategoryName]=\"Cars\") ";

again to luck.

I am using .Net 4.0 with the the 2013.2.717.40 version of the telerik controls for asp.net ajax.
 can someone advise?
thanks

9 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 21 Oct 2013, 05:37 AM
Hi ,

Please try the sample code snippet,which depicts your required scenario.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false" AllowPaging="true"
    OnNeedDataSource="RadGrid1_NeedDataSource" AllowFilteringByColumn="true" EnableLinqExpressions="true"
    OnPreRender="RadGrid1_PreRender">
    <MasterTableView>
        <Columns>
            <telerik:GridBoundColumn UniqueName="OrderID" DataField="OrderID" HeaderText="OrderID">
            </telerik:GridBoundColumn>       
            <telerik:GridBoundColumn DataField="ShipCountry" UniqueName="ShipCountry" HeaderText="ShipCountry">
                <FilterTemplate>
                    <telerik:RadComboBox ID="RC1" runat="server" DataValueField="ShipCountry" AppendDataBoundItems="true"
                        DataTextField="ShipCountry" SelectedValue='<%# ((GridItem)Container).OwnerTableView.GetColumn("ShipCountry").CurrentFilterValue %>'                        OnClientSelectedIndexChanged="categoryIndexChanged" DataSourceID="SqlDataSource2">
                        <Items>
                            <telerik:RadComboBoxItem Text="All" />
                        </Items>
                    </telerik:RadComboBox>
                    <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
                        <script type="text/javascript">
                            function categoryIndexChanged(sender, args) {
                                var tableView = $find("<%# ((GridItem)Container).OwnerTableView.ClientID %>");
                                tableView.filter("ShipCountry", args.get_item().get_value(), "EqualTo");
                            }
                        </script>
                    </telerik:RadScriptBlock>
                </FilterTemplate>
            </telerik:GridBoundColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

C#:
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    String ConnString = ConfigurationManager.ConnectionStrings["Northwind_newConnectionString3"].ConnectionString;
    SqlConnection conn = new SqlConnection(ConnString);
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.SelectCommand = new SqlCommand("SELECT * FROM Orders", conn);
 
    DataTable myDataTable = new DataTable();
 
    conn.Open();
    try
    {
        adapter.Fill(myDataTable);
    }
    finally
    {
        conn.Close();
    }
 
    RadGrid1.DataSource = myDataTable;
 
    if (!Page.IsPostBack)
    {
        RadGrid1.MasterTableView.FilterExpression = "(Convert.ToString(it[\"ShipCountry\"]) = \"Spain\") ";
        GridColumn column = RadGrid1.MasterTableView.GetColumnSafe("ShipCountry");
        column.CurrentFilterFunction = GridKnownFunction.EqualTo;
        column.CurrentFilterValue = "Spain";
    }
}

Thanks,
Princy
0
ck
Top achievements
Rank 1
answered on 30 Oct 2013, 02:14 PM
Hi Princy,

thanks for the reply but it does not work.

Now i get.

No applicable indexer exists in type 'MasterEntry'


why is this so complicated?


0
Princy
Top achievements
Rank 2
answered on 31 Oct 2013, 06:10 AM
Hi ,

Please set EnableLinqExpressions="false" and modify the code as below.

C#:
protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
   // Code to populate RadGrid
    if (!Page.IsPostBack)
    {
        RadGrid1.MasterTableView.FilterExpression = "(ShipCountry = 'Spain')";    
        GridColumn column = RadGrid1.MasterTableView.GetColumnSafe("ShipCountry");
        column.CurrentFilterFunction = GridKnownFunction.EqualTo;
        column.CurrentFilterValue = "Spain";    
    }       
}

Hope this helps. Let me know if any concerns.
Thanks,
Princy
0
ck
Top achievements
Rank 1
answered on 04 Nov 2013, 03:32 PM
I will but what is the impact of setting EnableLinqExpressions to false?

does this mean that i won't be able to use Linq as a Datasource?
0
Princy
Top achievements
Rank 2
answered on 05 Nov 2013, 06:06 AM
Hi,

Setting EnableLinqExpressions to false is required for filtering grid which is bound to a DataTable. The default value of this property is true, so you would not have to set it if you use LinqDataSource for example. When the LINQ expressions of the RadGrid are enabled, the performance is significantly improved because the grid uses dynamic LINQ expressions for all data operations. However, this leads to some limitations and that is why the EnableLinqExpressions property is provided. It is fine to bind to a DataTable and not set EnableLinqExpressions="false" as long as you do not perform manual filtering that requires T-SQL syntax.

Thanks,
Princy


0
ck
Top achievements
Rank 1
answered on 05 Nov 2013, 10:05 AM
still does not work... no exception but no filtering
0
Princy
Top achievements
Rank 2
answered on 06 Nov 2013, 08:39 AM
Hi ,

I'm not able to replicate the issue, the code works fine at my end. Please provide your full code snippet so as to help you with the issues.

Thanks,
Princy
0
ck
Top achievements
Rank 1
answered on 06 Nov 2013, 09:27 AM
<telerik:RadGrid ID="RadGrid1" runat="server" OnItemDataBound="RadGrid1_ItemDataBound" OnNeedDataSource="RadGrid1_NeedDataSource" ItemStyle-CssClass="info-row"
        AlternatingItemStyle-CssClass="alternating-info-row" AllowSorting="true" OnItemEvent="RadGrid1_ItemEvent" EnableLinqExpressions="false">
        <GroupingSettings CaseSensitive="false" />
        <MasterTableView AutoGenerateColumns="False" CommandItemDisplay="None" AllowPaging="true" PageSize="30" AllowSorting="true" AllowFilteringByColumn="true"
            CurrentResetPageIndexAction="SetPageIndexToFirst" DataKeyNames="Id" Frame="Border" TableLayout="Auto" AlternatingItemStyle-CssClass="info-row" AllowAutomaticInserts="false" AllowAutomaticDeletes="false">
            <Columns>
 
                <telerik:GridTemplateColumn HeaderText="" ReadOnly="true" ItemStyle-Width="5" HeaderStyle-HorizontalAlign="Center" AllowFiltering="false">
                    <ItemStyle CssClass="no-border" HorizontalAlign="Center" />
                    <HeaderTemplate>#</HeaderTemplate>
                    <ItemTemplate>
                        <%# Container.ItemIndex + 1 %>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridTemplateColumn HeaderText="Title" ReadOnly="true" ItemStyle-Width="410" HeaderStyle-HorizontalAlign="Center" AllowFiltering="true" DataField="Title" DataType="System.String" AutoPostBackOnFilter="true" FilterControlWidth="430" SortExpression="Title">
                    <ItemStyle CssClass="no-border" />
                    <ItemTemplate>
                        <asp:Label ID="lblTitle" runat="server" Text='<%# Eval("Title") %>'></asp:Label>
                    </ItemTemplate>
                </telerik:GridTemplateColumn>
                <telerik:GridBoundColumn DataField="LanguageShort" DataType="System.String" HeaderText="Language(s)" ReadOnly="true" ItemStyle-Width="20" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center" AllowFiltering="false"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="FormatShort" AllowFiltering="false" DataType="System.String" HeaderText="Format(s)" ReadOnly="true" ItemStyle-Width="20" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Center"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="PublicationSubCategory.CategoryPeriodicity.Name" DataType="System.String" HeaderText="Category Periodicity" ReadOnly="true" ItemStyle-Width="130" HeaderStyle-HorizontalAlign="Center" FilterControlWidth="130"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="PublicationSubCategory.SubCategoryName" DataType="System.String" UniqueName="SubCategoryName" HeaderText="Category" ReadOnly="true" ItemStyle-Width="250" HeaderStyle-HorizontalAlign="Center">
                    <FilterTemplate>
                        <telerik:RadComboBox ID="RC1" runat="server" DataValueField="SubCategoryName" AppendDataBoundItems="true" DataTextField="SubCategoryName" SelectedValue='<%# ((GridItem)Container).OwnerTableView.GetColumn("SubCategoryName").CurrentFilterValue %>'
                            OnClientSelectedIndexChanged="categoryIndexChanged" DataSourceID="EntityDataSource1" Width="310">
                            <Items>
                                <telerik:RadComboBoxItem Text="All" />
                            </Items>
                        </telerik:RadComboBox>
                        <telerik:RadScriptBlock ID="RadScriptBlock1" runat="server">
                            <script type="text/javascript">
                                function categoryIndexChanged(sender, args) {
                                    var tableView = $find("<%# ((GridItem)Container).OwnerTableView.ClientID %>");
                                    tableView.filter("SubCategoryName", args.get_item().get_value(), "EqualTo");
                                }
                            </script>
                        </telerik:RadScriptBlock>
                    </FilterTemplate>
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="FullSystemId" DataType="System.String" HeaderText="System Assigned ID" ReadOnly="true" ItemStyle-Width="60" HeaderStyle-HorizontalAlign="Center" ItemStyle-HorizontalAlign="Right" AllowFiltering="true" FilterControlWidth="60" AutoPostBackOnFilter="true"></telerik:GridBoundColumn>
            </Columns>
 
        </MasterTableView>
    </telerik:RadGrid>
</div>
<asp:EntityDataSource ID="EntityDataSource1" runat="server" ConnectionString="name=PublicationsContainer" DefaultContainerName="PublicationsContainer"
    CommandText="select distinct s.[SubCategoryName] from PublicationsContainer.PublicationSubCategories as s">

and the code behind

protected void RadGrid1_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            RadGrid1.DataSource = DataSource;
            if (!Page.IsPostBack && InitialFilter != null)
            {
               RadGrid1.MasterTableView.FilterExpression = "("+ InitialFilter.ColumnName + " = '" + InitialFilter.Value + "')";
                GridColumn col = RadGrid1.MasterTableView.GetColumnSafe(InitialFilter.ColumnName);
                col.CurrentFilterFunction = InitialFilter.Operator;
                col.CurrentFilterValue = InitialFilter.Value;
            }
        }
 
 
        public OnLoadFilter InitialFilter { get; set; }
 
        public class OnLoadFilter
        {
            public string ColumnName { get; set; }
            public GridKnownFunction Operator { get; set; }
            public string Value { get; set; }
        }

The markup and code above is for a user control where the grid is hosted. the control's OnLoadFilter is set in the Page_Load of the page where it is hosted.

string filterOnLoad = Request["category"];
 if (!IsPostBack && !string.IsNullOrEmpty(filterOnLoad))
            {
                EditionsListControl.InitialFilter = new Controls.EditionsListControl.OnLoadFilter { ColumnName = "SubCategoryName", Operator = GridKnownFunction.EqualTo, Value = filterOnLoad };
            }
0
Princy
Top achievements
Rank 2
answered on 07 Nov 2013, 04:46 AM
Hi ,

I have tried your code and it works fine at my end, can you please put breakpoints in your code and see if the values for filtering such as ColumnName, Operator and Value are being passed properly and obtained at the UserControl code.

Thanks,
Princy
Tags
Grid
Asked by
ck
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
ck
Top achievements
Rank 1
Share this question
or