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

Auto generated columns set width

9 Answers 149 Views
Grid
This is a migrated thread and some comments may be shown as answers.
francis
Top achievements
Rank 1
francis asked on 09 Jan 2012, 11:31 AM
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.

9 Answers, 1 is accepted

Sort by
0
Jayesh Goyani
Top achievements
Rank 2
answered on 09 Jan 2012, 11:50 AM
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
0
Shinu
Top achievements
Rank 1
answered on 09 Jan 2012, 12:00 PM
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.
0
THOMAS
Top achievements
Rank 1
answered on 30 Apr 2013, 12:02 PM
Shinu - can you provide code for this solution in vb? Thanks

0
Shinu
Top achievements
Rank 1
answered on 30 Apr 2013, 12:11 PM
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.
0
Julian
Top achievements
Rank 1
answered on 11 May 2015, 05:59 PM

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.

 

 

 

0
Eyup
Telerik team
answered on 14 May 2015, 10:38 AM
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
0
Julian
Top achievements
Rank 1
answered on 14 May 2015, 03:26 PM

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?

 

 

0
Eyup
Telerik team
answered on 19 May 2015, 08:33 AM
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
0
Julian
Top achievements
Rank 1
answered on 19 May 2015, 02:04 PM
I'll keep that in mind thanks!
Tags
Grid
Asked by
francis
Top achievements
Rank 1
Answers by
Jayesh Goyani
Top achievements
Rank 2
Shinu
Top achievements
Rank 1
THOMAS
Top achievements
Rank 1
Julian
Top achievements
Rank 1
Eyup
Telerik team
Share this question
or