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

5 posts, 2 answers
  1. Gary B.
    Gary B. avatar
    11 posts
    Member since:
    Jul 2009

    Posted 21 Aug 2009 Link to this post

    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.
  2. ManniAT
    ManniAT avatar
    877 posts
    Member since:
    Nov 2003

    Posted 22 Aug 2009 Link to this post

    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"
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 24 Aug 2009 Link to this post

    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.
  5. Gary B.
    Gary B. avatar
    11 posts
    Member since:
    Jul 2009

    Posted 24 Aug 2009 Link to this post

    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?
  6. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 25 Aug 2009 Link to this post

    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.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017