Problem binding DataGrid nested inside Repeater

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stephen Miller

    Problem binding DataGrid nested inside Repeater

    I am using the OnItemDataBound event of Repeater control to nest a
    DataGrid within the Repeater. When I attempt to bind to the DataGrid
    using the DataSource method I get the error message "Object reference
    not set to an instance of an object". This error message commonly
    occurs when a server control is incorrecly declared, so naturally I
    have double checked this.

    To test this, I moved the aspx code for the DataGrid
    ('myNestedDataG rid') outside the Repeater ('myRepeater') and it bound
    without problem.

    I know that you can nest a DataGrid within a Repeater, but I must be
    missing something:

    <code_behind>

    '-- Declare server controls
    Protected WithEvents myRepeater As System.Web.UI.W ebControls.Repe ater
    Protected WithEvents myNestedDataGri d As
    System.Web.UI.W ebControls.Data Grid

    '-- Define page wide variable to hold nested data source
    Private mDataView As DataView

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    System.EventArg s) Handles MyBase.Load

    Dim myDataSet As DataSet = getRepeater

    mDataView = myDataSet.Table s("myTable").De faultView

    myRepeater.Data Source() = mDataView
    myRepeater.Data Bind()

    End Sub

    Public Sub myRepeater_Item Bound(ByVal sender As System.Object, ByVal
    e As System.Web.UI.W ebControls.Repe aterItemEventAr gs) Handles
    myRepeater.Item DataBound

    Dim lngIdx As Integer = e.Item.ItemInde x
    If lngIdx > -1 Then

    myNestedDataGri d.DataSource =
    getNestedDataGr id(mDataView(ln gIdx).Row("Item ID")).Tables("m yNestedTable"). DefaultView
    myNestedDataGri d.DataBind()

    End If

    End Sub

    Private Function getRepeater() As DataSet
    '-- Return DataSet
    End Function

    Private Function getNestedDataGr id(ByVal ItemID As Long) As DataSet
    '-- Return DataSet, using ItemID as primary key
    End Function

    </code_behind>

    <aspx_that_fail s>

    <asp:Repeater id="myRepeater " runat="Server"
    OnItemDataBound ="myRepeater_It emBound">
    <ItemTemplate >
    <b><%# DataBinder.Eval (Container.Data Item, "ItemName") %></b>
    <asp:datagrid id="myNestedDat aGrid">
    <columns>
    <asp:boundcolum n datafield="Fiel d1"></asp:boundcolumn >
    <asp:boundcolum n datafield="Fiel d2"></asp:boundcolumn >
    <asp:boundcolum n datafield="Fiel d3"></asp:boundcolumn >
    </columns>
    </asp:datagrid>
    </ItemTemplate>
    </asp:Repeater>

    </aspx_that_fails >

    <aspx_that_work s>

    <asp:Repeater id="myRepeater " runat="Server"
    OnItemDataBound ="myRepeater_It emBound">
    <ItemTemplate >
    <b><%# DataBinder.Eval (Container.Data Item, "ItemName") %></b>
    </ItemTemplate>
    </asp:Repeater>
    <asp:datagrid id="myNestedDat aGrid">
    <columns>
    <asp:boundcolum n datafield="Fiel d1"></asp:boundcolumn >
    <asp:boundcolum n datafield="Fiel d2"></asp:boundcolumn >
    <asp:boundcolum n datafield="Fiel d3"></asp:boundcolumn >
    </columns>
    </asp:datagrid>

    </aspx_that_works >
  • James Zhuo

    #2
    Re: Problem binding DataGrid nested inside Repeater

    are you sure that you can an control inside another and yet still be able to
    reference it??
    i think i have tried this before without success either.

    "Stephen Miller" <jsausten@hotma il.com> wrote in message
    news:cdb404de.0 308151848.6523d 96c@posting.goo gle.com...[color=blue]
    > I am using the OnItemDataBound event of Repeater control to nest a
    > DataGrid within the Repeater. When I attempt to bind to the DataGrid
    > using the DataSource method I get the error message "Object reference
    > not set to an instance of an object". This error message commonly
    > occurs when a server control is incorrecly declared, so naturally I
    > have double checked this.
    >
    > To test this, I moved the aspx code for the DataGrid
    > ('myNestedDataG rid') outside the Repeater ('myRepeater') and it bound
    > without problem.
    >
    > I know that you can nest a DataGrid within a Repeater, but I must be
    > missing something:
    >
    > <code_behind>
    >
    > '-- Declare server controls
    > Protected WithEvents myRepeater As System.Web.UI.W ebControls.Repe ater
    > Protected WithEvents myNestedDataGri d As
    > System.Web.UI.W ebControls.Data Grid
    >
    > '-- Define page wide variable to hold nested data source
    > Private mDataView As DataView
    >
    > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
    > System.EventArg s) Handles MyBase.Load
    >
    > Dim myDataSet As DataSet = getRepeater
    >
    > mDataView = myDataSet.Table s("myTable").De faultView
    >
    > myRepeater.Data Source() = mDataView
    > myRepeater.Data Bind()
    >
    > End Sub
    >
    > Public Sub myRepeater_Item Bound(ByVal sender As System.Object, ByVal
    > e As System.Web.UI.W ebControls.Repe aterItemEventAr gs) Handles
    > myRepeater.Item DataBound
    >
    > Dim lngIdx As Integer = e.Item.ItemInde x
    > If lngIdx > -1 Then
    >
    > myNestedDataGri d.DataSource =
    >[/color]
    getNestedDataGr id(mDataView(ln gIdx).Row("Item ID")).Tables("m yNestedTable"). D
    efaultView[color=blue]
    > myNestedDataGri d.DataBind()
    >
    > End If
    >
    > End Sub
    >
    > Private Function getRepeater() As DataSet
    > '-- Return DataSet
    > End Function
    >
    > Private Function getNestedDataGr id(ByVal ItemID As Long) As DataSet
    > '-- Return DataSet, using ItemID as primary key
    > End Function
    >
    > </code_behind>
    >
    > <aspx_that_fail s>
    >
    > <asp:Repeater id="myRepeater " runat="Server"
    > OnItemDataBound ="myRepeater_It emBound">
    > <ItemTemplate >
    > <b><%# DataBinder.Eval (Container.Data Item, "ItemName") %></b>
    > <asp:datagrid id="myNestedDat aGrid">
    > <columns>
    > <asp:boundcolum n datafield="Fiel d1"></asp:boundcolumn >
    > <asp:boundcolum n datafield="Fiel d2"></asp:boundcolumn >
    > <asp:boundcolum n datafield="Fiel d3"></asp:boundcolumn >
    > </columns>
    > </asp:datagrid>
    > </ItemTemplate>
    > </asp:Repeater>
    >
    > </aspx_that_fails >
    >
    > <aspx_that_work s>
    >
    > <asp:Repeater id="myRepeater " runat="Server"
    > OnItemDataBound ="myRepeater_It emBound">
    > <ItemTemplate >
    > <b><%# DataBinder.Eval (Container.Data Item, "ItemName") %></b>
    > </ItemTemplate>
    > </asp:Repeater>
    > <asp:datagrid id="myNestedDat aGrid">
    > <columns>
    > <asp:boundcolum n datafield="Fiel d1"></asp:boundcolumn >
    > <asp:boundcolum n datafield="Fiel d2"></asp:boundcolumn >
    > <asp:boundcolum n datafield="Fiel d3"></asp:boundcolumn >
    > </columns>
    > </asp:datagrid>
    >
    > </aspx_that_works >[/color]


    Comment

    • Stevie_mac

      #3
      Re: Problem binding DataGrid nested inside Repeater

      It can be done (sort of)
      In the HTML of your datagrid put a reference to a data source...
      <asp:DataGrid id=DataGrid1 runat="server" AutogenerateCol umns="False"
      DataSource='<%# getGridDataSour ce( ctype(DataBinde r.Eval(Containe r.DataItem,
      "CustomerID"),s tring) ) %>'

      Whats going on here is... When a row of your repeater binds, a data grid is
      created & the data source calls a function called getGridDataSour ce. THE
      IMPORTANT BIT is the DataBinder.Eval (Container.Data Item, "CustomerID ") that
      is passed to getGridDataSour ce. The Container.DataI tem is the Current
      Reapeater Row. so... You key together a piece of data from the current
      Repeater row with the datasource of the data grid - so that the content of
      the data grid is relevant to the current repeater row!

      Now, when the datagrids bind, you can have a handler for
      OnItemDataBound ='<%# whatever....%> & get a reference to the grid.

      "Stephen Miller" <jsausten@hotma il.com> wrote in message
      news:cdb404de.0 308151848.6523d 96c@posting.goo gle.com...[color=blue]
      > I am using the OnItemDataBound event of Repeater control to nest a
      > DataGrid within the Repeater. When I attempt to bind to the DataGrid
      > using the DataSource method I get the error message "Object reference
      > not set to an instance of an object". This error message commonly
      > occurs when a server control is incorrecly declared, so naturally I
      > have double checked this.
      >
      > To test this, I moved the aspx code for the DataGrid
      > ('myNestedDataG rid') outside the Repeater ('myRepeater') and it bound
      > without problem.
      >
      > I know that you can nest a DataGrid within a Repeater, but I must be
      > missing something:
      >
      > <code_behind>
      >
      > '-- Declare server controls
      > Protected WithEvents myRepeater As System.Web.UI.W ebControls.Repe ater
      > Protected WithEvents myNestedDataGri d As
      > System.Web.UI.W ebControls.Data Grid
      >
      > '-- Define page wide variable to hold nested data source
      > Private mDataView As DataView
      >
      > Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
      > System.EventArg s) Handles MyBase.Load
      >
      > Dim myDataSet As DataSet = getRepeater
      >
      > mDataView = myDataSet.Table s("myTable").De faultView
      >
      > myRepeater.Data Source() = mDataView
      > myRepeater.Data Bind()
      >
      > End Sub
      >
      > Public Sub myRepeater_Item Bound(ByVal sender As System.Object, ByVal
      > e As System.Web.UI.W ebControls.Repe aterItemEventAr gs) Handles
      > myRepeater.Item DataBound
      >
      > Dim lngIdx As Integer = e.Item.ItemInde x
      > If lngIdx > -1 Then
      >
      > myNestedDataGri d.DataSource =
      >[/color]
      getNestedDataGr id(mDataView(ln gIdx).Row("Item ID")).Tables("m yNestedTable"). D
      efaultView[color=blue]
      > myNestedDataGri d.DataBind()
      >
      > End If
      >
      > End Sub
      >
      > Private Function getRepeater() As DataSet
      > '-- Return DataSet
      > End Function
      >
      > Private Function getNestedDataGr id(ByVal ItemID As Long) As DataSet
      > '-- Return DataSet, using ItemID as primary key
      > End Function
      >
      > </code_behind>
      >
      > <aspx_that_fail s>
      >
      > <asp:Repeater id="myRepeater " runat="Server"
      > OnItemDataBound ="myRepeater_It emBound">
      > <ItemTemplate >
      > <b><%# DataBinder.Eval (Container.Data Item, "ItemName") %></b>
      > <asp:datagrid id="myNestedDat aGrid">
      > <columns>
      > <asp:boundcolum n datafield="Fiel d1"></asp:boundcolumn >
      > <asp:boundcolum n datafield="Fiel d2"></asp:boundcolumn >
      > <asp:boundcolum n datafield="Fiel d3"></asp:boundcolumn >
      > </columns>
      > </asp:datagrid>
      > </ItemTemplate>
      > </asp:Repeater>
      >
      > </aspx_that_fails >
      >
      > <aspx_that_work s>
      >
      > <asp:Repeater id="myRepeater " runat="Server"
      > OnItemDataBound ="myRepeater_It emBound">
      > <ItemTemplate >
      > <b><%# DataBinder.Eval (Container.Data Item, "ItemName") %></b>
      > </ItemTemplate>
      > </asp:Repeater>
      > <asp:datagrid id="myNestedDat aGrid">
      > <columns>
      > <asp:boundcolum n datafield="Fiel d1"></asp:boundcolumn >
      > <asp:boundcolum n datafield="Fiel d2"></asp:boundcolumn >
      > <asp:boundcolum n datafield="Fiel d3"></asp:boundcolumn >
      > </columns>
      > </asp:datagrid>
      >
      > </aspx_that_works >[/color]


      Comment

      Working...