Keeping DataGrid Columns through Postback

Discussion in 'ASP .Net Datagrid Control' started by Allen K, Apr 6, 2004.

  1. Allen K

    Allen K Guest

    I have a Datagrid that has programmatically added columns, as well as
    columns added through the VS.NET DataGrid Control Editor. Everything is
    fine the first time around when the page loads, but when I do a postback,
    the columns that I programmatically added disappear. The columns I added
    through the Control Editor however are still visible.

    Anyone have any idea as to what the problem might be? Does it have
    something to do with the columns not persisting in viewstate? How do I fix
    this?

    Thanks!
    Allen
    Allen K, Apr 6, 2004
    #1
    1. Advertising

  2. Allen K

    Teemu Keiski Guest

    Hi,

    you need to add columns on every request, they act like other controls in
    that sense that they need to be recreated for each request.

    --
    Teemu Keiski
    MCP, Microsoft MVP (ASP.NET), AspInsiders member
    ASP.NET Forum Moderator, AspAlliance Columnist
    http://blogs.aspadvice.com/joteke

    "Allen K" <x******@hotmail.com> wrote in message
    news:Xns94C2C45CF677Ablahblahcom@198.161.157.145...
    > I have a Datagrid that has programmatically added columns, as well as
    > columns added through the VS.NET DataGrid Control Editor. Everything is
    > fine the first time around when the page loads, but when I do a postback,
    > the columns that I programmatically added disappear. The columns I added
    > through the Control Editor however are still visible.
    >
    > Anyone have any idea as to what the problem might be? Does it have
    > something to do with the columns not persisting in viewstate? How do I fix
    > this?
    >
    > Thanks!
    > Allen
    Teemu Keiski, Apr 6, 2004
    #2
    1. Advertising

  3. Allen K

    Allen K Guest

    I tried re-adding the columns in the Page_Load method on every postback but
    that doesn't seem to work.. The only way I can get it to work is if I
    rebind the DataGrid each time.. however that doesn't seem like an elegant
    solution considering that I have a huge datagrid and the same information
    needs to be persisted through 10 or so postbacks.. I don't want to have to
    rebind each time..

    Help please!

    "Teemu Keiski" <> wrote in news:uEwnH49GEHA.2052
    @TK2MSFTNGP11.phx.gbl:

    > Hi,
    >
    > you need to add columns on every request, they act like other controls in
    > that sense that they need to be recreated for each request.
    >
    Allen K, Apr 7, 2004
    #3
  4. Allen K

    Guest Guest

    I am doing the exact same thing you are requesting. Here
    is what you do:
    1. You must wire-up a method to the eventhandler of the
    grid called ItemDataBound.
    this.dgServices.ItemDataBound += new
    System.Web.UI.WebControls.DataGridItemEventHandler
    (this.dgServices_ItemDataBound);
    2. Then create a method, usually the same name as the
    control with the event after the underscore.
    private void dgServices_ItemDataBound(object sender,
    DataGridItemEventArgs e)
    3. Then, depending on what you are displaying (Header,
    Item, Footer) you must write those out to the control.
    if(e.Item.ItemType == ListItemType.Header)
    if(e.Item.ItemType == ListItemType.Item ||
    e.Item.ItemType == ListItemType.AlternatingItem)
    if(e.Item.ItemType == ListItemType.Pager)

    That should get you started. Have fun!

    Brian
    >-----Original Message-----
    >I have a Datagrid that has programmatically added

    columns, as well as
    >columns added through the VS.NET DataGrid Control

    Editor. Everything is
    >fine the first time around when the page loads, but when

    I do a postback,
    >the columns that I programmatically added disappear. The

    columns I added
    >through the Control Editor however are still visible.
    >
    >Anyone have any idea as to what the problem might be?

    Does it have
    >something to do with the columns not persisting in

    viewstate? How do I fix
    >this?
    >
    >Thanks!
    >Allen
    >.
    >
    Guest, Apr 19, 2004
    #4
    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. Allen K

    DataGrid Columns lost on Postback????

    Allen K, Apr 7, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    528
    Allen K
    Apr 8, 2004
  2. Rodusa
    Replies:
    2
    Views:
    6,380
    Rodusa
    Feb 15, 2005
  3. Fluxcore

    Keeping columns the same length

    Fluxcore, Dec 21, 2003, in forum: HTML
    Replies:
    29
    Views:
    801
    Toby A Inkster
    Dec 22, 2003
  4. rob thomson
    Replies:
    0
    Views:
    260
    rob thomson
    Sep 4, 2003
  5. ton
    Replies:
    2
    Views:
    205
    Alvin Bruney [MVP]
    Feb 11, 2004
Loading...

Share This Page