Nested DataGrids

Discussion in 'ASP .Net Datagrid Control' started by Vishal, Nov 19, 2004.

  1. Vishal

    Vishal Guest

    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, Nov 19, 2004
    #1
    1. Advertising

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

    http://www.4GuysFromRolla.com

    * When you think ASP.NET, think 4GuysFromRolla.com!
     
    Scott Mitchell [MVP], Nov 20, 2004
    #2
    1. Advertising

  3. Vishal

    Vishal Guest

    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


    >-----Original Message-----
    >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
    >
    > http://www.4GuysFromRolla.com
    >
    >* When you think ASP.NET, think 4GuysFromRolla.com!
    >.
    >
     
    Vishal, Nov 20, 2004
    #3
  4. Vishal

    Vishal Guest

    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
    >
    >
    >>-----Original Message-----
    >>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
    >>
    >> http://www.4GuysFromRolla.com
    >>
    >>* When you think ASP.NET, think 4GuysFromRolla.com!
    >>.
    >>

    >.
    >
     
    Vishal, Nov 20, 2004
    #4
  5. Vishal

    Vishal Guest

    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

    >-----Original Message-----
    >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
    >>
    >>
    >>>-----Original Message-----
    >>>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
    >>>
    >>> http://www.4GuysFromRolla.com
    >>>
    >>>* When you think ASP.NET, think 4GuysFromRolla.com!
    >>>.
    >>>

    >>.
    >>

    >.
    >
     
    Vishal, Nov 20, 2004
    #5
  6. Vishal

    Vishal Guest

    Please help me scott if you can. I am totally stuck since
    hours.


    >-----Original Message-----
    >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
    >
    > http://www.4GuysFromRolla.com
    >
    >* When you think ASP.NET, think 4GuysFromRolla.com!
    >.
    >
     
    Vishal, Nov 20, 2004
    #6
  7. Vishal

    Vishal Guest

    Can somebody help here please?

    Vishal.

    >-----Original Message-----
    >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
    >
    > http://www.4GuysFromRolla.com
    >
    >* When you think ASP.NET, think 4GuysFromRolla.com!
    >.
    >
     
    Vishal, Nov 21, 2004
    #7
    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. Juliano Bortolozzi

    Re: Nested Datagrids

    Juliano Bortolozzi, Jul 14, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    428
    Juliano Bortolozzi
    Jul 14, 2003
  2. Dave

    Nested DataGrids

    Dave, Jul 15, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    628
    S. Justin Gengo
    Jul 15, 2003
  3. Dave

    Nested DataGrids

    Dave, Jul 21, 2003, in forum: ASP .Net
    Replies:
    1
    Views:
    395
    Ray Dixon [MVP]
    Jul 21, 2003
  4. Dave T

    Edit Nested Datagrids

    Dave T, Nov 3, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    403
    Dave T
    Nov 3, 2003
  5. ProgramAnalyst

    nested datagrids

    ProgramAnalyst, Nov 4, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    443
    ProgramAnalyst
    Nov 5, 2004
Loading...

Share This Page