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

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 :).

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.

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

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!

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

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 <asp:TemplateColumn> but that is not what is required here. Has anybody got any ideas?

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...

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

Thanks,

Hal

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.

This article has been dead for over six months. Start a new discussion instead.