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

Modifying Colums when AutoGenerateColumns=True

5 Answers 343 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jeff
Top achievements
Rank 1
Jeff asked on 17 Jul 2009, 12:18 AM
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

5 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 17 Jul 2009, 05:11 AM
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.
0
Jeff
Top achievements
Rank 1
answered on 17 Jul 2009, 06:57 AM
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
0
Andrés David Santacoloma Isaza
Top achievements
Rank 1
answered on 16 May 2011, 11:25 PM
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.
0
Princy
Top achievements
Rank 2
answered on 17 May 2011, 05:48 AM
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.
0
Peter
Top achievements
Rank 1
answered on 21 Nov 2012, 06:45 AM
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.
Tags
Grid
Asked by
Jeff
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Jeff
Top achievements
Rank 1
Andrés David Santacoloma Isaza
Top achievements
Rank 1
Peter
Top achievements
Rank 1
Share this question
or