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

Hide a column programmatically

5 Answers 2204 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Clive Hoggar
Top achievements
Rank 1
Clive Hoggar asked on 24 Aug 2009, 08:10 PM
Hi

I have searched the forums and can't see anything that seesm to match my case.

I want to hide a column based on a value set in a settings table.  No problem getting the
setting value as true or false, but I can't make the column hidden.

Columns are not autogenerated and the column in question is declared like this
<telerik:GridBoundColumn DataField="StockQuantity" DataType="System.Int16"   
            HeaderText="Stock" SortExpression="StockQuantity"   
            UniqueName="StockQuantity">  
</telerik:GridBoundColumn> 

How can this be made visible/invisble from the code behind?
I have tried this below but I think it is applicable only to autogenerated columns.. Anyway it does not do anything.
Protected Sub RadGrid1_ColumnCreated(ByVal sender As Object, ByVal e As Telerik.Web.UI.GridColumnCreatedEventArgs) Handles RadGrid1.ColumnCreated  
 
        If e.Column.UniqueName = "StockQuantity" Then  
            e.Column.Visible = False 
        End If  
 
    End Sub  
 

Thanks for help here!

Clive

5 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 24 Aug 2009, 08:59 PM
Hello Clive,

Please try the following approach:
protected void Page_PreRender(object o, EventArgs e) 
    RadGrid1.MasterTableView.GetColumn("IDColumn").Display = false

I hope this helps. Let me know if you need more information.

Best regards,
Daniel
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Check out the tips for optimizing your support resource searches.
0
Clive Hoggar
Top achievements
Rank 1
answered on 25 Aug 2009, 10:12 AM
Hi

Thanks for this.

In fact I could not get this to work 'as is' but when I put the action in the onload event,
it worked! 

Just giving feedback in case it helps someone else..

Clive 
0
Gene
Top achievements
Rank 1
answered on 01 Jul 2012, 01:42 PM
I am using RadAjaxManager and have a similar problem to the one described in this old post.  In my situation, I first display my grid and then, based on a selection from a rad ComboBox whose SelectedIndexChanged event I handle in the code behind.  Depending on what the user has selected in the ComboBox, I want to programmatically hide a column or change something about it.  For an example below, I am attempting to hide a column (which was already rendered) from view after the user makes a selection.  I confirmed that I am "hooked up" because when the user makes a selection, I can see in debug mode that the code executes in the event handler.  But, even though I set the Visible property of the column to false, it still appears in the grid after the event handler is done.  What I am doing wrong or perhaps forgetting to do?

Much thanks for whomever can please help me!

protected void comboRouteTypeSelector_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
        {
            GridColumn gridColumn = this.gridSites.MasterTableView.Columns.FindByUniqueName("SiteSeqNum");
            int Idx = this.gridSites.MasterTableView.Columns.IndexOf(gridColumn);
            this.gridSites.MasterTableView.Columns[Idx].Visible = false;
            this.gridSites.MasterTableView.Rebind();            
        }
0
Clive Hoggar
Top achievements
Rank 1
answered on 01 Jul 2012, 11:17 PM
Hi I think you need to use display=false not visible=false to make this work client side without another postback... Clive
0
Gene
Top achievements
Rank 1
answered on 01 Jul 2012, 11:56 PM
Thanks for responding, Clive, but I also tried using Display=false previously, but to no avail.  So, I have tried both ways in my code behind's event handler, once I tried Display=false and another time I tried Visible=false, but unfortunately, the column with which I am trying to hide still appears.  I also noticed that calling Rebind() in my code behind intiates my grid's PreRender event handler.  I also tried setting Display and Visible to false in the PreRender's event handler, such as is shown below, but that doesn't work, either; after the PreRender event handler finishes execution, I still see my grid containing the column I wish to hide.  

The situation I have is that the controls, including GridColumns and GridTemplateColumns, all first display and are rendered.  Then, depending on a user's criteria selections, I wish to hide or display columns.  There are times in which I would like to toggle the visibility of my grid columns from my code behind and other times I would like to toggle the visibility of the columns from the JavaScript.  Shouldn't it be possible to show or hide columns after they've been previously rendered through both means - from the code behind or from the client JavaScript?  I would like to know how to accomplish this, if it is possible. And, if it is not too much to ask, I would like to know how to access and change other attributes of a grid's column, such as it's header text and it's DataField, from both JavaScript and from the code behind.
protected void gridSites_PreRender(object sender, EventArgs e)
        {
            gridSites.MasterTableView.GetColumn("SiteSeqNum").Visible = false;           
        }

 

Tags
Grid
Asked by
Clive Hoggar
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Clive Hoggar
Top achievements
Rank 1
Gene
Top achievements
Rank 1
Share this question
or