Solved

ASP.Net 2.0 Gridview Inside a Gridview (Parent/Child)

Posted on 2006-11-20
13
950 Views
Last Modified: 2008-01-09
I have a gridview that returns the following results pulled from Table1:

ID
100
______________________
200
______________________
300
______________________

I need to add a column called 'Staff' that lists the names of people associated with the ID.  The staff names are in Table2.  Table1 and Table2 are linked by ID.

ID     Staff_Name
100   Joe Smith
        Jane Doe
________________________________
200    Bob Jones
         May Kay
________________________________
300   Alice Rite
        Lori Jones
        Al Johnson



How do I modify the following code to add the Staff column?

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="AccessDataSource1">
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" SortExpression="ID" />
            </Columns>
        </asp:GridView>
        <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="~/mydb.mdb"
            SelectCommand="SELECT [ID] FROM [Table1]"></asp:AccessDataSource>

Thanks for your help!
0
Comment
Question by:drees727
13 Comments
 
LVL 9

Expert Comment

by:kraffay
ID: 17983440
Here's a good example, it just uses a objectdatasource instead of the AccessDataSource.  

http://www.vbdotnetheaven.com/UploadFile/nikhil_be_it/GridViewInsideGridView05232006004636AM/GridViewInsideGridView.aspx
0
 
LVL 15

Expert Comment

by:Solar_Flare
ID: 17984664
the best way would be to change your query for the datasource so that it pulls the data from both tables at once

eg SELECT Table1.ID, Table2.Staff_Name FROM table1, table2 WHERE Table2.Staff_ID=table1.ID
0
 

Author Comment

by:drees727
ID: 17988847
Solar Flare - That won't work because I'd end up with the same record listed more than once.

kraffay - I looked at the code in your link and tried to dwindle it down since I'm not doing any editing or updating.  This is what I've ended up with but it's not pulling in the names.  Any suggestions?

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataIDNames="ID" DataSourceID="Accessdatasource1" >
            <Columns>
                <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" ReadOnly="True" SortExpression="ID" >
                </asp:BoundField>
                <asp:TemplateField HeaderText="Staff">
                    <ItemTemplate>
                        <asp:GridView ID="Gridview2" runat="server" AutoGeneratecolumns="True" DataIDNames="ID" DataSourceID="Accessdatasource2" >
                        </asp:GridView>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

     <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="mydb.mdb"
            SelectCommand="SELECT [ID] FROM [Table1]"></asp:AccessDataSource>
           
         <asp:AccessDataSource ID="AccessDataSource2" runat="server" DataFile="mydb.mdb"
            SelectCommand="SELECT [Name], [ID] FROM [TABLE2] Where ID = @ID">
           
        <SelectParameters>
          <asp:ControlParameter ControlID="GridView1" Name="ID" Type="String" />
        </SelectParameters>      
            </asp:AccessDataSource>
0
 
LVL 9

Expert Comment

by:kraffay
ID: 17989044
Do you code to handle the OnRowDatabound event of the the Parent Grid?  This is where you set the Select param of the child's data source
0
 

Author Comment

by:drees727
ID: 17990151
I don't have any OnRowDatabound event.  Apparently that's where I'm lost.  Help!  
0
 
LVL 9

Expert Comment

by:kraffay
ID: 17990694
You need something like this:

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

        Dim objListItem As DataControlRowState

        objListItem = e.Row.RowState

        Dim intMAsterID1 As Integer

        If e.Row.RowType = DataControlRowType.DataRow Then

            Dim grd As GridView

            If objListItem = DataControlRowState.Normal Or objListItem = DataControlRowState.Alternate Then

            grd = CType(e.Row.FindControl("Gridview2"), GridView)

            If grd IsNot Nothing Then

                grd.DataSourceID = ""

                grd.DataSource = ChildDataSource

                Dim intMAsterID1 As Integer

                MAsterID1 = Convert.ToInt32(CType(e.Row.DataItem, DataRowView).Row.ItemArray(0).ToString())

                AccessDataSource2.SelectParameters("ID").DefaultValue = intMAsterID1

                AccessDataSource2.Select()

                grd.DataBind()

            End If

        End If

      End If

