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

Problem adding dynamic template columns

3 Answers 140 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jeremy
Top achievements
Rank 1
Jeremy asked on 28 Jul 2010, 09:58 AM
Hi

I've followed this example and the documentation here to create a grid with 2 rows in the header for just some of the columns. I've declared the grid control in the .aspx control, and declared the mixture of normal bound columns and template columns in the Page_Init function of the code behind.

It displays fine, but when I do a postback (eg sort) then it adds more columns to the grid. I've tried checking for IsPostback, but that just makes the template column contents disappear. What am I missing?

Here is the .aspx page:
<telerik:RadGrid ID="RadGrid1" runat="server" AutoGenerateColumns="false"
        AllowSorting="true" Skin="Vista">
    <MasterTableView DataKeyNames="HOLE_ID, HOLE_SORT">
        <HeaderStyle HorizontalAlign="Center" />
        <ItemStyle HorizontalAlign="Center" />
        <AlternatingItemStyle HorizontalAlign="Center" />
        <NoRecordsTemplate>
            <div style="width:100%; text-align:center; height:21px; padding:4px 4px 4px 10px;">
                <asp:Label ID="lblNoRecords" runat="server" Text="No data exists for this Hole."></asp:Label>
            </div>
        </NoRecordsTemplate>
    </MasterTableView>
    <ClientSettings>
        <Scrolling AllowScroll="true" />
    </ClientSettings>
</telerik:RadGrid>



Code behind excerpt:

Private colHeaderGroupList as List(of String) = GetColumnHeaderGroups()
Private colHeaderList as List(of MyColumnData) = GetColumnHeaderData()
 
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
    If Page.IsPostBack = False Then
        LoadColumns()
    End If
End Sub
 
Private Sub LoadColumns()
    'clear columns
    RadGrid1.MasterTableView.Columns.Clear()
    'add standard column
    Dim col1 As GridBoundColumn = New GridBoundColumn()
    RadGrid1.MasterTableView.Columns.Add(col1)
    With col1
        .DataField = "HOLE_NO"
        .HeaderText = "Hole"
        .SortExpression = "HOLE_SORT"
    End With
    'add elements as template columns to use double row headers
    For Each colHeader As String In colHeaderGroupList
        'add column to grid
        Dim templateCol As GridTemplateColumn = New GridTemplateColumn()
        RadGrid1.MasterTableView.Columns.Add(templateCol)
        'get just the columns for this group
        Dim thisGroup as List(of MyColumnData) = (From x in colHeaderList
                                  Where x.Group = colHeader
                                  Select x).ToList()
        'create templates for column
        templateCol.HeaderTemplate = New MyDynamicHeaderTemplate(thisGroup)
        templateCol.ItemTemplate = New MyDynamicItemTemplate(thisGroup)
    Next
End Sub
 
Private Class MyDynamicHeaderTemplate
.
.
.
End Class
 
Private Class MyDynamicItemTemplate
.
.
.
End Class


Kind Regards,
Jeremy

3 Answers, 1 is accepted

Sort by
0
Jeremy
Top achievements
Rank 1
answered on 30 Jul 2010, 09:46 AM
Ahah! Put it in RadGrid_init instead of Page_init and it works fine :)
0
Dave
Top achievements
Rank 1
answered on 18 Aug 2010, 07:17 PM
I have a similar issue.  I am greating several dynamic columns in my grid.  The columns display fine, but when I enter Editmode, the dynamic columns do not display.

Any thoughts?

Thanks,

Dave

