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

Common filter/search for all columns in grid.

1 Answer 648 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Prasad
Top achievements
Rank 1
Prasad asked on 02 Jun 2014, 06:56 AM
I am trying to implement single search/filter for all the columns in the grid (see attached image). I have customized filter for column, using filter template. Below are my questions, will appreciate your help/ suggestions.
1) I wanted the custom filter above column headers and also it should not be attached to one column. In the image, you can see the column expands to the width of custom filter. Can we implement this in radgrid?

2) Do telerik radgrid has any out of the box functionality similar to the mentioned requirement?

Thanks.

1 Answer, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 03 Jun 2014, 07:30 AM
Hi Prasad,

You can use the CommandItemTemplate to have a common filter textbox and apply filter. Take a look at the sample code snippet.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server OnItemCommand="RadGrid1_ItemCommand">
 <MasterTableView CommandItemDisplay="Top">
   <CommandItemTemplate>
    <asp:TextBox ID="txtFilter" runat="server"></asp:TextBox>
    <asp:Button ID="btnFilter" runat="server" Text="Apply Filter" CommandName="FilterAll" />
     <asp:Button ID="btnClear" runat="server" Text="Clear Filter" CommandName="ClearFilter" />
   </CommandItemTemplate>   
 </MasterTableView>
</telerik:RadGrid>

C#:
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{     
    string filterExpression = string.Empty;
    if (e.CommandName == "FilterAll")
    {
        if (e.Item is GridCommandItem)
        {
            GridCommandItem cmdItem = (GridCommandItem)e.Item;
            TextBox txtFilter = (TextBox)cmdItem.FindControl("txtFilter");
            foreach (GridColumn col in RadGrid1.MasterTableView.Columns)
            {
                foreach(GridDataItem dataItem in RadGrid1.Items)
                {
                    // make sure you have UniqueName of a column same as its DataField name
                    if (dataItem[col.UniqueName].Text == txtFilter.Text)
                    {
                        filterExpression = "([" + col.UniqueName + "] = '" + txtFilter.Text + "')";                        
                        RadGrid1.MasterTableView.FilterExpression = filterExpression;                          
                    }                       
                }                   
            }
            RadGrid1.MasterTableView.Rebind();
        }           
    }
    if (e.CommandName == "ClearFilter")
    {
        if (e.Item is GridCommandItem)
        {
            GridCommandItem cmdItem = (GridCommandItem)e.Item;
            TextBox txtFilter = (TextBox)cmdItem.FindControl("txtFilter");
            txtFilter.Text = string.Empty;
            RadGrid1.MasterTableView.FilterExpression = string.Empty;
        }
    }
}

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