locked
Nested Gridview and Cell Values RRS feed

  • Question

  • User2053033320 posted

    Hello .NET Experts,

    I got a gridview within a gridview. I need to get the datakey (CarID) on my 

    Protected Sub LinkBtnSelectEditCar_Click(sender As Object, e As System.EventArgs)
            Dim LinkButtonSelectEditCar As LinkButton = TryCast(sender, LinkButton)
            Dim GridViewCarsForLocations As GridView = DirectCast(LinkButtonSelectEditCar.NamingContainer, GridView)
            Dim Row As GridViewRow = DirectCast(LinkButtonSelectEditCar.NamingContainer, GridViewRow)
    
            Session("CarIDSelected") = GridViewCarsForLocations.DataKeys(Row.RowIndex).Value
            Session("CarNameSelected") = Row.Cells(0).Text
            Response.Redirect("editCar.aspx")
        End Sub
    
    
    

     <asp:GridView ID="GridViewLocations" runat="server" CssClass="table table-striped table-hover" AutoGenerateColumns="False" PageSize="100" ShowHeader="true" BackColor="White" BorderWidth="0px" GridLines="None" DataKeyNames="LocationID">
            <Columns>
                <asp:TemplateField HeaderText="Locations">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkBtnSelectEditLocation" OnClick="LinkBtnSelectEditLocation_Click" Font-Underline="false" runat="server" Text='<%#Eval("LocationName") %>' ToolTip="Click here to edit location information"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
    
                <asp:TemplateField HeaderText="Cars">
                    <ItemTemplate>
                        <asp:GridView runat="server" ID="GridViewCarsForLocation" DataKeyNames="CarID" CssClass="table table-striped table-hover" AutoGenerateColumns="False" ShowHeader="false">
                            <Columns>
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:LinkButton ID="LinkBtnSelectCarsPlant" OnClick="LinkBtnSelectEditcar_Click" Font-Underline="false" runat="server" Text='<%#Eval("carName") %>' ToolTip="Click here to edit car information"></asp:LinkButton>
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>
                </ItemTemplate>
                </asp:TemplateField>
    
            </Columns>
        </asp:GridView>

    The error reads (translated):

    The object of type System.Web.UI.WebControls.GridViewRow can not be converted to System.Web.UI.WebControls.GridView

    I think it is caused by this line:


    Dim GridViewCarsForLocations As GridView = DirectCast(LinkButtonSelectEditCar.NamingContainer, GridView)

    I dont know how to interpret the error since I am not try to convert but rather find the GridView (parent) of the LinkButton that fired the event.

    Wednesday, January 4, 2017 12:25 PM

Answers

  • User-1716253493 posted

    Hi metallon, sorry i have not test it before

    You can use double parent like this

    Dim GridViewCarsForLocations As GridView = DirectCast(LinkButtonSelectEditCar.NamingContainer.Parent.Parent, GridView)

    I have test the latest code

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 6, 2017 12:32 AM

