Nested Datagrid reference?

T

Tim

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;
}
 
A

Alvin Bruney

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?
 
T

Tim Cavins

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
 
A

Alvin Bruney

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.
 
T

Tim Cavins

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
 
D

Douglas J. Badin

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,810
Latest member
Kassie0918

Latest Threads

Top