Adding columns dynamically to data grid

Discussion in 'ASP .Net' started by Roger Frei, Jun 15, 2007.

  1. Roger Frei

    Roger Frei Guest

    Hello ng,

    I have a datagrid that is bound to a datasource. That works good so far. Now
    I want to add another column to my grid dynamically. That also works good
    until the first postback. The column is still there but for some reason its
    content is empty. I added the code underneath this post. The problem is that
    the delegate CreateCellTemplate is not called anymore. But why? I can't
    understand.. :-( Viewstate is enabled for the grid.

    Code
    (The grid is an infragistics one. however, i think that has no impact in
    that case.)

    protected void Page_Load(object sender, EventArgs e) {

    if (!Page.IsPostBack) {

    // add column dynamically to grid
    TemplatedColumn col = new TemplatedColumn(true);

    col.CellTemplate = new CompiledTemplateBuilder(new
    BuildTemplateMethod(CreateCellTemplate));
    myGrid.Columns.Insert(1, col);
    }
    }

    private void CreateCellTemplate(Control container) {

    CellItem currentItem = (CellItem) container;

    // cast it into my business object
    InvoiceEntity dataItem = (InvoiceEntity)currentItem.DataItem;

    CheckBox chkBox = new CheckBox();
    chkBox.ID = "baseList." + dataItem.Id;
    container.Controls.Add(chkBox);
    }


    Thanks in advance for your help!

    Regards Roger
     
    Roger Frei, Jun 15, 2007
    #1
    1. Advertising

  2. You need to recreate dynamically added controls on every postback,
    preferably in the PreInit event.

    --
    Eliyahu Goldin,
    Software Developer & Consultant
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin


    "Roger Frei" <> wrote in message
    news:...
    > Hello ng,
    >
    > I have a datagrid that is bound to a datasource. That works good so far.

    Now
    > I want to add another column to my grid dynamically. That also works good
    > until the first postback. The column is still there but for some reason

    its
    > content is empty. I added the code underneath this post. The problem is

    that
    > the delegate CreateCellTemplate is not called anymore. But why? I can't
    > understand.. :-( Viewstate is enabled for the grid.
    >
    > Code
    > (The grid is an infragistics one. however, i think that has no impact in
    > that case.)
    >
    > protected void Page_Load(object sender, EventArgs e) {
    >
    > if (!Page.IsPostBack) {
    >
    > // add column dynamically to grid
    > TemplatedColumn col = new TemplatedColumn(true);
    >
    > col.CellTemplate = new CompiledTemplateBuilder(new
    > BuildTemplateMethod(CreateCellTemplate));
    > myGrid.Columns.Insert(1, col);
    > }
    > }
    >
    > private void CreateCellTemplate(Control container) {
    >
    > CellItem currentItem = (CellItem) container;
    >
    > // cast it into my business object
    > InvoiceEntity dataItem = (InvoiceEntity)currentItem.DataItem;
    >
    > CheckBox chkBox = new CheckBox();
    > chkBox.ID = "baseList." + dataItem.Id;
    > container.Controls.Add(chkBox);
    > }
    >
    >
    > Thanks in advance for your help!
    >
    > Regards Roger
    >
    >
     
    Eliyahu Goldin, Jun 15, 2007
    #2
    1. Advertising

  3. Roger Frei

    Roger Frei Guest

    Hi! Thanks a lot for your answer. I tried your suggestion. Firstly, I put
    the code in the PreInit event. The problem there is that the grid is null at
    this time.. :-( Then I tried it with the PreLoad event. Same result as
    before when the code was in the Load method. :-(

    It has something to do with the "CreateCellTempate" method. It is only
    called if (isPostBack == false). It doesn't matter if I recreate the column
    after every potsback or not. This method is called just once! Is this
    somehow related with the databinding of the grid? Any ideas? :)




    "Eliyahu Goldin" <> schrieb im
    Newsbeitrag news:...
    > You need to recreate dynamically added controls on every postback,
    > preferably in the PreInit event.
    >
    > --
    > Eliyahu Goldin,
    > Software Developer & Consultant
    > Microsoft MVP [ASP.NET]
    > http://msmvps.com/blogs/egoldin
    >
    >
    > "Roger Frei" <> wrote in message
    > news:...
    >> Hello ng,
    >>
    >> I have a datagrid that is bound to a datasource. That works good so far.

    > Now
    >> I want to add another column to my grid dynamically. That also works good
    >> until the first postback. The column is still there but for some reason

    > its
    >> content is empty. I added the code underneath this post. The problem is

    > that
    >> the delegate CreateCellTemplate is not called anymore. But why? I can't
    >> understand.. :-( Viewstate is enabled for the grid.
    >>
    >> Code
    >> (The grid is an infragistics one. however, i think that has no impact in
    >> that case.)
    >>
    >> protected void Page_Load(object sender, EventArgs e) {
    >>
    >> if (!Page.IsPostBack) {
    >>
    >> // add column dynamically to grid
    >> TemplatedColumn col = new TemplatedColumn(true);
    >>
    >> col.CellTemplate = new CompiledTemplateBuilder(new
    >> BuildTemplateMethod(CreateCellTemplate));
    >> myGrid.Columns.Insert(1, col);
    >> }
    >> }
    >>
    >> private void CreateCellTemplate(Control container) {
    >>
    >> CellItem currentItem = (CellItem) container;
    >>
    >> // cast it into my business object
    >> InvoiceEntity dataItem = (InvoiceEntity)currentItem.DataItem;
    >>
    >> CheckBox chkBox = new CheckBox();
    >> chkBox.ID = "baseList." + dataItem.Id;
    >> container.Controls.Add(chkBox);
    >> }
    >>
    >>
    >> Thanks in advance for your help!
    >>
    >> Regards Roger
    >>
    >>

    >
    >
     
    Roger Frei, Jun 15, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?cG11ZA==?=
    Replies:
    2
    Views:
    992
    =?Utf-8?B?cG11ZA==?=
    Jan 10, 2005
  2. keithb
    Replies:
    0
    Views:
    2,228
    keithb
    May 4, 2006
  3. herman404
    Replies:
    1
    Views:
    455
    =?Utf-8?B?c2xlbWVu?=
    Jun 20, 2006
  4. jonefer
    Replies:
    0
    Views:
    780
    jonefer
    May 21, 2007
  5. Ahmet Gunes

    How to add columns to a grid dynamically at run time?

    Ahmet Gunes, Sep 15, 2003, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    211
    Ravikanth[MVP]
    Sep 15, 2003
Loading...

Share This Page