Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

asp.net nested gridview

Options
  • 11-08-2008 6:07pm
    #1
    Closed Accounts Posts: 317 ✭✭


    Hi All,
    Hopefully an easy one.

    I have a gridview. I have a template field as one of the columns.
    Within this template field I have another gridview.

    So, when the page loads, I select a row from the gridview, then, the template column appears with the other gridview within the gridview.

    It works fine but the child gridview is appearing on ALL rows, not just the one I selected.
    It's not a problem with SQL, that's all I know for sure.

    If anyone can point me in the right direction would be great. Thanks


Comments

  • Moderators, Science, Health & Environment Moderators Posts: 8,952 Mod ✭✭✭✭mewso


    Are you using code in the selectedindexchanged event? If so post the code. If not then then the asp.net html that makes it all happen.


  • Closed Accounts Posts: 317 ✭✭tiptap


    musician wrote: »
    Are you using code in the selectedindexchanged event? If so post the code. If not then then the asp.net html that makes it all happen.

    Howdy,

    Nope, I'm not using any code in the selectedindexchanged event.

    The child gridview has a datasource with a parameter which is set to the parent gridviews selected value.


  • Moderators, Science, Health & Environment Moderators Posts: 8,952 Mod ✭✭✭✭mewso


    If you look at the html you'll see that the nested gridview has a datasourceid matching the id of the datasource. Remove that then in the selectedindexchanged event for the main gridview you'll need to find the gridview and it's accompanying datasource in the selected row:-

    Dim gview As GridView = CType(GridView1.Rows(GridView1.SelectedIndex).Cells(2).FindControl("GridView2"), GridView)
    Dim sqlds As SqlDataSource = CType(GridView1.Rows(GridView1.SelectedIndex).Cells(2).FindControl("SqlDataSource2"), SqlDataSource)
    gview.DataSourceID = sqlds.ID
    gview.DataBind()

    I'm assuming the main gridview is called gridview1, the nested gridview is called gridview2, the nested datasource is called sqldatasource2 and the nested grid is in the third column (cells(2)). This should ensure that only the selected row will should a databound nested grid.


  • Closed Accounts Posts: 317 ✭✭tiptap


    musician wrote: »
    If you look at the html you'll see that the nested gridview has a datasourceid matching the id of the datasource. Remove that then in the selectedindexchanged event for the main gridview you'll need to find the gridview and it's accompanying datasource in the selected row:-

    Dim gview As GridView = CType(GridView1.Rows(GridView1.SelectedIndex).Cells(2).FindControl("GridView2"), GridView)
    Dim sqlds As SqlDataSource = CType(GridView1.Rows(GridView1.SelectedIndex).Cells(2).FindControl("SqlDataSource2"), SqlDataSource)
    gview.DataSourceID = sqlds.ID
    gview.DataBind()

    I'm assuming the main gridview is called gridview1, the nested gridview is called gridview2, the nested datasource is called sqldatasource2 and the nested grid is in the third column (cells(2)). This should ensure that only the selected row will should a databound nested grid.

    thanks for your help on this.

    ok, my html now looks like this after taking out the parameterisation

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    DataKeyNames="competition_id" DataSourceID="AccessDataSource1"
    style="font-family: Tahoma; font-size: small" Width="662px">
    <Columns>
    <asp:CommandField ShowSelectButton="True" />
    <asp:BoundField DataField="competition_id" HeaderText="competition_id"
    InsertVisible="False" ReadOnly="True" SortExpression="competition_id"
    Visible="False" />
    <asp:BoundField DataField="competition_desc" HeaderText="Competitions" />
    <asp:TemplateField>
    <ItemTemplate>
    <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
    AutoGenerateEditButton="True" DataKeyNames="division_id"
    DataSourceID="AccessDataSource2" style="font-family: Tahoma; font-size: small">
    <Columns>
    <asp:CommandField ShowSelectButton="True" />
    <asp:BoundField DataField="division_id" HeaderText="division_id"
    InsertVisible="False" ReadOnly="True" SortExpression="division_id"
    Visible="False" />
    <asp:BoundField DataField="division_desc" HeaderText="Divisions"
    SortExpression="division_desc" />
    </Columns>
    </asp:GridView>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    <SelectedRowStyle Font-Bold="False" Font-Italic="False" />
    </asp:GridView>




    my selectedindexchanged looks like this, I changed it to accessdatasource as I'm using access just for the minute

    Dim gview As GridView = CType(GridView1.Rows(GridView1.SelectedIndex).Cells(3).FindControl("GridView2"), GridView)
    Dim sqlds As AccessDataSource = CType(GridView1.Rows(GridView1.SelectedIndex).Cells(2).FindControl("accessDataSource2"), AccessDataSource)
    gview.DataSourceID = sqlds.ID
    gview.DataBind()


    It's throwing an error on gview.datasouceID=sqlds.ID saying object reference not set.

    I'm stumped ! I tried the microsoft way of datarouwbound and getting the same error also.


  • Moderators, Science, Health & Environment Moderators Posts: 8,952 Mod ✭✭✭✭mewso


    For starters I have highlighted the bit I meant you to remove.
    tiptap wrote: »



    <asp:GridViewID="GridView2"runat="server"AutoGenerateColumns="False"
    AutoGenerateEditButton="True"DataKeyNames="division_id"
    DataSourceID="AccessDataSource2"style="font-family: Tahoma; font-size: small">

    and the wrong cell index is referenced her. Should be 3.
    tiptap wrote: »
    Dim sqlds As AccessDataSource = CType(GridView1.Rows(GridView1.SelectedIndex).Cells(2).FindControl("accessDataSource2"), AccessDataSource)


  • Advertisement
  • Closed Accounts Posts: 317 ✭✭tiptap


    musician wrote: »
    For starters I have highlighted the bit I meant you to remove.


    and the wrong cell index is referenced her. Should be 3.

    Unfortunately still the same error....here are my ammendmants

    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
    Dim gview As GridView = CType(GridView1.Rows(GridView1.SelectedIndex).Cells(3).FindControl("GridView2"), GridView)
    Dim sqlds As AccessDataSource = CType(GridView1.Rows(GridView1.SelectedIndex).Cells(3).FindControl("accessDataSource2"), AccessDataSource)
    gview.DataSourceID = sqlds.ID
    gview.DataBind()
    End Sub




    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    DataKeyNames="competition_id" DataSourceID="AccessDataSource1"
    style="font-family: Tahoma; font-size: small" Width="662px">
    <Columns>
    <asp:CommandField ShowSelectButton="True" />
    <asp:BoundField DataField="competition_id" HeaderText="competition_id"
    InsertVisible="False" ReadOnly="True" SortExpression="competition_id"
    Visible="False" />
    <asp:BoundField DataField="competition_desc" HeaderText="Competitions" />
    <asp:TemplateField>
    <ItemTemplate>
    <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
    AutoGenerateEditButton="True"
    style="font-family: Tahoma; font-size: small">
    <Columns>
    <asp:CommandField ShowSelectButton="True" />
    <asp:BoundField DataField="division_id" HeaderText="division_id"
    InsertVisible="False" ReadOnly="True" SortExpression="division_id"
    Visible="False" />
    <asp:BoundField DataField="division_desc" HeaderText="Divisions"
    SortExpression="division_desc" />
    </Columns>
    </asp:GridView>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    <SelectedRowStyle Font-Bold="False" Font-Italic="False" />
    </asp:GridView>


  • Moderators, Science, Health & Environment Moderators Posts: 8,952 Mod ✭✭✭✭mewso


    You seem to have removed the accessdatasource2 from the html. I didn't mean that. As I pointed out above by removing the specific property of the gridview, datasourceid="accessdatasource2", it will not databind to the accessdatasource2 at the start but we need the actual accessdatasource there so we can databind to it later. That is why we do it in our code to restrict it to the currently selected row.

    Not to sound too preachy but it's worth taking the time to understand whats going on here so you can better solve your own problems over time. I have never done what I am trying to tell you to do so fingers crossed it will work but the point being I thought of the solution from an understanding of whats going on in the background. Anyway give that a try and I'll try and remove the headmaster's cap.


  • Closed Accounts Posts: 317 ✭✭tiptap


    musician wrote: »
    You seem to have removed the accessdatasource2 from the html. I didn't mean that. As I pointed out above by removing the specific property of the gridview, datasourceid="accessdatasource2", it will not databind to the accessdatasource2 at the start but we need the actual accessdatasource there so we can databind to it later. That is why we do it in our code to restrict it to the currently selected row.

    Not to sound too preachy but it's worth taking the time to understand whats going on here so you can better solve your own problems over time. I have never done what I am trying to tell you to do so fingers crossed it will work but the point being I thought of the solution from an understanding of whats going on in the background. Anyway give that a try and I'll try and remove the headmaster's cap.

    No worries at all about the headmaster's cap :-)

    I ain't really a developer by any means ! (as I'm sure you have gathered)..
    Still can't get the darn thing working but I'll carry on


  • Moderators, Science, Health & Environment Moderators Posts: 8,952 Mod ✭✭✭✭mewso


    tiptap wrote: »
    No worries at all about the headmaster's cap :-)

    I ain't really a developer by any means ! (as I'm sure you have gathered)..
    Still can't get the darn thing working but I'll carry on

    What have you got so far?


  • Closed Accounts Posts: 317 ✭✭tiptap


    musician wrote: »
    What have you got so far?

    pretty much the same thing, I tried to add back in the part I deleted but still no joy, I changed the cell value to 3 but it's a no go as well...

    Just reading up on that error on google now,
    I have both my html and full vb below.



    <%
    Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
    <
    html xmlns="http://www.w3.org/1999/xhtml"&gt;
    <
    head runat="server">
    <title>Untitled Page</title>
    <style type="text/css">
    .style1
    {
    width: 100%;
    height: 368px;
    }
    .style2
    {
    width: 253px;
    }
    </style>
    </
    head>
    <
    body>
    <form id="form1" runat="server">
    <div>

    <asp:ScriptManager ID="ScriptManager1" runat="server">
    </asp:ScriptManager>
    <asp:UpdatePanel ID="UpdatePanel1" runat="server">
    <ContentTemplate>
    <asp:AccessDataSource ID="AccessDataSource1" runat="server"
    DataFile="~/App_Data/comps.mdb" SelectCommand="SELECT * FROM [tbl_competition]">
    </asp:AccessDataSource>
    <asp:AccessDataSource ID="AccessDataSource2" runat="server"
    DataFile="~/App_Data/comps.mdb"
    SelectCommand="SELECT [division_id], [division_desc] FROM [tbl_division] WHERE ([competition_id] = ?)">
    <SelectParameters>
    <asp:Parameter Name="competition_id" Type="Int32" />
    </SelectParameters>
    </asp:AccessDataSource>
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    DataKeyNames="competition_id" DataSourceID="AccessDataSource1"
    style="font-family: Tahoma; font-size: small" Width="662px">
    <Columns>
    <asp:CommandField ShowSelectButton="True" />
    <asp:BoundField DataField="competition_id" HeaderText="competition_id"
    InsertVisible="False" ReadOnly="True" SortExpression="competition_id"
    Visible="False" />
    <asp:BoundField DataField="competition_desc" HeaderText="Competitions" />
    <asp:TemplateField>
    <ItemTemplate>
    <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False"
    AutoGenerateEditButton="True" DataKeyNames="division_id"
    style="font-family: Tahoma; font-size: small">
    <Columns>
    <asp:CommandField ShowSelectButton="True" />
    <asp:BoundField DataField="division_id" HeaderText="division_id"
    InsertVisible="False" ReadOnly="True" SortExpression="division_id"
    Visible="False" />
    <asp:BoundField DataField="division_desc" HeaderText="Divisions"
    SortExpression="division_desc" />
    </Columns>
    </asp:GridView>
    </ItemTemplate>
    </asp:TemplateField>
    </Columns>
    <SelectedRowStyle Font-Bold="False" Font-Italic="False" />
    </asp:GridView>
    <br />
    <table class="style1">
    <tr>
    <td class="style2" valign="top">
     </td>
    </tr>
    </table>
    </ContentTemplate>
    </asp:UpdatePanel>
    <br />

    </div>
    </form>
    </
    body>
    </
    html>






    Partial Class _Default
    Inherits System.Web.UI.Page



    Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.SelectedIndexChanged
    Dim gview As GridView = CType(GridView1.Rows(GridView1.SelectedIndex).Cells(3).FindControl("GridView2"), GridView)
    Dim sqlds As New AccessDataSource
    sqlds =
    CType(GridView1.Rows(GridView1.SelectedIndex).Cells(3).FindControl("accessDataSource2"), AccessDataSource)
    gview.DataSourceID = sqlds.ID
    gview.DataBind()
    End Sub
    End
    Class


  • Advertisement
  • Moderators, Science, Health & Environment Moderators Posts: 8,952 Mod ✭✭✭✭mewso


    Theres still no accessdatasource2 in that html. Edit your template again and setup the datasource of the nested gridview then go into the html and remove the datasourceid="accessdatasource2" property in the gridview.


Advertisement