<telerik:RadGrid ID="rgBR" runat="server" AllowSorting="True" 
       Skin="Office2007" onneeddatasource="RadGrid1_NeedDataSource" AutoGenerateColumns="false"
       Height="320px" Width="900px" onprerender="rgBR_PreRender" 
       oninit="rgBR_Init">
       <ClientSettings AllowRowsDragDrop="true" AllowKeyboardNavigation="true">
          <KeyboardNavigationSettings AllowActiveRowCycle="true" FocusKey="Y" />
          <Scrolling AllowScroll="True" UseStaticHeaders="True" />
      </ClientSettings>
       <MasterTableView DataKeyNames="PricingDrugGroupID" EditMode="InPlace">
           <CommandItemSettings ShowAddNewRecordButton="false" 
               ShowExportToExcelButton="true" ShowRefreshButton="false" />
           <Columns>
               <telerik:GridBoundColumn DataField="CountryName"
                   DataFormatString="<nobr>{0}</nobr>" HeaderStyle-Width="130px" 
                   HeaderText="CountryName" ItemStyle-Width="130px" UniqueName="Country">
               </telerik:GridBoundColumn>
               <telerik:GridBoundColumn DataField="PricingDrugName" 
                   DataFormatString="<nobr>{0}</nobr>" HeaderStyle-Width="130px" 
                   HeaderText="PricingDrugName" ItemStyle-Width="130px" UniqueName="PricingDrug">
               </telerik:GridBoundColumn>
              <telerik:GridBoundColumn DataField="BrandGeneric" 
                   DataFormatString="<nobr>{0}</nobr>" HeaderStyle-Width="130px" 
                   HeaderText="BrandGeneric" ItemStyle-Width="130px" UniqueName="BrandGeneric">
               </telerik:GridBoundColumn>
               <telerik:GridBoundColumn DataField="PricingDrugGroupTypeName" 
                   DataFormatString="<nobr>{0}</nobr>" HeaderStyle-Width="130px" 
                   HeaderText="PricingDrugGroupTypeName" ItemStyle-Width="130px" 
                   UniqueName="PricingDrugGroupTypeName">
               </telerik:GridBoundColumn>
               </Columns>
       </MasterTableView>
   </telerik:RadGrid>

