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

Still adding new and new empty columns after Postback

1 Answer 71 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Daniel
Top achievements
Rank 1
Daniel asked on 11 Oct 2011, 07:24 PM
Hi,

I have problem with dynamically generated columns in RadGrid. Each postback adds me same count of empty columns. Here is my test code.

thx for help

DK

<%@ Page Title="" Language="C#" AutoEventWireup="true"  CodeBehind="Test.aspx.cs" Inherits="Test.TestGrid" %>
   <head runat="server" />
  <form id="frm" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
      <asp:HiddenField ID="hfMinYear" runat="server" /><asp:HiddenField ID="hfMaxYear" runat="server" />
    <telerik:RadGrid ID="grdForecast" AllowFilteringByColumn="false" AllowMultiRowSelection="true" AllowSorting="true"
      AutoGenerateColumns="false" EnableViewState="true" OnNeedDataSource="grdForecast_NeedDataSource" runat="server">
      <ClientSettings EnableRowHoverStyle="true">
        <Selecting AllowRowSelect="True" />
      </ClientSettings>
      <MasterTableView DataKeyNames="ForecastId" EnableColumnViewState="false"><HeaderStyle Wrap="false" /><ItemStyle Wrap="false" />
        <Columns>
          <telerik:GridClientSelectColumn UniqueName="ClientSelectColumn" />
          <telerik:GridBoundColumn DataField="ValueTotal" DataFormatString="{0:0.##}" HeaderText="Total Value" HeaderStyle-Wrap="false" ItemStyle-HorizontalAlign="Right" ItemStyle-Wrap="false" ReadOnly="True" UniqueName="ValueTotal"></telerik:GridBoundColumn>
        </Columns>
      </MasterTableView>
    </telerik:RadGrid>
    <asp:button id="ddd" text="Postback" runat="server" />
</form>

Code-behind

using System;
using System.Data;
using Telerik.Web.UI;
 
namespace Test
{
  public partial class TestGrid : System.Web.UI.Page
  {
    protected int ProjectId;
 
    protected void Page_Init(object sender, EventArgs e)
    {
//      if (IsPostBack)
      {
        int MinYear = 2010, MaxYear = 2020;
        CreateColumns(MinYear, MaxYear);
      }
    }
 
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
 
    protected void CreateColumns(int MinYear, int MaxYear)
    {
      for (int i = MinYear; i <= MaxYear; i++)
      {
        grdForecast.MasterTableView.Columns.Add(new GridBoundColumn()
        {
          DataField = "Value" + i,
          DataFormatString = "{0:0.##}",
          HeaderText = "Value " + i,
          ReadOnly = true,
          UniqueName = "Value" + i
        });
      }
    }
 
    protected void grdForecast_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
      grdForecast.DataSource = new DataTable();
    }
 
  }
}


1 Answer, 1 is accepted

Sort by
0
Accepted
Iana Tsolova
Telerik team
answered on 14 Oct 2011, 01:52 PM
Hello Daniel,

You should created the columns on initial postback only. And when the grid is added declaratively, you should first add the newly created column and then set its properties:
GridBoundColumn column = new GridBoundColumn();
grdForecast.MasterTableView.Columns.Add(column);
column.DataField = "Value" + i;
column.DataFormatString = "{0:0.##}";
column.HeaderText = "Value " + i;
column.ReadOnly = true;
column.UniqueName = "Value" + i;


Greetings,
Iana Tsolova
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Grid
Asked by
Daniel
Top achievements
Rank 1
Answers by
Iana Tsolova
Telerik team
Share this question
or