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

Hide a column for a grid that is DataBound and AutoPopulateColumns=true

4 Answers 529 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Gary B.
Top achievements
Rank 1
Gary B. asked on 21 Aug 2009, 10:28 PM
When I specify column names in the aspx file those show up with "System."... (it gets cut off because there is not enough room), but then the columns show up again matching exactly the names from my DataSource. How do I get the DataSource data to populate the MasterTableView columns.
I noticed when I do grid.DataBind() alone (not filling in the column names in aspx MasterTableView) then there are no columns in the MasterTableView object, so I can't just hide it in the codebehind.

It seems like it should be simple to just Bind the Source then hide the column I don't want to display, but it is not working like that.

4 Answers, 1 is accepted

Sort by
0
ManniAT
Top achievements
Rank 2
answered on 22 Aug 2009, 04:49 PM
Hi,

I guess you have a way to identify the columns you want to hide.
Here a simple approach - I use the "Header Text" which is (in autobound columns) the name of the DataField bound to the column.

protected void rgTwo_ColumnCreated(object sender, Telerik.Web.UI.GridColumnCreatedEventArgs e) {  
            string strX = e.Column.HeaderText;  
            if(strX == "TheID") {  
                e.Column.Visible = false;  
            }  
        } 
So in general - you check something on your column (name, datatype...) to identify it and if it matches the criteria - simply set Visible to false.

Regards

Manfred
PS: if this post was helpful please use "Mark as answer"
0
Accepted
Princy
Top achievements
Rank 2
answered on 24 Aug 2009, 05:44 AM
Hello Gary,

You can try the following code in any of the grid events to hide a column from being displayed in the grid's normal mode as well as edit mode.
c#:
 RadGrid1.MasterTableView.GetColumn("ColumnUniqueName").Visible = false
 (RadGrid1.MasterTableView.GetColumn("ColumnUniqueName"as GridBoundColumn).ReadOnly = true
 // where ColumnUniqueName is the same as the name of the datafield which you would want to hide 

Thanks
Princy.
0
Gary B.
Top achievements
Rank 1
answered on 24 Aug 2009, 02:13 PM
Thanks I used the GetColumn("Name").Visible = false;
Ok, is it just me or is this confusing.
<code>
grid.MasterTableView.Columns.Count == 0
</code>
is true
but
<code>
grid.MasterTableView.GetColumn("Name");
</code>
still returns a column object. Where is it storing it? Or is there some magic going on?
If I wanted to hide columns 1-5 or something would I have to create a list of their names to iterate through or is there an actual enumerable object I can access?
0
Accepted
Princy
Top achievements
Rank 2
answered on 25 Aug 2009, 06:43 AM
Hello Gary,

The MasterTableView.Columns collection does not include the autogenerated columns and that is why you are able to retrieve the autogenerated column even when the columns' count is 0. When you use autogenerated columns, you have to use the following code:
c#:
if (RadGrid1.MasterTableView.RenderColumns.Length == 0) 
 { 
   RadGrid1.MasterTableView.GetColumn("Name").Visible = false
 } 
 
To iterate through some columns inorder to hide it in order, you can try out the following code:
c#:
 protected void RadGrid1_PreRender(object sender, EventArgs e) 
    { 
        for(int i=2; i<=7; i++)  
        { 
         RadGrid1.MasterTableView.RenderColumns[i].Visible = false
        } 
    } 

Thanks
Princy.
Tags
Grid
Asked by
Gary B.
Top achievements
Rank 1
Answers by
ManniAT
Top achievements
Rank 2
Princy
Top achievements
Rank 2
Gary B.
Top achievements
Rank 1
Share this question
or