Modifying Colums when AutoGenerateColumns=True

6 posts, 1 answers
  1. Jeff
    Jeff avatar
    51 posts
    Member since:
    Aug 2007

    Posted 16 Jul 2009 Link to this post

    Hello,

    The scenario:
    • AutoGenerateColumns is set to true
    • I attach a DataTable during NeedDataSource
    • At some point before the page load, I'm trying to hide one of columns if a certain condition is true.

    The problem:
    No matter which event I try, RadGrid1.MasterTableView.Columns is always empty.

    I've tried just about every event: NeedDataSource, OnItemCreated, oncolumncreated, oncolumnsreorder, ondatabound, onprerender, Page.PreRender. At no time is the Columns property populated with columns.

    I suppose I could create the columns programmatically from the DataTable but I'd rather let RadGrid do it for me if possible.

    Any advice is greatly appreciated.

    Thanks,
    Jeff
  2. Answer
    Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 17 Jul 2009 Link to this post

    Hello Jeff,

    You can try out the following code in the PreRender event to hide a column in the grid:
    c#:
    protected void RadGrid1_PreRender1(object sender, EventArgs e)  
        {  
           RadGrid1.MasterTableView.GetColumn("ColumnUniqueName").Visible = false;  
             // the columnUniqueName is the same as the DataField of the column(autogenerated)   
        }  

    Thanks
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Jeff
    Jeff avatar
    51 posts
    Member since:
    Aug 2007

    Posted 17 Jul 2009 Link to this post

    Princy- You truly are the "Master."

    Even though the Columns property is not populated, the GetColumn function retrieves the data that you know should be there. (Because you put it there!)

    This seems like a bug to me but maybe telerik has an explanation.

    In any case, he's another example, broken down a little more:

        protected void RadGrid1_PreRender(object sender, EventArgs e) 
        { 
            // You won't find any columns here 
            Console.WriteLine(RadGrid1.MasterTableView.Columns.Count); 
     
            // Use GetColumn- suddenly you have access to columns you know should be there 
            GridColumn gc = RadGrid1.MasterTableView.GetColumn("Manufacturer"); 
             
            // Hide the column you didn't want to see (or make any other modifications) 
            gc.Visible = false
             
            // This _so_ doesn't work. 
            //RadGrid1.MasterTableView.Columns[0].Visible = !GroupedByManu; 
        } 
     

    Thanks again.

    Jeff
  5. Andrés David Santacoloma Isaza
    Andrés David Santacoloma Isaza avatar
    51 posts
    Member since:
    Mar 2010

    Posted 16 May 2011 Link to this post

    Hi Jeff:
    Your code is good, but I have a problem:
    I do not know the number or the name of the columns because they are generated from a stored procedure depending on various circumstances, sometimes throws a number of columns and sometimes other amount, and so the same with their name.
    Is there another way to access data without knowing its name?

    Thanks.
  6. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 17 May 2011 Link to this post

    Hello Andres,

     You can use the UniqueName property to get the column unique name. Here is a  sample code. Hope this helps you.

    C#:
    foreach (GridColumn col in RadGrid1.MasterTableView.AutoGeneratedColumns)
     {
     RadGrid1.MasterTableView.GetColumn(col.UniqueName).Visible = false;
     }  

    Thanks,
    Princy.
  7. Peter
    Peter avatar
    98 posts
    Member since:
    Mar 2012

    Posted 21 Nov 2012 Link to this post

    Thanks Jeff - yes, I find it bizarre that the Columns collection is empty (or only 2 columns - the auto Edit/Delete buttons), yet the GetColumn event works fine.

    I guess I'm just glad it works - it allows us to build a generic/dynamic solution for managing multiple (simple/lookup) tables.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017