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

Persist grid filter expression

2 Answers 109 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Avinash
Top achievements
Rank 1
Avinash asked on 02 Feb 2011, 09:10 PM
Hi,

I am looking into couple of forums which talks about filter setting, filter expressions, however, solution which I am looking for is not available.

Problem:
1.I have multiple columns on my grid. One of them is Hyperlink column e.g. Employee Name
2. Now I am filtering on particular column, e.g. Department and storing that value into session, as I can click on employee name hyperlink to redirect to employee profile page.
Session["FILTER_EXPRESSION"] = reportingGrid.MasterTableView.FilterExpression;

3. Now when I again visiting to grid page, I am using following line of code:
reportingGrid.MasterTableView.FilterExpression = Session["FILTER_EXPRESSION"].ToString();

4. This is filtering properly but, filter text boxes are black, which suppose to load with filter expression value.

5.I got following line of code which is used to show filter value into textbox.
GridColumn gc = reportingGrid.MasterTableView.GetColumnSafe("DepartmentName");
               gc.CurrentFilterFunction = GridKnownFunction.Contains;
               gc.CurrentFilterValue = "Finance";

 but I dont want to use this. I want to read the string which is stored in session and load values. Because this is used when you know on which column filter is applied. But in my case, in session variable I dont know, on how many columns filter applied and what is their respective values.

I will really appreciate your help.
Avinash

2 Answers, 1 is accepted

Sort by
0
Avinash
Top achievements
Rank 1
answered on 02 Feb 2011, 10:34 PM
Hi,

I got reference in following forum,

I have modified 2 functions as per my need and it is working fantastic :)

/// <summary>
    /// Save filter expression into session variable
    /// </summary>
    public void SaveSettings()
    {
        object[] gridSettings = new object[2];
        int columnsLength = this.reportingGrid.MasterTableView.Columns.Count + this.reportingGrid.MasterTableView.AutoGeneratedColumns.Length;
        ArrayList allColumns = new ArrayList(columnsLength);
        allColumns.AddRange(this.reportingGrid.MasterTableView.Columns);
        allColumns.AddRange(this.reportingGrid.MasterTableView.AutoGeneratedColumns);
 
        /******************  Save filters  *******************/
        gridSettings[0] = (object)this.reportingGrid.MasterTableView.FilterExpression;
        //save the visible/displayed columns settings and current filter value/current filter function             
        ArrayList visibleColumns = new ArrayList(columnsLength);
        ArrayList displayedColumns = new ArrayList(columnsLength);
        Pair[] columnFilter = new Pair[columnsLength];
        int i = 0;
        foreach (GridColumn column in allColumns)
        {
            //Check current column has applied filter
            // Only then add that into array
            if (!string.IsNullOrEmpty(column.CurrentFilterValue))
            {
                columnFilter[i] = new Pair(column.CurrentFilterFunction, column.CurrentFilterValue);
            }
            i++;
        }
 
        gridSettings[1] = columnFilter;
        Session["FILTER_EXPRESSION"] = gridSettings;
    }
 
    /// <summary>
    /// Load session variable filter settings when user comes first time onto page
    /// </summary>
    public void LoadSettings()
    {
        object[] gridSettings = (object[])Session["FILTER_EXPRESSION"];
        int columnsLength = this.reportingGrid.MasterTableView.Columns.Count + this.reportingGrid.MasterTableView.AutoGeneratedColumns.Length;
        ArrayList allColumns = new ArrayList(columnsLength);
        allColumns.AddRange(this.reportingGrid.MasterTableView.Columns);
        allColumns.AddRange(this.reportingGrid.MasterTableView.AutoGeneratedColumns);
 
        /******************  Load filter expression  *******************/
        this.reportingGrid.MasterTableView.FilterExpression = (string)gridSettings[0];
        Pair[] columnFilter = (Pair[])gridSettings[1];
 
        int j = 0;
 
        foreach (GridColumn column in allColumns)
        {
            //Check current column is not null
            // And filter applied on it
            if (columnFilter[j] != null)
            {
                column.CurrentFilterFunction = (GridKnownFunction)columnFilter[j].First;
                column.CurrentFilterValue = (string)columnFilter[j].Second;
                column.Visible = true;
                column.Display = true;
            }
            j++;
        }
    }

Nice to work again on such interesting issue.

Enjoy coding !!!
0
Dev Cesar Augusto
Top achievements
Rank 1
answered on 28 Mar 2017, 09:47 PM

Perfect!

Successfully applied and tested

Successfully applied and tested
Tags
Grid
Asked by
Avinash
Top achievements
Rank 1
Answers by
Avinash
Top achievements
Rank 1
Dev Cesar Augusto
Top achievements
Rank 1
Share this question
or