Nested DataGrids

V

Vishal

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
 
S

Scott Mitchell [MVP]

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
(e-mail address removed)
http://www.4GuysFromRolla.com

* When you think ASP.NET, think 4GuysFromRolla.com!
 
V

Vishal

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
 
V

Vishal

BTW: Isnt the sender the datagrid?

-----Original Message-----
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

(myEditButton.Parent.Parent.Parent,
.
 
V

Vishal

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
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top