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

[Solved] Combo filter on dynamically created columns - radGrid

4 Answers 274 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jan
Top achievements
Rank 1
Jan asked on 12 Apr 2013, 01:27 PM
Hi all.

I have radGrid to which I have to generate all columns dynamically. I'd like to have combobox filtering for some columns (e.g. enums).
I've spent a lot of hours on searching through google/this forum, on trying demos, examples, whatever. Unfortunatelly I was not successful. When I am able to create column with combobox in filteringitem I have error:

"Failed to load viewstate. The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request. ...."

radGrid building is placed into Page_load.

When I have normal textbox filter on that column everything is ok. 

4 Answers, 1 is accepted

Sort by
0
Antonio Stoilkov
Telerik team
answered on 17 Apr 2013, 08:03 AM
Hi Jaroslav,

The described behavior is expected. It is not possible to create programmatic template columns in Page_Load. In order to achieve your scenario you should create RadGrid and its columns in the Page_Init event. Additionally, you could go trough the help article below which describes the approaches for RadGrid programmatic creation in detail.

Regards,
Antonio Stoilkov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Jan
Top achievements
Rank 1
answered on 18 Apr 2013, 12:21 PM
Hi Antonio.

I've tried to move BuildGrid functionality from Page_Load to Page_Init. Unfortunatelly it's the same. Error is:

Message: Sys.WebForms.PageRequestManagerServerErrorException: Failed to load viewstate.  The control tree into which viewstate is being loaded must match the control tree that was used to save viewstate during the previous request.  For example, when adding controls dynamically, the controls added during a post-back must match the type and position of the controls added during the initial request.


My code is:

DataPage code behind
public partial class DataPage : Classes.BasePage
    {
        public string _table = "";
        static TTObject _tableObject;
        protected void Page_Init(object sender, EventArgs e)
        {
            if (Request["table"] != null)
                _table = Request["table"].ToString();
            if (!IsPostBack)
            {
                _tableObject = Data.GetTTObject(_table);
                BuildGrid();
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
             
        }
        private void BuildGrid()
        {
            Classes.GridBuilder.BuildBasicDataGrid(RadGrid1, _table);
        }
         
        protected void RadGrid1_NeedDataSource(object sender, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            Classes.GridBuilder.FillGridWithData(RadGrid1, _tableObject);
        }
    }

GridBuilder class
public static void BuildBasicDataGrid(RadGrid _grid, string _table)
        {
            _grid.AutoGenerateColumns = false;
            _grid.MasterTableView.AutoGenerateColumns = false;
  
            Hashtable _pars = new Hashtable();
            _pars.Add("@userId", ((LoggedUser)HttpContext.Current.Session["User"]).UserId.ToString());
            _pars.Add("@tableName", _table);
            DataTable _dtCols = SvcStoredProcedureExecutor.ExecuteDataTable("ACC_UI_GetUsersTableColumns", System.Data.CommandType.StoredProcedure, _pars);
  
            if (_dtCols.Rows.Count.Equals(0))
            {
                _pars = new Hashtable();
                _pars.Add("@tableName", _table);
                _dtCols = SvcStoredProcedureExecutor.ExecuteDataTable("ACC_UI_GetTableColumns", System.Data.CommandType.StoredProcedure, _pars);
            }
  
            foreach (DataRow _dr in _dtCols.Rows)
            {
                string _edtType = _dr["EDTType"].ToString();
  
                switch (_edtType)
                {
                     case "4":
                        Type _t = Type.GetType("Acc.Toll.UI.Website.Classes.Enums");
                        Hashtable _ht = (Hashtable)_t.GetProperty(_dr["EnumName"].ToString()).GetValue(null);
  
                        GridBoundColumn _ddlCol = new GridBoundColumn();
                        _grid.MasterTableView.Columns.Add(_ddlCol);
  
                        _ddlCol.FilterTemplate = new TTControls.DDLFilterTemplate(_ddlCol, _ht);
                        _ddlCol.DataField = _dr["DBName"].ToString();
                        _ddlCol.HeaderText = _dr["Label"].ToString();
                        _ddlCol.SortExpression = _dr["DBName"].ToString();
                        _ddlCol.UniqueName = _dr["DBName"].ToString();
                        break;
                }
            }
        }

Column filter template

public class DDLFilterTemplate : ITemplate
    {
        GridBoundColumn _col;
        object _comboDS;
        public DDLFilterTemplate(GridBoundColumn column, object comboDataSource)
        {
            this._col = column;
            this._comboDS = comboDataSource;
        }
  
        public void InstantiateIn(System.Web.UI.Control container)
        {
            RadComboBox _combo = new RadComboBox();
            _combo.ID = "DDL" + _col.UniqueName;
            _combo.DataTextField = "Value";
            _combo.DataValueField = "Value";
            _combo.DataSource = _comboDS;
            _combo.DataBind();
  
            _combo.AutoPostBack = true;
            _combo.SelectedIndexChanged += new RadComboBoxSelectedIndexChangedEventHandler(combo_SelectedIndexChanged);
  
            RadComboBoxItem item = _combo.FindItemByText(_col.CurrentFilterValue);
            if (item != null)
            {
                item.Selected = true;
            }
  
            container.Controls.Add(_combo);
        }
  
        void combo_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            ((GridFilteringItem)(((RadComboBox)sender).Parent.Parent)).FireCommandEvent("Filter", new Pair());
        }
    }

