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

[Solved] radgrid get hidden columns back

1 Answer 231 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Mark
Top achievements
Rank 1
Mark asked on 27 Jan 2010, 05:12 PM

hello,

 

Here is my query:

 

-          We have developed our own Grid(custom control) derived from the RadGrid. We are trying to build in 3 buttons/options for displaying the columns – ‘Show All’, ‘Default’ and ‘Custom’ – wherein Show All – displays all the columns, Default – shows a set number of columns and Custom option – allows you to select columns from a checkbox list.

-          On first load, the default columns are visible. If you try to select columns using the Custom option, it works only with a sub-set of the default columns and not if the selected column is not the one in the default list. Which means it allows you to remove a column from the default list of columns but doesn’t allow you to add any column to the default list. This is true even for the Show All option when I try to bring back all the columns.

-          Following is the code is in the overridden OnPreRender event of the custom control, which drives the above functionality.

-          Can you have a look at the code and tell me if this is how you get the hidden columns back ? Also can you confirm if PreRender is the correct place to handle this? Because code here works for remove the columns, so presume it works for adding the columns as well.

-          Also are there any mandatory events or other code snippets to implemented for a custom control derived from a Telerik RadGrid control?

 

 

========START CODE========

public class MyGrid : Telerik.Web.UI.RadGrid

{

......

......

//PreRender – overridden event

 

switch (this.ShowHidePanelState)

            {

                case ShowHideColumnPanelStates.All:

                    {

                        for (int i = 0; i < this.Columns.Count; i++)

                        {

                            this.Columns[i].Visible = true;

                        }

                        break;

                    }

                case ShowHideColumnPanelStates.Default:

                    {

                        Hashtable defaultColumns = new Hashtable();

                        foreach (DefaultColumn column in this.DefaultColumns)

                        {

                            if (this.Columns.FindByUniqueName(column.UniqueName) != null)

                            {

                                this.Columns.FindByUniqueName(column.UniqueName).Visible = true;

                            }

                            defaultColumns.Add(column.UniqueName, column.UniqueName);

                        }

 

                        for (int i = 0; i < this.Columns.Count; i++)

                        {

                            if (!defaultColumns.ContainsKey(this.Columns[i].UniqueName) )

                            {

                                this.Columns[i].Visible = false;

                            }

                        }

                        break;

                    }

                case ShowHideColumnPanelStates.Custom:

                    {

                        for (int i = 0; i < this.CustomShowHideColumns.Items.Count; i++)

                        {

                            if (this.Columns.FindByUniqueName(this.CustomShowHideColumns.Items[i].Value) != null)

                            {

                                if (this.CustomShowHideColumns.Items[i].Selected == true)

                                    this.Columns.FindByUniqueName(this.CustomShowHideColumns.Items[i].Value).Visible = true;

                                else

                                    this.Columns.FindByUniqueName(this.CustomShowHideColumns.Items[i].Value).Visible = false;

                            }

                        }

                        break;

                    }

            }

 

 

//CustomShowHideColumns - property

 

       private CheckBoxList _customShowHideColumns;

        private CheckBoxList CustomShowHideColumns

        {

            get

            {

                if (_customShowHideColumns == null)

                {

                    _customShowHideColumns = new CheckBoxList();

                    _customShowHideColumns.ID = "CustomShowHideColumns";

                    for (int i = 0; i < this.Columns.Count; i++)

                    {

                        string text = this.Columns[i].HeaderText;

                        string value = this.Columns[i].UniqueName;

                        ListItem item = new ListItem(text, value);

                        //item.Attributes.Add("onclick", string.Format("showHideColumn('{0}',{1},this.checked)", this.ClientID, i.ToString()));

                        _customShowHideColumns.Items.Add(item);

                    }

                    _customShowHideColumns.RepeatDirection = RepeatDirection.Horizontal;

                    _customShowHideColumns.RepeatColumns = this.ShowHideRepeatColumns;

 

                    bool overideDefaults = false;

                    for (int i = 0; i < _customShowHideColumns.Items.Count; i++)

                    {

                        string checkboxClientName = String.Format("CustomShowHideColumns${0}", i);

                        if (this.FormItems[checkboxClientName] != null)

                        {

                            _customShowHideColumns.Items[i].Selected = true;

                            overideDefaults = true;

                        }

                    }

                    if (overideDefaults == false)

                    {

                        this.SetCheckBoxListDefaults();

                    }

                }

            

                return _customShowHideColumns;

            }

        }

 

        private void SetCheckBoxListDefaults()

        {

            foreach (DefaultColumn column in this.DefaultColumns)

            {

                for (int i = 0; i < this.CustomShowHideColumns.Items.Count; i++)

                {

                    if (this.CustomShowHideColumns.Items[i].Value == column.UniqueName)

                    {

                        this.CustomShowHideColumns.Items[i].Selected = true;

                        i = this.Columns.Count;

                    }

                }

            }

        }

 

.

.

.

}//Class ends

======End CODE============

 

Any help will be much appreciated.........


1 Answer, 1 is accepted

Sort by
0
Schlurk
Top achievements
Rank 2
answered on 29 Jan 2010, 02:28 PM
One thing I noticed, which could be a source of the issue, is the usage of the "Visible" property as opposed to "Display". The Visible property essentially tells the grid whether or not the columns should be rendered on the page, while the display property simply sets up the CSS to hide or display the column. I think if you just use Display="true/false" instead of Visible you should be fine.
Tags
Grid
Asked by
Mark
Top achievements
Rank 1
Answers by
Schlurk
Top achievements
Rank 2
Share this question
or