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

Clearing existing filter expression

3 Answers 77 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Hiren
Top achievements
Rank 1
Hiren asked on 30 Aug 2011, 02:44 PM
how to clear existing filter expression when i filter another column.

in 1st column i had applied filter then when i tried to filter 2nd column it should clear 1st column filter and need to apply 2nd column filter

how to clear that

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 1
answered on 30 Aug 2011, 03:12 PM
Hello Giri,

Try the following  code snippet.
C#:
protected void Page_Load(object sender, EventArgs e)
 {
     RadGrid1.MasterTableView.FilterExpression = string.Empty;
     foreach (GridColumn column in RadGrid1.MasterTableView.RenderColumns)
       {
         if (column.SupportsFiltering())
          {
             column.CurrentFilterValue = string.Empty;
             column.CurrentFilterFunction = GridKnownFunction.NoFilter;
          }
       }
     RadGrid1.MasterTableView.Rebind();
}

Thanks,
Shinu.
0
Hiren
Top achievements
Rank 1
answered on 31 Aug 2011, 06:00 AM
I am getting this error

Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidOperationException: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.

Source Error:

Line 39:                         </FilterTemplate>
Line 40:                         <ItemTemplate>
Line 41: <%# Eval("LastName")%>Line 42:                         </ItemTemplate>
Line 43:                     </telerik:GridTemplateColumn>