Thank You for every additional help.

Regards,

Jaroslav.
0
Antonio Stoilkov
Telerik team
answered on 23 Apr 2013, 05:34 AM
Hi Jaroslav,

The experienced exception continues because when creating a RadGrid control in Page_Init the control could not be declared in the aspx page. In order to resolve your issue you should create the whole RadGrid in the init event as shown in the example below.
public static void BuildBasicDataGrid(RadGrid _grid, string _table)
{
    RadGrid _grid = new RadGrid();
    _grid.AutoGenerateColumns = false;
    _grid.MasterTableView.AutoGenerateColumns = false;
   
    Hashtable _pars = new Hashtable();
    _pars.Add("@userId", ((LoggedUser)HttpContext.Current.Session["User"]).UserId.ToString());
    _pars.Add("@tableName", _table);
    DataTable _dtCols = SvcStoredProcedureExecutor.ExecuteDataTable("ACC_UI_GetUsersTableColumns", System.Data.CommandType.StoredProcedure, _pars);
   
    if (_dtCols.Rows.Count.Equals(0))
    {
        _pars = new Hashtable();
        _pars.Add("@tableName", _table);
        _dtCols = SvcStoredProcedureExecutor.ExecuteDataTable("ACC_UI_GetTableColumns", System.Data.CommandType.StoredProcedure, _pars);
    }
   
    foreach (DataRow _dr in _dtCols.Rows)
    {
        string _edtType = _dr["EDTType"].ToString();
   
        switch (_edtType)
        {
                case "4":
                Type _t = Type.GetType("Acc.Toll.UI.Website.Classes.Enums");
                Hashtable _ht = (Hashtable)_t.GetProperty(_dr["EnumName"].ToString()).GetValue(null);
   
                GridBoundColumn _ddlCol = new GridBoundColumn();
                _grid.MasterTableView.Columns.Add(_ddlCol);
   
                _ddlCol.FilterTemplate = new TTControls.DDLFilterTemplate(_ddlCol, _ht);
                _ddlCol.DataField = _dr["DBName"].ToString();
                _ddlCol.HeaderText = _dr["Label"].ToString();
                _ddlCol.SortExpression = _dr["DBName"].ToString();
                _ddlCol.UniqueName = _dr["DBName"].ToString();
                break;
        }
    }
}

Kind regards,
Antonio Stoilkov
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
0
Jan
Top achievements
Rank 1
answered on 24 Apr 2013, 08:30 AM
I'd like to thank you for your suggestions. Because we have to work with millions of records I had to write own filtering functionality and we do not use built-in filtering now.
Tags
Grid
Asked by
Jan
Top achievements
Rank 1
Answers by
Antonio Stoilkov
Telerik team
Jan
Top achievements
Rank 1
Share this question
or