drees727
asked on
ASP.Net 2.0 Gridview Inside a Gridview (Parent/Child)
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="AccessDataSo urce1">
<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:AccessData Source>
Thanks for your help!
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
<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:AccessData
Thanks for your help!
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
eg SELECT Table1.ID, Table2.Staff_Name FROM table1, table2 WHERE Table2.Staff_ID=table1.ID
ASKER
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="Accessdataso urce1" >
<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="Accessdataso urce2" >
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="mydb.mdb"
SelectCommand="SELECT [ID] FROM [Table1]"></asp:AccessData Source>
<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>
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
<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"
</asp:GridView>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:AccessDataSource ID="AccessDataSource1" runat="server" DataFile="mydb.mdb"
SelectCommand="SELECT [ID] FROM [Table1]"></asp:AccessData
<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>
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
ASKER
I don't have any OnRowDatabound event. Apparently that's where I'm lost. Help!
You need something like this:
Protected Sub GridView1_RowDataBound(ByV al sender As Object, ByVal e As System.Web.UI.WebControls. GridViewRo wEventArgs ) 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.Altern ate Then
grd = CType(e.Row.FindControl("G ridview2") , GridView)
If grd IsNot Nothing Then
grd.DataSourceID = ""
grd.DataSource = ChildDataSource
Dim intMAsterID1 As Integer
MAsterID1 = Convert.ToInt32(CType(e.Ro w.DataItem , DataRowView).Row.ItemArray (0).ToStri ng())
AccessDataSource2.SelectPa rameters(" ID").Defau ltValue = intMAsterID1
AccessDataSource2.Select()
grd.DataBind()
End If
End If
End If
End Sub
Protected Sub GridView1_RowDataBound(ByV
Dim objListItem As DataControlRowState
objListItem = e.Row.RowState
Dim intMAsterID1 As Integer
If e.Row.RowType = DataControlRowType.DataRow
Dim grd As GridView
If objListItem = DataControlRowState.Normal
grd = CType(e.Row.FindControl("G
If grd IsNot Nothing Then
grd.DataSourceID = ""
grd.DataSource = ChildDataSource
Dim intMAsterID1 As Integer
MAsterID1 = Convert.ToInt32(CType(e.Ro
AccessDataSource2.SelectPa
AccessDataSource2.Select()
grd.DataBind()
End If
End If
End If
End Sub
ASKER
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(ByV al sender As Object, ByVal e As System.Web.UI.WebControls. GridViewRo wEventArgs ) 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.Altern ate Then
grd = CType(e.Row.FindControl("G ridview2") , GridView)
If grd IsNot Nothing Then
grd.DataSourceID = ""
grd.DataSource = AccessDataSource2
Dim Key As Integer
ID1 = Convert.ToInt32(CType(e.Ro w.DataItem , DataRowView).Row.ItemArray (0).ToStri ng())
intMAsterID1 = ID1
AccessDataSource2.SelectPa rameters(" ID1").Defa ultValue = 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="Accessdataso urce1" >
<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="Accessdataso urce2" >
<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:AccessData Source>
<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>
**************************
Protected Sub GridView1_RowDataBound(ByV
Dim objListItem As DataControlRowState
objListItem = e.Row.RowState
Dim intMAsterID1 As Integer
If e.Row.RowType = DataControlRowType.DataRow
Dim grd As GridView
If objListItem = DataControlRowState.Normal
grd = CType(e.Row.FindControl("G
If grd IsNot Nothing Then
grd.DataSourceID = ""
grd.DataSource = AccessDataSource2
Dim Key As Integer
ID1 = Convert.ToInt32(CType(e.Ro
intMAsterID1 = ID1
AccessDataSource2.SelectPa
AccessDataSource2.Select()
grd.DataBind()
End If
End If
End If
End Sub
My Gridview page:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False
<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
<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:AccessData
<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>
Try replace this line:
ID1 = Convert.ToInt32(CType(e.Ro w.DataItem , DataRowView).Row.ItemArray (0).ToStri ng())
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/
ID1 = Convert.ToInt32(CType(e.Ro
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/
ASKER
That fixed that line but now I'm getting "Argument not specified for paramater arguments of Public Function..." on this line:
AccessDataSource2.Select()
AccessDataSource2.Select()
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.
ASKER
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
http://www.codeproject.com/useritems/SkinSample.asp
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
http://www.vbdotnetheaven.com/UploadFile/nikhil_be_it/GridViewInsideGridView05232006004636AM/GridViewInsideGridView.aspx