ASPX Page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GoogleFilteringSample.aspx.cs"
    Inherits="GoogleFilteringSample" %>
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <telerik:RadGrid runat="server" EnableLinqExpressions="false" AllowFilteringByColumn="true"
            ID="RgdUserControl" GridLines="None" OnNeedDataSource="RadGridUserControl_NeedDataSource">
            <MasterTableView AutoGenerateColumns="False" DataKeyNames="EmployeeID" AdditionalDataFieldNames="LastName,FirstName">
                <RowIndicatorColumn>
                    <HeaderStyle Width="20px"></HeaderStyle>
                </RowIndicatorColumn>
                <ExpandCollapseColumn>
                    <HeaderStyle Width="20px"></HeaderStyle>
                </ExpandCollapseColumn>
                <Columns>
                    <telerik:GridBoundColumn DataField="EmployeeID" DataType="System.Int32" HeaderText="EmployeeID"
                        ReadOnly="True" SortExpression="EmployeeID" UniqueName="EmployeeID" AllowFiltering="false">
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="Title" HeaderText="Title" SortExpression="Title"
                        UniqueName="Title">
                        <FilterTemplate>
                            <telerik:RadComboBox runat="server" ID="RcbTitle" AutoPostBack="true" ShowToggleImage="false"
                                MarkFirstMatch="true" OnItemsRequested="RcbTitle_ItemsRequested" OnClientSelectedIndexChanged="onClientSelectedIndexChangedTitle">
                            </telerik:RadComboBox>
                        </FilterTemplate>
                    </telerik:GridBoundColumn>
                    <telerik:GridTemplateColumn DataField="LastName" HeaderText="LastName" SortExpression="LastName"
                        UniqueName="LastName">
                        <FilterTemplate>
                            <telerik:RadComboBox ID="RcbLastName" EnableLoadOnDemand="true" runat="server" OnItemsRequested="RcbLastName_ItemsRequested"
                                ShowToggleImage="false" MarkFirstMatch="true" OnClientSelectedIndexChanged="onClientSelectedIndexChangedLN">
                            </telerik:RadComboBox>
                        </FilterTemplate>
                        <ItemTemplate>
                            <%# Eval("LastName")%>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridTemplateColumn DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName"
                        UniqueName="FirstName">
                        <FilterTemplate>
                            <telerik:RadComboBox ID="RcbFirstName" EnableLoadOnDemand="true" OnClientSelectedIndexChanged="onClientSelectedIndexChangedFN"
                                runat="server" OnItemsRequested="RcbFirstName_ItemsRequested" ShowToggleImage="false"
                                MarkFirstMatch="true">
                            </telerik:RadComboBox>
                        </FilterTemplate>
                        <ItemTemplate>
                            <%# Eval("FirstName")%>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridTemplateColumn DataField="TitleOfCourtesy" HeaderText="TitleOfCourtesy"
                        SortExpression="TitleOfCourtesy" UniqueName="TitleOfCourtesy">
                        <FilterTemplate>
                            <telerik:RadComboBox ID="RcbTitleOfCourtesy" EnableLoadOnDemand="true" OnClientSelectedIndexChanged="onClientSelectedIndexChangedTitleOfCourtesy"
                                runat="server" OnItemsRequested="RcbTitleOfCourtesy_ItemsRequested" ShowToggleImage="false"
                                MarkFirstMatch="true">
                            </telerik:RadComboBox>
                        </FilterTemplate>
                        <ItemTemplate>
                            <%# Eval("TitleOfCourtesy")%>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridTemplateColumn DataField="BirthDate" HeaderText="BirthDate" SortExpression="BirthDate"
                        UniqueName="BirthDate" DataType="System.DateTime">
                        <FilterTemplate>
                            <telerik:RadComboBox ID="RcbBirthDate" EnableLoadOnDemand="true" OnClientSelectedIndexChanged="onClientSelectedIndexChangedBirthDate"
                                runat="server" OnItemsRequested="RcbBirthDate_ItemsRequested" ShowToggleImage="false"
                                MarkFirstMatch="true">
                            </telerik:RadComboBox>
                        </FilterTemplate>
                        <ItemTemplate>
                            <%# Eval("BirthDate")%>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridTemplateColumn DataField="HireDate" HeaderText="HireDate" SortExpression="HireDate"
                        UniqueName="HireDate" DataType="System.DateTime">
                        <FilterTemplate>
                            <telerik:RadComboBox ID="RcbHireDate" EnableLoadOnDemand="true" OnClientSelectedIndexChanged="onClientSelectedIndexChangedHireDate"
                                runat="server" OnItemsRequested="RcbHireDate_ItemsRequested" ShowToggleImage="false"
                                MarkFirstMatch="true">
                            </telerik:RadComboBox>
                        </FilterTemplate>
                        <ItemTemplate>
                            <%# Eval("BirthDate")%>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridTemplateColumn DataField="Address" HeaderText="Address" SortExpression="Address"
                        UniqueName="Address">
                        <FilterTemplate>
                            <telerik:RadComboBox ID="RcbAddress" EnableLoadOnDemand="true" OnClientSelectedIndexChanged="onClientSelectedIndexChangedAddress"
                                runat="server" OnItemsRequested="RcbAddress_ItemsRequested" ShowToggleImage="false"
                                MarkFirstMatch="true">
                            </telerik:RadComboBox>
                        </FilterTemplate>
                        <ItemTemplate>
                            <%# Eval("Address")%>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                    <telerik:GridTemplateColumn DataField="City" HeaderText="City" SortExpression="City"
                        UniqueName="City">
                        <FilterTemplate>
                            <telerik:RadComboBox ID="RcbCity" EnableLoadOnDemand="true" runat="server" OnItemsRequested="RcbCity_ItemsRequested"
                                ShowToggleImage="false" MarkFirstMatch="true" OnClientSelectedIndexChanged="onClientSelectedIndexChangedCity">
                            </telerik:RadComboBox>
                        </FilterTemplate>
                        <ItemTemplate>
                            <%# Eval("City")%>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid><br />
        <asp:Button ID="clrFilters" runat="server" Text="Clear Filter" OnClick="clrFilters_Click" />
        <telerik:RadCodeBlock ID="RadCodeBlock1" runat="server">
  
            <script type="text/javascript">
            function onClientSelectedIndexChangedCity(sender, eventArgs) {
                var value = eventArgs.get_item().get_value();
                $find("<%= RgdUserControl.ClientID %>").get_masterTableView().filter("City", eventArgs.get_item().get_text(), "StartsWith");
            }
            function onClientSelectedIndexChangedLN(sender, eventArgs) {
                var value = eventArgs.get_item().get_value();
                $find("<%= RgdUserControl.ClientID %>").get_masterTableView().filter("LastName", eventArgs.get_item().get_text(), "StartsWith");
            }
            function onClientSelectedIndexChangedFN(sender, eventArgs) {
                var value = eventArgs.get_item().get_value();
                $find("<%= RgdUserControl.ClientID %>").get_masterTableView().filter("FirstName", eventArgs.get_item().get_text(), "StartsWith");
            }
            function onClientSelectedIndexChangedTitle(sender, eventArgs) {
                var value = eventArgs.get_item().get_value();
                $find("<%= RgdUserControl.ClientID %>").get_masterTableView().filter("Title", eventArgs.get_item().get_text(), "StartsWith");
            }
            function onClientSelectedIndexChangedTitleOfCourtesy(sender, eventArgs) {
                var value = eventArgs.get_item().get_value();
                $find("<%= RgdUserControl.ClientID %>").get_masterTableView().filter("TitleOfCourtesy", eventArgs.get_item().get_text(), "StartsWith");
            }
            function onClientSelectedIndexChangedBirthDate(sender, eventArgs) {
                var value = eventArgs.get_item().get_value();
                $find("<%= RgdUserControl.ClientID %>").get_masterTableView().filter("BirthDate", eventArgs.get_item().get_text(), "StartsWith");
            }
            function onClientSelectedIndexChangedHireDate(sender, eventArgs) {
                var value = eventArgs.get_item().get_value();
                $find("<%= RgdUserControl.ClientID %>").get_masterTableView().filter("HireDate", eventArgs.get_item().get_text(), "StartsWith");
            }
            function onClientSelectedIndexChangedAddress(sender, eventArgs) {
                var value = eventArgs.get_item().get_value();
                $find("<%= RgdUserControl.ClientID %>").get_masterTableView().filter("Address", eventArgs.get_item().get_text(), "StartsWith");
            
            </script>
  
        </telerik:RadCodeBlock>
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
    </div>
    </form>
</body>
</html>
ASPX.CS
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
  
public partial class GoogleFilteringSample : System.Web.UI.Page
{
    DataTable dt = null;
  
    protected void Page_Load(object sender, EventArgs e)
    {
        RgdUserControl.MasterTableView.FilterExpression = string.Empty;
        foreach (GridColumn column in RgdUserControl.MasterTableView.RenderColumns)
        {
            if (column.SupportsFiltering())
            {
                column.CurrentFilterValue = string.Empty;
                column.CurrentFilterFunction = GridKnownFunction.NoFilter;
            }
        }
        RgdUserControl.MasterTableView.Rebind();
    }
  
    protected void RcbTitle_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
    {
        (o as RadComboBox).DataTextField = "Title";
        (o as RadComboBox).DataTextField = "Title";
        (o as RadComboBox).DataSource = GetDataTable("Title", e.Text);
        (o as RadComboBox).DataBind();
    }
  
    protected void clrFilters_Click(object sender, System.EventArgs e)
    {
        ClearFilter();
    }
  
    private void ClearFilter()
    {
        foreach (GridColumn column in RgdUserControl.MasterTableView.Columns)
        {
            column.CurrentFilterValue = string.Empty;
            column.CurrentFilterFunction = GridKnownFunction.NoFilter;
        }
        RgdUserControl.MasterTableView.FilterExpression = string.Empty;
        RgdUserControl.MasterTableView.Rebind();
    }
  
    protected void RcbCity_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
    {
        (o as RadComboBox).DataTextField = "City";
        (o as RadComboBox).DataTextField = "City";
        (o as RadComboBox).DataSource = GetDataTable("City", e.Text);
        (o as RadComboBox).DataBind();
    }
  
    protected void RcbLastName_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
    {
        (o as RadComboBox).DataTextField = "LastName";
        (o as RadComboBox).DataTextField = "LastName";
        (o as RadComboBox).DataSource = GetDataTable("LastName", e.Text);
        (o as RadComboBox).DataBind();
    }
  
    protected void RcbFirstName_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
    {
  
        (o as RadComboBox).DataTextField = "FirstName";
        (o as RadComboBox).DataTextField = "FirstName";
        (o as RadComboBox).DataSource = GetDataTable("FirstName", e.Text);
        (o as RadComboBox).DataBind();
    }
  
  
    protected void RcbTitleOfCourtesy_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
    {
  
        (o as RadComboBox).DataTextField = "TitleOfCourtesy";
        (o as RadComboBox).DataTextField = "TitleOfCourtesy";
        (o as RadComboBox).DataSource = GetDataTable("TitleOfCourtesy", e.Text);
        (o as RadComboBox).DataBind();
    }
  
    protected void RcbBirthDate_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
    {
        (o as RadComboBox).DataTextField = "BirthDate";
        (o as RadComboBox).DataTextField = "BirthDate";
        (o as RadComboBox).DataSource = GetDataTable("BirthDate", e.Text);
        (o as RadComboBox).DataBind();
    }
  
    protected void RcbHireDate_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
    {
        (o as RadComboBox).DataTextField = "HireDate";
        (o as RadComboBox).DataTextField = "HireDate";
        (o as RadComboBox).DataSource = GetDataTable("HireDate", e.Text);
        (o as RadComboBox).DataBind();
    }
  
    protected void RcbAddress_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
    {
        (o as RadComboBox).DataTextField = "Address";
        (o as RadComboBox).DataTextField = "Address";
        (o as RadComboBox).DataSource = GetDataTable("Address", e.Text);
        (o as RadComboBox).DataBind();
    }
  
    public DataTable GetDataTable(string columnName, string searchString)
    {
        String myQuery = string.Empty;
  
        if (!columnName.Equals("All"))
            myQuery = "SELECT DISTINCT " + columnName + " FROM Employees WHERE " + columnName + " LIKE '" + searchString + "%'";
        else
            myQuery = "SELECT [EmployeeID], [LastName], [FirstName], [Title], [TitleOfCourtesy], [BirthDate], [HireDate], [Address], [City] FROM [Employees]";
  
        String ConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(ConnString);
        SqlDataAdapter adapter = new SqlDataAdapter();
        adapter.SelectCommand = new SqlCommand(myQuery, conn);
  
        DataTable myDataTable = new DataTable();
  
        conn.Open();
        try
        {
            adapter.Fill(myDataTable);
        }
        finally
        {
            conn.Close();
        }
        return myDataTable;
    }
    protected void RadGridUserControl_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
    {
        dt = GetDataTable("All", string.Empty);
        RgdUserControl.DataSource = dt;
    }
}
0
Marin
Telerik team
answered on 02 Sep 2011, 08:25 AM
Hi Giri,

 You should add the databinding expressions to a controls like label or literal:

<ItemTemplate>
  <asp:Label ID="Label1" Text='<%# Eval("OrderID") %>' runat="server"  </ItemTemplate>

Regards,
Marin
the Telerik team

Thank you for being the most amazing .NET community! Your unfailing support is what helps us charge forward! We'd appreciate your vote for Telerik in this year's DevProConnections Awards. We are competing in mind-blowing 20 categories and every vote counts! VOTE for Telerik NOW >>

Tags
Grid
Asked by
Hiren
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 1
Hiren
Top achievements
Rank 1
Marin
Telerik team
Share this question
or