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

how to access columns collection when they are auto-generated?

3 Answers 109 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Piyush Bhatt
Top achievements
Rank 2
Piyush Bhatt asked on 08 Jul 2009, 04:09 PM
Hello,

I need to keep AutoGenerateColumns=True as I am binding it to a dynamic data structure that keeps changing. But after I bind the data I want to set the Format string for few of those columns by going through the column collection and then changing 

DataFormatString property. Unfortunately, after the DataBind() call, the RadGrid does not have any columns in the ColumnsCollection. It looks like this ColumnsCollection only contains the columns that are declared in the ASPX page. How to access the columns collection when they are auto-generated?

-Piyush

3 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 08 Jul 2009, 04:33 PM
Hello Piyush,

Auto-generated columns are accessible through the AutoGeneratedColumns array.
AutoGeneratedColumns Property

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
Piyush Bhatt
Top achievements
Rank 2
answered on 08 Jul 2009, 04:40 PM

 

Thank you responding quickly. See below - the DataFormatString is not affecting the display and the output is same. What is the best place to change the DataFormatString for AutoGenerated Columns?

List
<ds> dslist = new List<ds>();

 

dslist.Add(

new ds("A1", "1000"));

 

dslist.Add(

new ds("A2", "2000"));

 

dslist.Add(

new ds("A3", "3000"));

 

radGrid1.DataSource = dslist;

radGrid1.DataBind();

 

foreach (GridColumn column in radGrid1.MasterTableView.AutoGeneratedColumns) // .MasterTableView.RenderColumns)

 

{

 

if (column is GridBoundColumn)

 

{

 

GridBoundColumn bc = column as GridBoundColumn;

 

bc.DataFormatString =

"{0:C2}";

 

}

}

0
Princy
Top achievements
Rank 2
answered on 09 Jul 2009, 04:50 AM
Hello Piyush,

Try setting the DataFormatString for bound columns in the ColumnCreated event of the grid as shown below:
c#:
protected void RadGrid1_ColumnCreated(object sender, GridColumnCreatedEventArgs e) 
    { 
        if (e.Column is GridBoundColumn) 
        { 
            GridBoundColumn col = (GridBoundColumn)e.Column; 
            col.DataFormatString = "{0:C2}";            
        } 
    } 

Thanks
Princy.
Tags
Grid
Asked by
Piyush Bhatt
Top achievements
Rank 2
Answers by
Daniel
Telerik team
Piyush Bhatt
Top achievements
Rank 2
Princy
Top achievements
Rank 2
Share this question
or