Groups | Blog | Home
all groups > asp.net datagrid control > november 2004 >

asp.net datagrid control : Nested DataGrids


Vishal
11/19/2004 2:18:15 PM
Hello,

I have created a nested datagrid with the help of this
article: http://www.standardio.org/article.aspx?id=155.
Now I need to edit the nested datagrid. I thought that it
was the same task as editing a single datagrid, but it
doesnt seem so. I created a simple edit handler for my
nested datagrid and inserted the following code:

Sub dgNested_Edit(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
dgNested.EditItemIndex = e.Item.ItemIndex
end sub

However I get the error that dgNested is not declared,
although it is. I guess I have to use somehow findcontrol
or anything else to get this working. Anybody has any
experience with editing nested Datagrids?

Thanks
Vishal
11/19/2004 6:38:16 PM
Thanks for the reply Scott. Yes I am doing it this way.
When I use your code and press the edit button then I get
the following error:

System.InvalidCastException: Specified cast is not valid.

What can I do here?

Vishal


[quoted text, click to view]
Vishal
11/19/2004 6:43:04 PM
BTW: Isnt the sender the datagrid?


[quoted text, click to view]
Vishal
11/19/2004 6:54:26 PM
Scott,

I have this code now and it works partially.

Sub dgNested_Edit(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
Dim myInnerDG as DataGrid = sender
response.write(sender.ID)
'Dim myEditButton as Button = CType(sender, Button)
'myInnerDG = CType(myEditButton.Parent.Parent.Parent,
DataGrid)

myInnerDG.EditItemIndex = e.Item.ItemIndex

end sub

However the thing is that when I press the first time the
button then I dont see the datagrid in edit mode. Although
the sender.ID is printer. Now when I press the second
time, then the datagrid is in edit mode. I dont understand
this. Basically the datagrid goes in edit mode when I
click the edit button twice. What can cause such a
behaviour?

Thanks

[quoted text, click to view]
Vishal
11/19/2004 7:27:50 PM
Please help me scott if you can. I am totally stuck since
hours.


[quoted text, click to view]
Scott Mitchell [MVP]
11/20/2004 2:26:12 AM
Vishal, are you wiring up the EditCommand to the event handler in your
declarative syntax, like so?

<asp:DataGrid id="OutterDataGrid" ...>
<Columns>
...
<asp:TemplateColumn>
<ItemTemplate>
<asp:DataGrid id="InnerDataGrid" OnEditCommand="dgNested_Edit"
.... />
</ItemTemplate>
</asp:TemplateColumn>
...
</Columns>
</asp:DataGrid>

???

Now, you won't be able to access dgNested directly from the EditCommand
event handler, but the sender parameter passed into the event handler
is, IIRC, the Edit button. Now, you can get to the DataGrid like so
(untested code follows):

Sub dgNested_Edit(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
Dim myInnerDG as DataGrid
Dim myEditButton as Button = CType(sender, Button)

myInnerDG = CType(myEditButton.Parent.Parent.Parent, DataGrid)

myInnerDG.EditItemIndex = ...
end sub


The Button's Parent is the TableCell it exists in. It's Parent is the
DataGridItem. And the DataGridItem's parent is the DataGrid. That's
why we do .Parent.Parent.Parent. Hope this makes sense! :-)


--

Scott Mitchell
mitchell@4guysfromrolla.com
http://www.4GuysFromRolla.com

Vishal
11/20/2004 6:41:26 PM
Can somebody help here please?

Vishal.

[quoted text, click to view]
Scott Mitchell [MVP]
11/21/2004 2:18:39 AM
Looks like you got it! My bad on the Sender being the Button. This is
the case if you are using a CheckBox or Button in the TemplateColumn
whose event is wired up to an event handler. But in the ItemCommand
it's the DataGrid, as you noted. Jolly good show.

Cheers.

--

Scott Mitchell
mitchell@4guysfromrolla.com
http://www.4GuysFromRolla.com

AddThis Social Bookmark Button