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

4 posts, 0 answers
  1. Piyush Bhatt
    Piyush Bhatt avatar
    166 posts
    Member since:
    Nov 2007

    Posted 08 Jul 2009 Link to this post

    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

  2. Daniel
    Admin
    Daniel avatar
    4943 posts

    Posted 08 Jul 2009 Link to this post

    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.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Piyush Bhatt
    Piyush Bhatt avatar
    166 posts
    Member since:
    Nov 2007

    Posted 08 Jul 2009 Link to this post

     

    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}";

     

    }

    }

  5. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 08 Jul 2009 Link to this post

    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.
Back to Top