End Sub

0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

by:drees727
ID: 17991331
Ok.  Bear with me.  I'm new at this.  I'm getting a 'Type datarowview is not defined' error.  Here is what I have now.  Assuming Master table ID is called 'ID1' and Child table ID is called 'ID2'.
****************************************************    

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        Dim objListItem As DataControlRowState
        objListItem = e.Row.RowState
        Dim intMAsterID1 As Integer
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim grd As GridView
            If objListItem = DataControlRowState.Normal Or objListItem = DataControlRowState.Alternate Then
                grd = CType(e.Row.FindControl("Gridview2"), GridView)
                If grd IsNot Nothing Then
                    grd.DataSourceID = ""
                    grd.DataSource = AccessDataSource2
                    Dim Key As Integer
                    ID1 = Convert.ToInt32(CType(e.Row.DataItem, DataRowView).Row.ItemArray(0).ToString())
                    intMAsterID1 = ID1
                    AccessDataSource2.SelectParameters("ID1").DefaultValue = intMAsterID1
                    AccessDataSource2.Select()
                    grd.DataBind()
                End If
            End If
        End If
    End Sub

My Gridview page:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="ID1" DataSourceID="Accessdatasource1" >
            <Columns>
                <asp:BoundField DataField="ID1" HeaderText="ID1" InsertVisible="False" ReadOnly="True" SortExpression="ID1" >
                </asp:BoundField>
                <asp:TemplateField HeaderText="Staff">
                    <ItemTemplate>
                        <asp:GridView ID="Gridview2" runat="server" AutoGeneratecolumns="False" DataKeyNames="ID2" DataSourceID="Accessdatasource2" >
            <Columns>
                                <asp:BoundField DataField="ID2" HeaderText="ID2" InsertVisible="False" ReadOnly="True" SortExpression="ID2" />
                                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            </Columns>
                        </asp:GridView>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

     <asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="mydb.mdb"
            SelectCommand="SELECT [ID1] FROM [Table1]"></asp:AccessDataSource>
           
         <asp:AccessDataSource ID="AccessDataSource2" runat="server" DataFile="mydb.mdb"
            SelectCommand="SELECT [Name], [ID2] FROM [Table2] Where ID2 = @ID1">
           
            <SelectParameters>
                <asp:Parameter Name="ID1" Type="Int32" />
            </SelectParameters>    
            </asp:AccessDataSource>
0
 
LVL 9

Expert Comment

by:kraffay
ID: 17991403
Try replace this line:

ID1 = Convert.ToInt32(CType(e.Row.DataItem, DataRowView).Row.ItemArray(0).ToString())

With this:

Dim dr as DataRowView = CType(e.Row.DataItem, DataRowView)
ID1 = Convert.ToInt32(dr("ID"))

Make sure you can compile before you try to run the app.

Good Luck!

http://www.integrityconsulting.net/blog/
0
 

Author Comment

by:drees727
ID: 17991701
That fixed that line but now I'm getting "Argument not specified for paramater arguments of Public Function..." on this line:

AccessDataSource2.Select()
0
 
LVL 9

Expert Comment

by:kraffay
ID: 17991851
Sorry, you're close, but I would have take your data and recreate your scenario to get this working.  Try taking another look at the sample.
0
 

Author Comment

by:drees727
ID: 18031834
I couldn't get the above sample to work.  I'm using the following to accomplish what I need.  Everying is working well now.
http://www.codeproject.com/useritems/SkinSample.asp
0
 
LVL 1

Accepted Solution

by:
DarthMod earned 0 total points
ID: 18461973
PAQed with points refunded (125)

DarthMod
Community Support Moderator
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now