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
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
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.
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) NextEnd SubThanks,
Shinu.
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.
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
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?
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
