Nested Datagrid reference?

Discussion in 'ASP .Net Datagrid Control' started by Tim, Jan 5, 2004.

  1. Tim

    Tim Guest

    I have a datagrid that is nested inside of another datagrid. The code
    is below.

    The datagrids display correctly, however, I cannot edit the inner
    datagrid as I haven't been able to correctly reference it. The Edit
    command is also below.

    How can I correctly reference the inner datagrid to edit it?

    Thanks,
    Tim


    <asp:Datagrid... >
    <asp:BoundColumn ...>
    <asp:BoundColumn ...>
    <asp:TemplateColumn>
    <ItemTemplate>
    <asp:DataGrid runat="server" id="dgSub"
    OnEditCommand="dgSub_Edit"
    DataSource='<%#
    ((DataRowView)Container.DataItem).Row.GetChildRows("GroupNameRelations")
    %>'>
    <asp:TemplateColumn HeaderText="Local Group Name">
    <ItemTemplate>
    <%# DataBinder.Evalontainer.DataItem, "LocalGroupName")
    %> </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox ID="txtLocalGroupName" Runat="server"
    Text='<% DataBinder.Eval(Container.DataItem, "LocalGroupName") %>'
    /> </EditItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn>
    <ItemTemplate>
    <asp:Button ID="btnLocalEdit" Runat="server"
    CommandName="Edit" Text="Edit" />
    </ItemTemplate>
    <EditItemTemplate>
    <asp:Button ID="btnLocalUpdate" Runat="server"
    CommandName="Update" Text="Update" />
    <asp:Button ID="btnLocalCancel" Runat="server"
    CommandName="Cancel" Text="Cancel" />
    </EditItemTemplate>
    </asp:TemplateColumn>
    </asp:DataGrid>
    </asp:DataGrid>

    The edit command currently looks like:
    protected void dgLocalGroups_Edit(object sender,
    DataGridCommandEventArgs e)
    {
    DataGrid dgLocalGroups =
    (DataGrid)dgGroups.FindControl("dgLocalGroups");
    // get the row that was clicked
    int intRow = e.Item.ItemIndex;
    dgLocalGroups.EditItemIndex = intRow;
    }
    Tim, Jan 5, 2004
    #1
    1. Advertising

  2. Tim

    Alvin Bruney Guest

    The datagrid can only be nested if its added to the parent control as a
    child control. So what you need to do is find the parent control and then
    iterate thru its collection of child controls. The findcontrol here will do
    most of the work. You need to know what level you are at so that you can
    cast correctly otherwise you will get a cast error. What problems are you
    experiencing?

    --
    Regards,
    Alvin Bruney
    Got tidbits? Get it here...
    http://tinyurl.com/2bz4t
    "Tim" <> wrote in message
    news:...
    > I have a datagrid that is nested inside of another datagrid. The code
    > is below.
    >
    > The datagrids display correctly, however, I cannot edit the inner
    > datagrid as I haven't been able to correctly reference it. The Edit
    > command is also below.
    >
    > How can I correctly reference the inner datagrid to edit it?
    >
    > Thanks,
    > Tim
    >
    >
    > <asp:Datagrid... >
    > <asp:BoundColumn ...>
    > <asp:BoundColumn ...>
    > <asp:TemplateColumn>
    > <ItemTemplate>
    > <asp:DataGrid runat="server" id="dgSub"
    > OnEditCommand="dgSub_Edit"
    > DataSource='<%#
    > ((DataRowView)Container.DataItem).Row.GetChildRows("GroupNameRelations")
    > %>'>
    > <asp:TemplateColumn HeaderText="Local Group Name">
    > <ItemTemplate>
    > <%# DataBinder.Evalontainer.DataItem, "LocalGroupName")
    > %> </ItemTemplate>
    > <EditItemTemplate>
    > <asp:TextBox ID="txtLocalGroupName" Runat="server"
    > Text='<% DataBinder.Eval(Container.DataItem, "LocalGroupName") %>'
    > /> </EditItemTemplate>
    > </asp:TemplateColumn>
    > <asp:TemplateColumn>
    > <ItemTemplate>
    > <asp:Button ID="btnLocalEdit" Runat="server"
    > CommandName="Edit" Text="Edit" />
    > </ItemTemplate>
    > <EditItemTemplate>
    > <asp:Button ID="btnLocalUpdate" Runat="server"
    > CommandName="Update" Text="Update" />
    > <asp:Button ID="btnLocalCancel" Runat="server"
    > CommandName="Cancel" Text="Cancel" />
    > </EditItemTemplate>
    > </asp:TemplateColumn>
    > </asp:DataGrid>
    > </asp:DataGrid>
    >
    > The edit command currently looks like:
    > protected void dgLocalGroups_Edit(object sender,
    > DataGridCommandEventArgs e)
    > {
    > DataGrid dgLocalGroups =
    > (DataGrid)dgGroups.FindControl("dgLocalGroups");
    > // get the row that was clicked
    > int intRow = e.Item.ItemIndex;
    > dgLocalGroups.EditItemIndex = intRow;
    > }
    Alvin Bruney, Jan 5, 2004
    #2
    1. Advertising

  3. Tim

    Tim Cavins Guest

    My problem is that when I click on the Edit button for an item in the
    inner datagrid, I get the following error:

    Exception Details: System.NullReferenceException: Object reference not
    set to an instance of an object.

    This is on the line where I set the EditItemIndex for the inner
    datagrid.

    So, I don't have the reference set up correctly in order to use the
    inner datagrid.

    Can you help with that?

    -Tim



    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Tim Cavins, Jan 5, 2004
    #3
  4. Tim

    Alvin Bruney Guest

    you are going to have to post the relevant code at least for the itemcommand
    handler or edit handler. Just the relevant portions. I've some ideas about
    what might be wrong but i won't speculate without further inspection.

    --
    Regards,
    Alvin Bruney
    Got tidbits? Get it here...
    http://tinyurl.com/2bz4t
    "Tim Cavins" <> wrote in message
    news:%...
    > My problem is that when I click on the Edit button for an item in the
    > inner datagrid, I get the following error:
    >
    > Exception Details: System.NullReferenceException: Object reference not
    > set to an instance of an object.
    >
    > This is on the line where I set the EditItemIndex for the inner
    > datagrid.
    >
    > So, I don't have the reference set up correctly in order to use the
    > inner datagrid.
    >
    > Can you help with that?
    >
    > -Tim
    >
    >
    >
    > *** Sent via Developersdex http://www.developersdex.com ***
    > Don't just participate in USENET...get rewarded for it!
    Alvin Bruney, Jan 6, 2004
    #4
  5. Tim

    Tim Cavins Guest

    Here is the same code as posted in my first comment. The datagrid code
    is first, then below that is my OnEditCommand code.

    <asp:Datagrid... >
    <asp:BoundColumn ...>
    <asp:BoundColumn ...>
    <asp:templatecolumn>
    <itemtemplate>
    <asp:DataGrid runat="server" id="dgSub"
    OnEditCommand="dgSub_Edit"
    datasource='<%#
    ((DataRowView)Container.DataItem).Row.GetChildRows("GroupNameRelations")
    %>'>
    <asp:TemplateColumn HeaderText="Local Group name">
    <itemtemplate>
    <%# DataBinder.Evalontainer.DataItem, "LocalGroupName")
    %> </itemtemplate>
    <edititemtemplate>
    <asp:TextBox ID="txtLocalGroupName" Runat="server"
    text='<% DataBinder.Eval(Container.DataItem, "LocalGroupName") %>'
    /> </edititemtemplate>
    </asp:templatecolumn>
    <asp:templatecolumn>
    <itemtemplate>
    <asp:Button ID="btnLocalEdit" Runat="server"
    CommandName="Edit" Text="Edit" />
    </itemtemplate>
    <edititemtemplate>
    <asp:Button ID="btnLocalUpdate" Runat="server"
    CommandName="Update" Text="Update" />
    <asp:Button ID="btnLocalCancel" Runat="server"
    CommandName="Cancel" Text="Cancel" />
    </edititemtemplate>
    </asp:templatecolumn>
    </asp:datagrid>
    </asp:datagrid>

    The edit command currently looks like:

    protected void dgLocalGroups_Edit(object sender,
    DataGridCommandEventArgs e)
    {
    DataGrid dgLocalGroups =
    (DataGrid)dgGroups.FindControl("dgLocalGroups");
    // get the row that was clicked
    int intRow = e.Item.ItemIndex;
    dgLocalGroups.EditItemIndex = intRow;
    }

    -Tim


    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
    Tim Cavins, Jan 6, 2004
    #5
  6. In general, maybe taking a different approach would help. It cleared up a
    number of different problems, especially editing being able to edit the
    sub-grid.

    Put you nested DataGrid in a Web User Control and relate the Parent Grid to
    the Child Grid via properties. This way you get a seperate set of events
    for the Child Grid.

    Doug

    "Tim" <> wrote in message
    news:...
    > I have a datagrid that is nested inside of another datagrid. The code
    > is below.
    >
    > The datagrids display correctly, however, I cannot edit the inner
    > datagrid as I haven't been able to correctly reference it. The Edit
    > command is also below.
    >
    > How can I correctly reference the inner datagrid to edit it?
    >
    > Thanks,
    > Tim
    >
    >
    > <asp:Datagrid... >
    > <asp:BoundColumn ...>
    > <asp:BoundColumn ...>
    > <asp:TemplateColumn>
    > <ItemTemplate>
    > <asp:DataGrid runat="server" id="dgSub"
    > OnEditCommand="dgSub_Edit"
    > DataSource='<%#
    > ((DataRowView)Container.DataItem).Row.GetChildRows("GroupNameRelations")
    > %>'>
    > <asp:TemplateColumn HeaderText="Local Group Name">
    > <ItemTemplate>
    > <%# DataBinder.Evalontainer.DataItem, "LocalGroupName")
    > %> </ItemTemplate>
    > <EditItemTemplate>
    > <asp:TextBox ID="txtLocalGroupName" Runat="server"
    > Text='<% DataBinder.Eval(Container.DataItem, "LocalGroupName") %>'
    > /> </EditItemTemplate>
    > </asp:TemplateColumn>
    > <asp:TemplateColumn>
    > <ItemTemplate>
    > <asp:Button ID="btnLocalEdit" Runat="server"
    > CommandName="Edit" Text="Edit" />
    > </ItemTemplate>
    > <EditItemTemplate>
    > <asp:Button ID="btnLocalUpdate" Runat="server"
    > CommandName="Update" Text="Update" />
    > <asp:Button ID="btnLocalCancel" Runat="server"
    > CommandName="Cancel" Text="Cancel" />
    > </EditItemTemplate>
    > </asp:TemplateColumn>
    > </asp:DataGrid>
    > </asp:DataGrid>
    >
    > The edit command currently looks like:
    > protected void dgLocalGroups_Edit(object sender,
    > DataGridCommandEventArgs e)
    > {
    > DataGrid dgLocalGroups =
    > (DataGrid)dgGroups.FindControl("dgLocalGroups");
    > // get the row that was clicked
    > int intRow = e.Item.ItemIndex;
    > dgLocalGroups.EditItemIndex = intRow;
    > }
    Douglas J. Badin, Jan 9, 2004
    #6
    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. sajvargs
    Replies:
    0
    Views:
    373
    sajvargs
    Feb 20, 2005
  2. Replies:
    1
    Views:
    956
    =?Utf-8?B?RWx0b24gVw==?=
    Jan 25, 2006
  3. Andreas Klemt
    Replies:
    0
    Views:
    266
    Andreas Klemt
    Oct 8, 2003
  4. uk00121

    Editing a datagrid nested within another datagrid

    uk00121, Apr 14, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    2
    Views:
    137
    uk00121
    Apr 19, 2004
  5. John Dalberg

    How does a nested datagrid reference parent row in master datagrid?

    John Dalberg, Dec 20, 2005, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    213
    Elton Wang
    Dec 20, 2005
Loading...

Share This Page