Web DataGrid problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Pete Davis

    Web DataGrid problem

    I'm using a web form DataGrid. When I populate the grid, I set the
    DataSource to a collection and then call DataGrid.DataBi nd(). When looking
    at the DataGrid in the debugger, the DataSource is set properly. When I then
    get to the SelectedIndexCh anged() event, the DataSource is set to null. I
    have both the page and the datagrid set with enableViewState set to true.
    The Items collection is valid and the Cells contain the proper data. What am
    I missing? What happened to the DataSource?

    Pete


  • TJoker .NET [MVP]

    #2
    Re: Web DataGrid problem

    When you use the DataGrid, the DataSource property is not kept in the
    viewstate.
    The approach to work with the datagrid is to re-fetch the datasource
    everytime it changes.
    In yout case, if you have information on each colelction item that you'd
    want to use in the event handler, then put this information somewhere in the
    row, like in a hidden input tag.

    --
    TJoker, MCSD.NET
    MVP: Paint, Notepad, Solitaire

    *************** *************** **********


    "Pete Davis" <pdavis68@hotma il.com> wrote in message
    news:8446993c6a 2626a84fa694aac 8d0db5f@news.me ganetnews.com.. .[color=blue]
    > I'm using a web form DataGrid. When I populate the grid, I set the
    > DataSource to a collection and then call DataGrid.DataBi nd(). When looking
    > at the DataGrid in the debugger, the DataSource is set properly. When I[/color]
    then[color=blue]
    > get to the SelectedIndexCh anged() event, the DataSource is set to null. I
    > have both the page and the datagrid set with enableViewState set to true.
    > The Items collection is valid and the Cells contain the proper data. What[/color]
    am[color=blue]
    > I missing? What happened to the DataSource?
    >
    > Pete
    >
    >[/color]


    Comment

    • Pete Davis

      #3
      Re: Web DataGrid problem

      Do you know what the reasoning behind that is? I mean, it can't be to save
      memory because the grid could just as easily hold onto the DataSource to
      redraw itself as the item and cell collections. It would be really nice if
      this were an optional "feature" because as it is, it means I need to store
      the collection in the session.

      Pete

      "TJoker .NET [MVP]" <nospam@nonono. no> wrote in message
      news:OElrewTgDH A.1764@TK2MSFTN GP09.phx.gbl...[color=blue]
      > When you use the DataGrid, the DataSource property is not kept in the
      > viewstate.
      > The approach to work with the datagrid is to re-fetch the datasource
      > everytime it changes.
      > In yout case, if you have information on each colelction item that you'd
      > want to use in the event handler, then put this information somewhere in[/color]
      the[color=blue]
      > row, like in a hidden input tag.
      >
      > --
      > TJoker, MCSD.NET
      > MVP: Paint, Notepad, Solitaire
      >
      > *************** *************** **********
      >
      >
      > "Pete Davis" <pdavis68@hotma il.com> wrote in message
      > news:8446993c6a 2626a84fa694aac 8d0db5f@news.me ganetnews.com.. .[color=green]
      > > I'm using a web form DataGrid. When I populate the grid, I set the
      > > DataSource to a collection and then call DataGrid.DataBi nd(). When[/color][/color]
      looking[color=blue][color=green]
      > > at the DataGrid in the debugger, the DataSource is set properly. When I[/color]
      > then[color=green]
      > > get to the SelectedIndexCh anged() event, the DataSource is set to null.[/color][/color]
      I[color=blue][color=green]
      > > have both the page and the datagrid set with enableViewState set to[/color][/color]
      true.[color=blue][color=green]
      > > The Items collection is valid and the Cells contain the proper data.[/color][/color]
      What[color=blue]
      > am[color=green]
      > > I missing? What happened to the DataSource?
      > >
      > > Pete
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • TJoker .NET [MVP]

        #4
        Re: Web DataGrid problem

        This is meant to save viewstate usage. Serializing a collection in the
        viewstate can produce huge HTML source. The datagrid just keeps what is
        needed to redraw each cell. Whatever information you didn't use in the grid,
        is lost.


        --
        TJoker, MCSD.NET
        MVP: Paint, Notepad, Solitaire

        *************** *************** **********


        "Pete Davis" <pdavis68@hotma il.com> wrote in message
        news:563057d79f ff64535620659c2 4416d02@news.me ganetnews.com.. .[color=blue]
        > Do you know what the reasoning behind that is? I mean, it can't be to save
        > memory because the grid could just as easily hold onto the DataSource to
        > redraw itself as the item and cell collections. It would be really nice if
        > this were an optional "feature" because as it is, it means I need to store
        > the collection in the session.
        >
        > Pete
        >
        > "TJoker .NET [MVP]" <nospam@nonono. no> wrote in message
        > news:OElrewTgDH A.1764@TK2MSFTN GP09.phx.gbl...[color=green]
        > > When you use the DataGrid, the DataSource property is not kept in the
        > > viewstate.
        > > The approach to work with the datagrid is to re-fetch the datasource
        > > everytime it changes.
        > > In yout case, if you have information on each colelction item that you'd
        > > want to use in the event handler, then put this information somewhere in[/color]
        > the[color=green]
        > > row, like in a hidden input tag.
        > >
        > > --
        > > TJoker, MCSD.NET
        > > MVP: Paint, Notepad, Solitaire
        > >
        > > *************** *************** **********
        > >
        > >
        > > "Pete Davis" <pdavis68@hotma il.com> wrote in message
        > > news:8446993c6a 2626a84fa694aac 8d0db5f@news.me ganetnews.com.. .[color=darkred]
        > > > I'm using a web form DataGrid. When I populate the grid, I set the
        > > > DataSource to a collection and then call DataGrid.DataBi nd(). When[/color][/color]
        > looking[color=green][color=darkred]
        > > > at the DataGrid in the debugger, the DataSource is set properly. When[/color][/color][/color]
        I[color=blue][color=green]
        > > then[color=darkred]
        > > > get to the SelectedIndexCh anged() event, the DataSource is set to[/color][/color][/color]
        null.[color=blue]
        > I[color=green][color=darkred]
        > > > have both the page and the datagrid set with enableViewState set to[/color][/color]
        > true.[color=green][color=darkred]
        > > > The Items collection is valid and the Cells contain the proper data.[/color][/color]
        > What[color=green]
        > > am[color=darkred]
        > > > I missing? What happened to the DataSource?
        > > >
        > > > Pete
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Ignacio Machin

          #5
          Re: Web DataGrid problem

          Hi Pete,

          That's the expected behavior, to understand what is happening I think is
          better explain a little how the state feature works, when you set the
          enableViewState basically what you are saying is that the control is going
          to be serialized inside a hidden control in the html page using a StateBag
          object, if you view the source of the html page you will see this info.
          Now the thing is that the DataSource has a reference to another object, if
          it's serialized then the referenced object would also need to be serialized
          this would create a HUGE overload to the page !!!
          What was the solution?
          It's your responsability to keep a reference to the datasource if you need
          it, you can use several ways to do so, the easiest I think is using the
          Session collection and reassign it in the OnLoad event of the page:

          page_Load( object sender, System.EventArg s e)
          {
          if ( !IsPostBack)
          {
          grid.DataSource = collection;
          Session["SavedCollectio n"] = collection;

          }
          else
          grid.DataSource = ( CollectionType) Session["SavedCollectio n"];

          }


          Hope this help,

          --
          Ignacio Machin,
          ignacio.machin AT dot.state.fl.us
          Florida Department Of Transportation


          "Pete Davis" <pdavis68@hotma il.com> wrote in message
          news:8446993c6a 2626a84fa694aac 8d0db5f@news.me ganetnews.com.. .[color=blue]
          > I'm using a web form DataGrid. When I populate the grid, I set the
          > DataSource to a collection and then call DataGrid.DataBi nd(). When looking
          > at the DataGrid in the debugger, the DataSource is set properly. When I[/color]
          then[color=blue]
          > get to the SelectedIndexCh anged() event, the DataSource is set to null. I
          > have both the page and the datagrid set with enableViewState set to true.
          > The Items collection is valid and the Cells contain the proper data. What[/color]
          am[color=blue]
          > I missing? What happened to the DataSource?
          >
          > Pete
          >
          >[/color]


          Comment

          Working...