All replies

  • User-1716253493 posted

    The NamingContainer of the LinkButton is GridViewRow

    AFAIK, the parent of the gridviewrow is the grid, so try using .Parent after getting gridviewrow.

    Try this

    Dim GridViewCarsForLocations As GridView = DirectCast(LinkButtonSelectEditCar.NamingContainer.Parent, GridView)

    or

    Get the row first

    Dim TheRow As GridViewRow = DirectCast(LinkButtonSelectEditCar.NamingContainer, GridViewRow)
    Dim GridViewCarsForLocations As GridView = DirectCast(TheRow.Parent, GridView)

    From gridviewrow, you can get rowindex

    From the grid you can get datakey value based rowindex.

    gvcfl.datakeys(idx).value

    Another way you can use hiddenfield then bind it to carid, you can get it by row.findcontrol("hiddenfieldcarid")

    or you can use another cell value, row.cells(n).text

    Thursday, January 5, 2017 1:08 AM
  • User2053033320 posted

    Hi oned_gk
    Thanks for your reply.
    When I try

    Dim GridViewCarsForLocations As GridView = DirectCast(LinkButtonSelectEditCar.NamingContainer.Parent, GridView)

    I get the error:

    Object of type "System.Web.UI.WebControls.ChildTable"cannot be converted to type"System.Web.UI.WebControls.GridView".

    The same happens when I use your second suggestion.

    Thursday, January 5, 2017 8:52 AM
  • User-271186128 posted

    Hi metallon,

    Please use FindControl("GridViewCarsForLocation") to get the gridview in the NamingContainer of  the linkbutton.

    I modify your code to this. It’s worked.

    Dim GridViewCarsForLocations As GridView = DirectCast(LinkButtonSelectEditCar.NamingContainer.FindControl("GridViewCarsForLocation"), GridView)  

    Best regards,
    Billy

    Thursday, January 5, 2017 10:07 AM
  • User2053033320 posted

    Hi Billy,

    Thanks for your reply. I get the error "Object reference not set to an instance of an object."

         Dim GridViewCarsForLocations As GridView = DirectCast(LinkButtonSelectEditCar.NamingContainer.FindControl("GridViewCarsForLocation"), GridView

    Does not seem to find a control.

    asp:GridView ID="GridViewLocations" runat="server" CssClass="table table-striped table-hover" AutoGenerateColumns="False" PageSize="100" ShowHeader="true" BackColor="White" BorderWidth="0px" GridLines="None" DataKeyNames="LocationID">
            <Columns>
                <asp:TemplateField HeaderText="Locations">
                    <ItemTemplate>
                        <asp:LinkButton ID="LinkBtnSelectEditLocation" OnClick="LinkBtnSelectEditLocation_Click" Font-Underline="false" runat="server" Text='<%#Eval("LocationName") %>' ToolTip="Click here to edit location information"></asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
    
                <asp:TemplateField HeaderText="Cars">
                    <ItemTemplate>
                        <asp:GridView runat="server" ID="GridViewCarsForLocation" DataKeyNames="CarID" CssClass="table table-striped table-hover" AutoGenerateColumns="False" ShowHeader="false">
                            <Columns>
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <asp:LinkButton ID="LinkBtnSelectEditCar" OnClick="LinkBtnSelectEditCar_Click" Font-Underline="false" runat="server" Text='<%#Eval("CarName") %>' ToolTip="Click here to edit Car information"></asp:LinkButton>
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>
                        <asp:LinkButton ID="LinkBtnSelectAddCarLocation" OnClick="LinkBtnAddCar_Click" Font-Underline="false" runat="server" ToolTip="Click here to add a new Car to this location">New Car</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateField>
    
            </Columns>
        </asp:GridView>
    
    
    
        Protected Sub LinkBtnSelectEditCar_Click(sender As Object, e As System.EventArgs)
            Dim LinkButtonSelectEditCar As LinkButton = TryCast(sender, LinkButton)
    
            Dim Row As GridViewRow = DirectCast(LinkButtonSelectEditCar.NamingContainer, GridViewRow)
    
    
            '--debugger shows this "nothing"
            Dim GridViewCarsForLocations As GridView = DirectCast(LinkButtonSelectEditCar.NamingContainer.FindControl("GridViewCarsForLocation"), GridView)
    
            '----the following line cause the error
            Session("CarIDSelected") = GridViewCarsForLocations.DataKeys(Row.RowIndex).Value
    
    
    
            Session("CarNameSelected") = Row.Cells(0).Text
            Response.Redirect("editCar.aspx")
        End Sub

    Thursday, January 5, 2017 10:52 AM
  • User-1716253493 posted

    Hi metallon, sorry i have not test it before

    You can use double parent like this

    Dim GridViewCarsForLocations As GridView = DirectCast(LinkButtonSelectEditCar.NamingContainer.Parent.Parent, GridView)

    I have test the latest code

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 6, 2017 12:32 AM
  • User2053033320 posted

    Hi oned_gk,
    Thanks for that. It worked but I wonder now how I can go even one level higher and get
    the label text for the CarLocation that the card is part of.
    meaning one level higher in the other gridview.
    is that possible? I tried parent.parent.parent and .parent but that did not work :)
    basically it is great to have the car selected for editing but I need the location
    of the car as well. I tried using the datakeys that I defined for the location but
    that did not work. I always got the wrong ID back.

    Friday, January 6, 2017 4:27 PM