Eric
Eric

Reputation: 8078

RadGrid loses Filter value

I'm using a RadGrid that i want to allow filtering for. I allow filtering on each individual column but i added a button that I intend to program to APPLY ALL FILTERS.

However, after it applies the filter I lose the filter values in the filter boxes and then it rebinds again on its own and the RadGrid is reset. This all happens when I click the Apply filter that i created in the command item template.

here's some code i'm using.

 protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{


if (e.CommandName == "FilterRadGrid" || e.CommandName == "Filter")
     {
         string expression = "";
         GridFilteringItem item = RadGrid1.MasterTableView.GetItems(GridItemType.FilteringItem)[0] as GridFilteringItem;
         string col1 = (item["col1 "].Controls[0] as TextBox).Text;
         string col2 = (item["col2"].Controls[0] as TextBox).Text;
         string col3= (item["col3"].Controls[0] as TextBox).Text;

             if (col1!= "")
                 expression += "([col1] LIKE \'%" + col1+ "%\')";
             if (col2!= "")
             {
                 if (expression != "")
                     expression += " AND ";
                 expression += "([col2] LIKE \'%" + col2+ "%\')";
             }
             if (col3!= "")
             {
                 if (expression != "")
                     expression += " AND ";
                 expression += "([col3] LIKE \'%" + col3+ "%\')";
             }


         RadGrid1.MasterTableView.FilterExpression = expression;
         RadGrid1.MasterTableView.Rebind();
     } 
}

It does everything it's supposed to do except after the filter the filter boxes are cleared and the Grid binds again for some strange reason and the Grid is no longer filtered. How can I hold the value of the filter boxes (col1,col2,col3)??

Thanks in advance for any help on the subject.

Upvotes: 0

Views: 6351

Answers (1)

thomasvdb
thomasvdb

Reputation: 749

You should set the FilterValue of each column seperately. Not by referencing the controls but by using a property of the GridColumn. You can retrieve your columns by using:

GridBoundColumn clmn = myGrid.Columns.FindByUniqueName( "theUniqueNameOfTheColumn" );

Afterwards you can the set the filter value of the column:

clmn.CurrentFilterValue = "myFilterValue";

This should work fine if you aren't using a FilterTemplate. If you're using a FilterTemplate, there are some extra steps.

Upvotes: 2

Related Questions