954,193 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Nest Datagrids

I have a datagrid show all "New" work orders. I want to be able to open "History" of a work order under that work order number in the same datagrid. How would I do that? Do I need to insert rows or is it possible to insert a datagrid under that row? Note: I do not want the datagrid inside one field, I want it to have the same fields as the datagrid, but under the current row (and before next "New" row). Do I need to use datalist or repeater?

Hope someone can help me.
Thanks!
/Daniel

daniel75
Newbie Poster
6 posts since May 2004
Reputation Points: 10
Solved Threads: 0
 

I wish I could help dude, I am waiting for someone to answer my questions too. Just be patient and one of the moderators will get to you :).

Slade
Practically a Master Poster
633 posts since Mar 2004
Reputation Points: 115
Solved Threads: 7
 

You can use two datagrids. You will need to use the HTML view to add the second datagrid in a templated column of the first datagrid. Then you have to add a sub for the ItemDataBound of the parent grid.

Inside the ItemDataBound routine, you will need to check each row and then filter a dataset on that item number and bind those results to the child datagrid.

Since you are new to ASP.NET, I will work up a sample and submit it here. Hopefully, that will get you started for now.

plope
Newbie Poster
4 posts since May 2004
Reputation Points: 10
Solved Threads: 0
 

Here's how your datagrid might look:

<asp:DataGrid id="dgParent" runat="server" AutoGenerateColumns="False" OnItemDataBound="DataBound">
 <Columns>
  <asp:BoundColumn DataField="ID" HeaderText="ID"></asp:BoundColumn>
  <asp:TemplateColumn HeaderText="Detail">
   <ItemTemplate>
    <asp:DataGrid id="dgChild" runat="server" AutoGenerateColumns="False">
     <Columns>
     <asp:BoundColumn DataField="ID" HeaderText="ID"></asp:BoundColumn>
     <asp:BoundColumn DataField="Desc" HeaderText="Desc"></asp:BoundColumn>
     </Columns>
     </asp:DataGrid>
    </ItemTemplate>
   </asp:TemplateColumn>
 </Columns>
</asp:DataGrid>

(And I acutally did do this in the Property Builder while editing the templated column in dgParent).

And then your code would look something like this:

Private Shared ds As DataSet

  Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If Not Page.IsPostBack Then
     BindData()
    End If
 End Sub

 Private Sub BindData()
 'build your dataset here (from database or manually or whatever)
  Dim tbl As New DataTable("Parent")
  tbl.Columns.Add("ID", GetType(Integer))
  tbl.Columns.Add("Desc", GetType(String))

  Dim tblChild As New DataTable("Child")
  With tblChild.Columns
   .Add("ID", GetType(Integer))
   .Add("Desc", GetType(String))
  End With

  Dim row, chRow As DataRow
  Dim i, j As Short

  For i = 1 To 10
   row = tbl.NewRow()
   row("ID") = i
   row("Desc") = "Parent row " & i
   For j = 1 To 2
    chRow = tblChild.NewRow()
    chRow("ID") = i
    chRow("Desc") = "Child row " & j
    tblChild.Rows.Add(chRow)
   Next
   tbl.Rows.Add(row)
  Next

  ds = New DataSet("ParentChild")
  ds.Tables.Add(tbl)
  ds.Tables.Add(tblChild)

  'don't have to do this, but might be helpful
  ds.Relations.Add("relationID", ds.Tables(0).Columns("ID"), ds.Tables(1).Columns("ID"))

  dgParent.DataSource = ds.Tables(0)
  dgParent.DataBind()
 End Sub

 Public Sub DataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles dgParent.ItemDataBound
  If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
   Dim id As Integer = DataBinder.Eval(e.Item.DataItem, "ID")
   Dim childView As New DataView(ds.Tables(1))
   childView.RowFilter = "ID = " & id

   Dim dgChild As DataGrid = e.Item.FindControl("dgChild")
   dgChild.DataSource = childView
   dgChild.DataBind()
  End If
 End Sub
plope
Newbie Poster
4 posts since May 2004
Reputation Points: 10
Solved Threads: 0
 

I just re-read your reply and realized you did not want the "History" in a field of the "New" datagrid. So I would say your best bet would be to use a datalist or repeater. Your steps would be essentially the same with the filter of the "History" data in the ItemDataBound routine.

Hope that helps!

plope
Newbie Poster
4 posts since May 2004
Reputation Points: 10
Solved Threads: 0
 
I have a datagrid show all "New" work orders. I want to be able to open "History" of a work order under that work order number in the same datagrid. How would I do that? Do I need to insert rows or is it possible to insert a datagrid under that row? Note: I do not want the datagrid inside one field, I want it to have the same fields as the datagrid, but under the current row (and before next "New" row). Do I need to use datalist or repeater? Hope someone can help me. Thanks! /Daniel

Hi

culd u pls provide more info on the same,solution will be based on how ur page to look like

thsnks n rgards
siddartha

siddartha_pal
Newbie Poster
8 posts since Jun 2004
Reputation Points: 11
Solved Threads: 0
 

Sorry about not noticing your thread earlier ;) . Sometimes, its hard to describe what you want to do, and if you can't describe what you want, you can't find help for it. I'm pretty sure what you are trying to do is build a Master/Detail Nested Datagrid.

Check out this article on DotNetJunkies: Building a Master/Detail DataGrid , and after you're through with that, check out Building a Master/Detail DataGrid Part II :cool:.

Tekmaven
Software Architect
Moderator
1,274 posts since Feb 2002
Reputation Points: 322
Solved Threads: 28
 

I too have been having trouble with this problem.

The difficulty seems to lie with

Note: I do not want the datagrid inside one field, I want it to have the same fields as the datagrid, but under the current row (and before next "New" row).

All I can find are examples that add a datagrid/ datalist/ repeater to a but that is not what is required here. Has anybody got any ideas?

Moistly
Newbie Poster
1 post since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

I don't know if you have taken a look at Denis Bauer site and blog, but he has available his source code (as well as a compiled assembly) for Heirargrid...

[RIGHT HERE]

It is nicely done and having the source makes tailoring to fit a piece of cake...

Thanks,

Hal

hlesesne
Newbie Poster
1 post since Dec 2004
Reputation Points: 10
Solved Threads: 0
 

What would trigger the action? Yes, you can add new rows. What are you binding to the DataGrid?

I like to use a DataReader, as they are very high performance. I loop through the DataReader to create an ArrayList. Then I bind the ArrayList to the DataGrid.

In server-side code, it is easy to add items to the ArrayList and simply re-bind.

tgreer
Made Her Cry
Team Colleague
2,118 posts since Dec 2004
Reputation Points: 227
Solved Threads: 37
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You