Master/Detail record...how to display in data grid

P

PatLaf

Hello to all...I have two tables that I retrieve on page load. I bind the master table to the datagrid and I have added the column to display the detail record but I need help with the datagrid I think. The list in the datagrid exceeds the page length...I can't use paging I have to show them all it is for our auditors, I would like to be able to display the list of checkboxes displaying the detail records directly underneath the row where the user clicked the show details button. I have an example from Dino Espositos book Building Web solutions with asp.net and ado.net but unfortunately I am new to vb.net and the example is in c# which I have even less experience with. Does anyone have a sample or know where I can look at something close to what I want to do?

Thanks in advance,

Pat
 
J

Jeffrey Tan[MSFT]

Hi Pat,

Thank you for posting in the community!

Based on my understanding, you use datagrid to display Master/Details
datatables. You use an extra column to show the details records.

======================================================
I have writen a sample project for this issue, in it, I use SqlServer's
default datatable "Categories" and "Products" in "Northwind" as datasouce.

Based on my research, I use an extra column to include an invisible
datagrid to show the details table. Html view like this:

<asp:datagrid id="Master" style="Z-INDEX: 101; LEFT: 48px; POSITION:
absolute; TOP: 24px" runat="server"
Width="480px" Height="304px" AutoGenerateColumns="False">
<Columns>
<asp:BoundColumn DataField="CategoryID"></asp:BoundColumn>
<asp:BoundColumn DataField="CategoryName"></asp:BoundColumn>
<asp:BoundColumn DataField="Description"></asp:BoundColumn>
<asp:ButtonColumn Text="Show Details"
ButtonType="PushButton"></asp:ButtonColumn>
<asp:TemplateColumn Visible="False">
<ItemTemplate>
<asp:DataGrid Runat="server" ID="details"
Visible="False"></asp:DataGrid>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:datagrid>



Then, when user click the "Show details" button, I en-visible that column
and the details datagrid, code below:

Dim ds As Dataset1
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
If Not IsPostBack Then
getsource()
masterdatabind()
End If
End Sub

Public Sub getsource()
Dim adapter1 As SqlDataAdapter = New SqlDataAdapter("select * from
Products", "server=localhost;database=Northwind;uid=sa;pwd=")
Dim adapter2 As SqlDataAdapter = New SqlDataAdapter("select * from
Categories", "server=localhost;database=Northwind;uid=sa;pwd=")
ds = New Dataset1
adapter2.Fill(ds, "Categories")
adapter1.Fill(ds, "Products")
End Sub

Public Sub masterdatabind()
Master.DataSource = ds.Categories
Master.DataBind()
End Sub

Private Sub Master_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
Master.ItemDataBound
Dim dgi As DataGridItem = e.Item
If dgi.ItemType = ListItemType.Item Or dgi.ItemType =
ListItemType.AlternatingItem Then
Dim c As Control
For Each c In dgi.Cells(4).Controls
If TypeOf (c) Is System.Web.UI.WebControls.DataGrid Then
Dim id As Int32 = Int32.Parse(dgi.Cells(0).Text)
Dim dg As DataGrid = CType(c, DataGrid)
Dim dr As DataRow()
dr = ds.Products.Select("CategoryID=" & id)
dg.DataSource = dr
dg.DataBind()
End If
Next
End If
End Sub

Private Sub Master_ItemCommand(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridCommandEventArgs) Handles
Master.ItemCommand
If CType(e.CommandSource, Button).Text = "Show Details" Then
Master.Columns(4).Visible = True
Dim dgi As DataGridItem = CType(e.Item, DataGridItem)
Dim c As Control
For Each c In dgi.Cells(4).Controls
c.Visible = True
Next
CType(e.CommandSource, Button).Text = "Hide Details"
Else
Master.Columns(4).Visible = False
Dim dgi As DataGridItem = CType(e.Item, DataGridItem)
Dim c As Control
For Each c In dgi.Cells(4).Controls
c.Visible = False
Next
CType(e.CommandSource, Button).Text = "Show Details"
End If

The code works well on my machine

=============================================================
Please apply my suggestion above and let me know if it helps resolve your
problem.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.
Have a nice day!!

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
P

Patrick Laferriere

Jeff,

When I run the sample and click on Show Details I don't see the details.
I get two colomns that are titled Row Errors and Has Errors
respectively. The HasErrors column is false all the way done but there
is nothing in the other column. Do you know why it's not displaying the
details for me?

Thanks in advance,
PatLaf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top