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

Custom RadGrid Filter not working nicely with other filters

1 Answer 55 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Content and Code
Top achievements
Rank 1
Content and Code asked on 29 Nov 2010, 10:37 AM
Hi,

I have followed this example:

http://www.telerik.com/help/aspnet-ajax/grdfilteringwithdropdownlist.html

and created a custom filter. Code is below:

public class StageFilterColumn : GridBoundColumn
    {       
        public StageFilterColumn ()
        {
           
        }

        protected override void SetupFilterControls(TableCell cell)
        {
            base.SetupFilterControls(cell);
            cell.Controls.RemoveAt(0);
            DropDownList list = new DropDownList();
            list.ID = "list" + this.DataField;
            list.AutoPostBack = true;
            list.SelectedIndexChanged += new EventHandler(list_SelectedIndexChanged);
            cell.Controls.AddAt(0, list);
            cell.Controls.RemoveAt(1);
            List<string> stagesStringList = GetDistinctStages();

            ListItem li0 = new ListItem("All Stages", "-1");
            list.Items.Add(li0);

            foreach (string stage in stagesStringList)
            {
                ListItem listItem = new ListItem(stage, stage);
                list.Items.Add(listItem);
            }
        }

        void list_SelectedIndexChanged(object sender, EventArgs e)
        {
            DropDownList dl = (DropDownList)sender;
            GridFilteringItem filterItem = dl.NamingContainer as GridFilteringItem;
            if (dl.SelectedItem.Value != "-1")
                filterItem.FireCommandEvent("Filter", new Pair("EqualTo", this.UniqueName));
            else
                filterItem.FireCommandEvent("Filter", new Pair("NoFilter", this.UniqueName));
        }

        //RadGrid calls this method when the value should be set to the filtering input control(s)
        protected override void SetCurrentFilterValueToControl(TableCell cell)
        {
            base.SetCurrentFilterValueToControl(cell);
            DropDownList list = (DropDownList)cell.Controls[0];
            if (this.CurrentFilterValue != string.Empty)
            {
                list.SelectedValue = this.CurrentFilterValue;
            }
        }

        //RadGrid calls this method to extract the filtering value from the filtering input control(s)
        protected override string GetCurrentFilterValueFromControl(TableCell cell)
        {
            DropDownList list = (DropDownList)cell.Controls[0];
            return list.SelectedValue;
        }
        protected override string GetFilterDataField()
        {
            return this.DataField;
        }

    }

I am also filtering on other columns where the user types something and then tabs away from the text box and it does an auto postback to filter the data but it returns no data at all. If I filter by my custom column then it is fine. If I filter by my custom column and then filter by any other column it also works as expected. 

When I filter by any other column the first time (this is when no data is returned when I know it should be) I looked at the filter expression and basically it is something like this (if (column1Filter == "criteriaIEntered" && customFilterColumn = "-1"). I believe the second bit customFilterColumn = "-1" causes the problem even though I am not filtering by this.

1 Answer, 1 is accepted

Sort by
0
Content and Code
Top achievements
Rank 1
answered on 29 Nov 2010, 12:49 PM
I managed to fix this issue by making the following change:

        protected override string GetCurrentFilterValueFromControl(TableCell cell)
        {
            DropDownList list = (DropDownList)cell.Controls[0];
            if (list.SelectedItem.Value.Equals("-1"))
            {
                return string.Empty;
            }
            return list.SelectedValue;
        }
Tags
Grid
Asked by
Content and Code
Top achievements
Rank 1
Answers by
Content and Code
Top achievements
Rank 1
Share this question
or