public class BrGeGridItemTemplate : ITemplate
 {
     protected Label lblLabel;
     private DynamicColumn rfdFieldData;
     public BrGeGridItemTemplate(DynamicColumn FieldData)
     {
         rfdFieldData = FieldData;
     }
     public void InstantiateIn(Control container)
     {
         lblLabel = new Label();
         lblLabel.ID = "lbl";
         lblLabel.DataBinding += new EventHandler(lblLabel_DataBinding);
         container.Controls.Add(lblLabel);
     }
     private void lblLabel_DataBinding(object sender, EventArgs e)
     {
         Label lblLabel = (Label)sender;
         GridDataItem container = (GridDataItem)lblLabel.NamingContainer;
         IList<ForecastAdjustmentView> adjs = ((PricingDrugGroupView)container.DataItem).Adjustmets;
         // Don't display a value if this is the base year
         // NOTE: actually need to calculate starting price if base year
         string adjustmentVal = String.Empty;
         if (!rfdFieldData.isBaseYear)
         {
             ForecastAdjustmentView adj = adjs.Where(a => a.ForecastPeriodTimelineID == rfdFieldData.ForecastPeriodTimelineID).Single();
             adjustmentVal = String.Format("{0:##.00%}", adj.AdjustmentValue);
         }
         lblLabel.Text = adjustmentVal;
     }
 }
 public class BrGeEditGridItemTemplate : ITemplate
 {
     protected Label lblLabel;
     private DynamicColumn rfdFieldData;
     public BrGeEditGridItemTemplate(DynamicColumn FieldData)
     {
         rfdFieldData = FieldData;
     }
     public void InstantiateIn(Control container)
     {
         lblLabel = new Label();
         lblLabel.ID = "lbl";
         lblLabel.DataBinding += new EventHandler(lblLabel_DataBinding);
         container.Controls.Add(lblLabel);
     }
     private void lblLabel_DataBinding(object sender, EventArgs e)
     {
         TextBox tbTextBox = (TextBox)sender;
         GridDataItem container = (GridDataItem)tbTextBox.NamingContainer;
         IList<ForecastAdjustmentView> adjs = ((PricingDrugGroupView)container.DataItem).Adjustmets;
         // Don't display a value if this is the base year
         // NOTE: actually need to calculate starting price if base year
         string adjustmentVal = String.Empty;
         if (!rfdFieldData.isBaseYear)
         {
             ForecastAdjustmentView adj = adjs.Where(a => a.ForecastPeriodTimelineID == rfdFieldData.ForecastPeriodTimelineID).Single();
             adjustmentVal = String.Format("{0:##.00%}", adj.AdjustmentValue);
         }
         tbTextBox.Text = adjustmentVal;
     }
 }  
 public class DynamicColumn
 {
     public int ForecastPeriodTimelineID { get; set; }
     public bool isBaseYear { get; set; }
     public string ColumnName { get; set; }
     public string ColumnType { get; set; }
 }
  public partial class PF_BR : System.Web.UI.Page
 {
     #region Private Members
     IMarketDefinitionServices _marketDefinationServices;
     IPriceForecasterServices _priceForecasterServices;
     int ForecastID = 164;
     int VersionID = 5788;
     public IList<DynamicColumn> dynamicColumns;
     #endregion
     protected void Page_Load(object sender, EventArgs e)
     {
         if (String.IsNullOrEmpty(SessionHandler.ForecastId) ||
             String.IsNullOrEmpty(SessionHandler.ForecastId))
         {
             // Redirect
             Response.Redirect("MF_OpenForecast.aspx");
         }
         else
         {
             ForecastID = int.Parse(SessionHandler.ForecastId);
             VersionID = int.Parse(SessionHandler.VersionId);
         }
        _marketDefinationServices = ObjectFactory.GetInstance<MarketDefinitionServices>();
        _priceForecasterServices = ObjectFactory.GetInstance<PriceForecasterServices>();
         if (!Page.IsPostBack)
         {
             GetDynamicColumnInfo();
             RebuildDynamicColumns();
         }
     }
     private void GetDynamicColumnInfo()
     {
         IList<ForecastPeriodTimelineView> timelineData = _marketDefinationServices.getForecastTimelineByForecastID(ForecastID);
         dynamicColumns = new List<DynamicColumn>();
         foreach (var tld in timelineData)
         {
             // Append a column to the grid
             //GridTemplateColumn tc = new GridTemplateColumn();
             DateTime date = Convert.ToDateTime(tld.PeriodTimelineDate);
             DynamicColumn dc = new DynamicColumn
             {
                 ForecastPeriodTimelineID = tld.ForecastPeriodTimelineID,
                 isBaseYear = tld.ForecastPeriodTimelineTypeID == 1 ? true : false, //Base Year
                 ColumnName = date.Year.ToString(),
                 ColumnType = "decimal",
             };
             dynamicColumns.Add(dc);
         }
     }
     private void RebuildDynamicColumns()
     {
         foreach (var dc in dynamicColumns)
         {
               
             GridTemplateColumn templateColumn = new GridTemplateColumn();
             templateColumn.ItemTemplate = new BrGeGridItemTemplate(dc);
             templateColumn.DataField = dc.ColumnName;
             templateColumn.SortExpression = dc.ColumnName;
             templateColumn.UniqueName = dc.ColumnName.ToString();
             templateColumn.HeaderText = dc.ColumnName;
             templateColumn.EditItemTemplate = new BrGeEditGridItemTemplate(dc);
             rgBR.MasterTableView.Columns.Add(templateColumn);
         }
     }
     protected void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
     {
         rgBR.DataSource = _priceForecasterServices.getBrandPercentBrandPricingDrugGroupViews(VersionID);
     }
0
Jeremy
Top achievements
Rank 1
answered on 19 Aug 2010, 01:41 AM
I think yourRebuildDynamicColumns() function needs to called from the radgrid Init function - not the pageload :)
I ended up not using this option, but here is the empty function call;

protected void RadGrid1_Init(object sender, EventArgs e)
{
 
}

Tags
Grid
Asked by
Jeremy
Top achievements
Rank 1
Answers by
Jeremy
Top achievements
Rank 1
Dave
Top achievements
Rank 1
Share this question
or