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

Custom Filter with Filter options based on visible entries

0 Answers 33 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Peter
Top achievements
Rank 1
Peter asked on 03 Sep 2012, 10:36 AM
hi ,

not sure if this is the right place to post this but i thought it might be useful for people , i implemented the custom Google Style filter , as per the example given , but i have found a way to extend it , so the filter options are based on the current visible results in the grid , i.e if you have allready put another filter in place , it limits the filter results in the other columns based on the results on the previous filters.

here is the code for it.
public class MyGenericFilter : GridBoundColumn
    {
 
        //RadGrid will call this method when it initializes the controls inside the filtering item cells
        protected override void SetupFilterControls(TableCell cell)
        {
            base.SetupFilterControls(cell);
            cell.Controls.RemoveAt(0);
            RadComboBox combo = new RadComboBox();
            combo.ID = ("RadComboBox1" + this.UniqueName);
            combo.ShowToggleImage = false;
            combo.Skin = "Office2007";
            combo.EnableLoadOnDemand = true;
            combo.AllowCustomText = true;
            combo.AutoPostBack = true;
            //combo.MarkFirstMatch = true;
            combo.Height = Unit.Pixel(100);
            combo.Width = Unit.Pixel(100);
            combo.ItemsRequested += this.list_ItemsRequested;
            combo.SelectedIndexChanged += this.list_SelectedIndexChanged;
            combo.DropDownWidth = Unit.Pixel(150);
            cell.Controls.AddAt(0, combo);
            //cell.Controls.RemoveAt(1);
        }
 
        //RadGrid will cal this method when the value should be set to the filtering input control(s)
        protected override void SetCurrentFilterValueToControl(TableCell cell)
        {
            base.SetCurrentFilterValueToControl(cell);
            RadComboBox combo = (RadComboBox)cell.Controls[0];
            if ((this.CurrentFilterValue != string.Empty))
            {
                combo.Text = this.CurrentFilterValue;
            }
        }
 
        //RadGrid will cal this method when the filtering value should be extracted from the filtering input control(s)
        protected override string GetCurrentFilterValueFromControl(TableCell cell)
        {
            RadComboBox combo = (RadComboBox)cell.Controls[0];
            return combo.Text;
        }
 
        private void list_ItemsRequested(object o, RadComboBoxItemsRequestedEventArgs e)
        {
            ((RadComboBox)o).DataTextField = this.DataField;
            ((RadComboBox)o).DataValueField = this.DataField;
 
            RadGrid MyGrid = ((RadGrid)((RadComboBox)o).Parent.Parent.Parent.Parent.Parent.Parent);
 
            MyGrid.AllowPaging = false;
            MyGrid.Rebind();
 
            DataTable NewDT = new DataTable(this.DataField);
            NewDT.Columns.Add(new DataColumn(this.DataField));
 
            foreach (GridDataItem GD in MyGrid.Items)
            {
                if (GD[this.DataField].Text.Contains(e.Text) && GD[this.DataField].Text != " ")
                {
                    var foundAuthors = NewDT.Select(this.DataField + " = '" + GD[this.DataField].Text + "'");
                    if (foundAuthors.Length == 0)
                    {
                        DataRow DR = NewDT.NewRow();
                        DR[this.DataField] = GD[this.DataField].Text;
 
                        NewDT.Rows.Add(DR);
                    }
                }
            }
 
            MyGrid.AllowPaging = true;
            MyGrid.Rebind();
 
 
            NewDT.DefaultView.Sort = this.DataField + " ASC";
 
            ((RadComboBox)o).DataSource = NewDT;
            ((RadComboBox)o).DataBind();
        }
 
        private void list_SelectedIndexChanged(object o, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            GridFilteringItem filterItem = (GridFilteringItem)((RadComboBox)o).NamingContainer;
            if ((this.UniqueName == "Index"))
            {
                //this is filtering for integer column type
                filterItem.FireCommandEvent("Filter", new Pair("EqualTo", this.UniqueName));
            }
            //filtering for string column type
            filterItem.FireCommandEvent("Filter", new Pair("Contains", this.UniqueName));
        }
    }

No answers yet. Maybe you can help?

Tags
Grid
Asked by
Peter
Top achievements
Rank 1
Share this question
or