Auto generated columns set width

10 posts, 0 answers
  1. Francis
    Francis avatar
    11 posts
    Member since:
    Jan 2008

    Posted 09 Jan 2012 Link to this post

    Hi,
    I bind the DataTable to the RadGrid with autogeneratecolumns set to true. The columns i bind have need to be set with a different widths.
    Here s the code below The TableLayout is set to "AUTO".

    <telerik:RadGrid runat="server" ID="_grid" OnItemCreated="_grid_Created" AutoGenerateColumns="true" OnNeedDataSource="_grid_Need"><br>    <MasterTableView TableLayout="Auto"><br>    </MasterTableView><br>    </telerik:RadGrid

    And below is the code behind
    protected void _grid_Need(object sender, GridNeedDataSourceEventArgs e)
            {
                List<Wraps> d = new List<Wraps>();

                for (int i = 0; i < 10; i++)
                {
                    d.Add(new Wraps
                    {
                        ID = i,
                        Address = "SOme thing//////.....",
                        Name = string.Format("SOME NAME {0}", i),
                        Zip = (i + 1000).ToString()
                    });
                }
                _grid.DataSource = d;
            }

            protected void _grid_Created(object sender, GridItemEventArgs e)
            {
                foreach (GridColumn column in e.Item.OwnerTableView.RenderColumns)
                {
                    column.HeaderStyle.Width = Unit.Pixel(50);
                    column.ItemStyle.Width = Unit.Pixel(50);
                }
            }

    Nothing happens . Kindly help me so that i be able to set different widths for diffrent columns ????

    Thanks & Regards,
    Francis P.
  2. Jayesh Goyani
    Jayesh Goyani avatar
    2732 posts
    Member since:
    May 2010

    Posted 09 Jan 2012 Link to this post

    Hello,

    protected void RadGrid1_PreRender(object sender, EventArgs e)
        {
            foreach(GridColumn column in  RadGrid1.MasterTableView.AutoGeneratedColumns)
            {
                if (column.UniqueName == "ParentID")
                {
                    column.ItemStyle.Width = Unit.Pixel(800);
                }
            }
        }


    Thanks,
    Jayesh Goyani
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 09 Jan 2012 Link to this post

    Hello,

    Try the following code.
    C#.
    protected void grid_ColumnCreated(object sender, GridColumnCreatedEventArgs e)
    {
      foreach (GridBoundColumn col in grid.MasterTableView.AutoGeneratedColumns)
     {
       col.HeaderStyle.Width = Unit.Pixel(100);
    }
     }

    -Shinu.
  5. THOMAS
    THOMAS avatar
    1 posts
    Member since:
    Apr 2013

    Posted 30 Apr 2013 Link to this post

    Shinu - can you provide code for this solution in vb? Thanks

  6. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 30 Apr 2013 Link to this post

    Hi,

    Try the following.
    VB:
    Protected Sub grid_ColumnCreated(sender As Object, e As GridColumnCreatedEventArgs)
        For Each col As GridBoundColumn In grid.MasterTableView.AutoGeneratedColumns
            col.HeaderStyle.Width = Unit.Pixel(100)
        Next
    End Sub

    Thanks,
    Shinu.
  7. Julian
    Julian avatar
    54 posts
    Member since:
    Apr 2015

    Posted 11 May 2015 Link to this post

    I wanted to say that those responses work but it seems that the pre-e

     

    GridColumn[] gcArray = radGrid1.MasterTableView.AutoGeneratedColumns;
     
               for (int col = 0; col < gcArray.Length; col++)
               {
                   if (col == 0)
                   {
                       gcArray[col].HeaderStyle.Width = 220;
                       gcArray[col].HeaderStyle.HorizontalAlign = HorizontalAlign.Left;
                       gcArray[col].ItemStyle.HorizontalAlign = HorizontalAlign.Right;
                   }
                   else
                   {
                       gcArray[col].HeaderStyle.HorizontalAlign = HorizontalAlign.Right;
                       gcArray[col].ItemStyle.HorizontalAlign = HorizontalAlign.Right;
                   }
               }
     

     This worked fine until I added the preRender event. Then it was is if was completely ignored. I had to add the above code posted by the Admin's here to set column width.

     Just thought I'd share.

     

     

     

  8. Eyup
    Admin
    Eyup avatar
    3007 posts

    Posted 14 May 2015 Link to this post

    Hi Julian,

    Thank you for sharing solution with our community. Generally, the ColumnCreated event handler is suitable for similar requirements:
    http://www.telerik.com/help/aspnet-ajax/grid-customizing-autogenerated-columns.html

    Regards,
    Eyup
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  9. Julian
    Julian avatar
    54 posts
    Member since:
    Apr 2015

    Posted 14 May 2015 in reply to Eyup Link to this post

    I'll remember that.

     Actually the code I posted above was inside the "radGrid1_OnDataBound" event. This code stopped functioning once I added the "radGrid1_PreRender" event. To fix it so I could re-size my columns again I had to add code to the PreRender event:

     

     foreach (GridColumn column in radGrid1.MasterTableView.AutoGeneratedColumns)
                {
                    if (column.UniqueName == " ")
                    {
                        column.ItemStyle.Width = Unit.Pixel(1200);
                    }
                }

     

    I am unsure why the addition of the PreRender event caused the code in the OnDataBound event to stop working?

     

     

  10. Eyup
    Admin
    Eyup avatar
    3007 posts

    Posted 19 May 2015 Link to this post

    Hello Julian,

    I am not familiar with your specific project, but the usefulness of the DataBound event handler is limited and generally, there are more appropriate events for almost any scenario.

    Regards,
    Eyup
    Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
  11. Julian
    Julian avatar
    54 posts
    Member since:
    Apr 2015

    Posted 19 May 2015 in reply to Eyup Link to this post

    I'll keep that in mind thanks!
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017