I add some columns in Page_Load,and want to set multicolumn header in these columns.
in Page_load
var dr = db.ExecuteReader("select id,name from tb_warehouse"); while (dr.Read()) { var col = new UltraGridColumn(true) { BaseColumnName = ("Quantity" + dr["id"]), Key = ("Quantity" + dr["id"]), HeaderText = dr["name"].ToString(), DataType = "System.Decimal", AllowUpdate = AllowUpdate.Yes, Format = "###,###,###.##", Width = Unit.Pixel(60) }; col.CellStyle.HorizontalAlign = HorizontalAlign.Right; j += 1; col.Header.RowLayoutColumnInfo.OriginX = j; uwgList.Columns.Insert(j, col); } dr.Close();
in InitializeLayout foreach (UltraGridColumn c in uwgList.DisplayLayout.Bands[0].Columns) { c.Header.RowLayoutColumnInfo.OriginY = 1; } var ch = new ColumnHeader(true); ch.Caption = "test"; ch.RowLayoutColumnInfo.OriginX = 4; ch.RowLayoutColumnInfo.OriginY = 0; ch.RowLayoutColumnInfo.SpanX = uwgList.Columns.FromKey("sumQuantity").Index - uwgList.Columns.FromKey("unit").Index; e.Layout.Bands[0].HeaderLayout.Add(ch);
foreach (UltraGridColumn c in e.Layout.Bands[0].Columns) { if ((c.Index <= uwgList.Columns.FromKey("unit").Index) || (c.Index >= uwgList.Columns.FromKey("sumQuantity").Index))
{ c.Header.RowLayoutColumnInfo.OriginY = 0; c.Header.RowLayoutColumnInfo.SpanY = 2;
} }
but these columns's header caption didn't display the "test".
why??
From what I can tell, the code you are using seems perfectly fine and is in sync with what we have in our multi column example located here:
http://samples.infragistics.com/2008.2/webfeaturebrowser/srcview.aspx?path=WebGrid/MultiHeaders/MultiHeaders.src&file=MultiHeaders.aspx.cs&font=3
The only reason for the problem that I can think of at this type is that InitializeLayout happens at an earlier point than Page_Load, hence the columns are still not instantiated there.
Can you place breakpoint in both methods and see which one gets executed first? Or can you move your column init code from Page_Load to OnInit and see how it goes?
I have another thought which may also help determine what's going on.
Are you databinding the WebGrid, or are you adding rows dynamically (in addition to adding columns dynamically)?
If you're adding rows dynamically, then the InitializeLayout event will not get raised. If this is the case, then you'll need to move all the code from the InitializeLayout event handelr to either Page_Load or OnInit.
If you're databinding the WebGrid, then it's likely an order-of-events situation as Rumen described in his post.
Hi,
The user still had problem after trying the suggestions. Was this ever got resolved? I need to do the similar things:
1. Multi-Column Header (MCH) presentation in the Grid Header columns
e.g. A 3-row header with: Year has 4 Quarters & 3 Months Columns per each Quarter.
2. To create columns dynamically, by code, based on users inputs to determine the # of columns needed, in this case: the Start & End dates - to drive how many months to be displayed, as MCH mentioned in #1 above.
Thanks.