all groups > asp.net webcontrols > february 2006 >
You're in the

asp.net webcontrols

group:

Nested Datagrid spanning columns of Parent Datagrid


Nested Datagrid spanning columns of Parent Datagrid Erik
2/28/2006 7:35:28 AM
asp.net webcontrols:
Good Morning,
I am trying to write a report which will have a nested datagrid spanning the
columns of the previous line (1st line is the equipment detail information,
2nd line are the user-entered notes for the equipment). When the user notes
are nested in the parent datagrid as a template column on the first line, the
report's length grows to an unacceptable length.

Any insight and suggestions are greatly appreciated.
RE: Nested Datagrid spanning columns of Parent Datagrid Phillip Williams
2/28/2006 8:46:27 AM
Hi Erik,

Can you post the markup that you used to give an idea about the final layout
of your nested tables? You might be able to get the result that you want by
re-arranging your templates or by using CSS.

For example, in this demo,
http://www.webswapp.com/codesamples/aspnet20/nestedgridviews/default.aspx
the nested gridview appears as if it spanned n-1 of the columns of the
parentGridView, like this:

<asp:GridView ID="parentGridViw" runat="server" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table border="0" cellpadding="2" cellspacing="0" width="510">
<tr>
<!-- this row has the parent grid implementation on n columns-->
</tr>

<tr>
<td colspan="n-1">
<!-- this row has the child grid which spans n-1 columns of the
parent-->
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


[quoted text, click to view]
RE: Nested Datagrid spanning columns of Parent Datagrid Phillip Williams
2/28/2006 9:36:28 AM
My previous answer used the GridView as an example but you can do the same
with the datagrid as in this demo:
http://www.societopia.net/Samples/DataGrid_Hierarchy.aspx

<asp:DataGrid ID="ParentGrid" Runat =server >
<Columns>
<asp:TemplateColumn >
<ItemTemplate >
<table class="DataGridStyle4" border=0 cellpadding=0 cellspacing=1>
<tr>
<!--- this row has the parent Grid in n columns--->
</tr>
<tr >
<!--- this row has the child grid in n-1 columns -->
<td colspan =n-1>
<asp:DataGrid id="ChildGrid1" Runat="server"></asp:DataGrid>
</td>
<td>
<!--- this is the nth columns (which can be left blank-->
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


[quoted text, click to view]
RE: Nested Datagrid spanning columns of Parent Datagrid Erik
2/28/2006 10:45:28 AM
Thanks again Phillip. I will translate this to vb, and let you know what
happens.

[quoted text, click to view]
RE: Nested Datagrid spanning columns of Parent Datagrid Erik
3/1/2006 7:42:53 AM
Good Morning Phillip,
I have one error from the conversion I cannot figure out -
'CompanyPrimaryKey' is not a member of 'MIMSearch.ChildDataGrid'
Here is the line it is erroring on:
ChildGrid.CompanyPrimaryKey = iPrimaryKey
Here is the Class:
Public Class ChildDataGrid
Inherits System.Web.UI.UserControl
Protected ChildGrid As System.Web.UI.WebControls.DataGrid
Public Property CompanyPrimaryKey() As Integer
Get
Dim ret As Integer = 0
If Not ViewState("CompanyPrimaryKey") Is Nothing Then
ret = (Int(ViewState("CompanyPrimaryKey")))
End If
Return ret
End Get
Set(ByVal Value As Integer)
ViewState("CompanyPrimaryKey") = Value
DataGrid_Bind()
End Set
End Property
Private Sub DataGrid_Bind()
If Not Data_View() Is Nothing Then
If Data_View().Count > 0 Then
ChildGrid.DataSource = Data_View()
ChildGrid.DataBind()
End If
End If
End Sub
Private Function Data_View() As DataView
Dim dv As DataView = Nothing
Dim ds As DataSet = Session("DataGrid_ParentChild")
If Not ds Is Nothing Then
Dim dvParent As New DataView(ds.Tables("Company"), "ID=" +
CompanyPrimaryKey, Nothing, System.Data.DataViewRowState.CurrentRows)
If (dvParent.Count > 0) Then
dv = dvParent(0).CreateChildView("ParentChild")
End If
End If
Return dv
End Function
Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
'Put user code to initialize the page here
End Sub
#Region " Web Form Designer Generated Code "
Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
InitializeComponent()
MyBase.OnInit(e)
End Sub
'<summary>
' Required method for Designer support - do not modify
' the contents of this method with the code editor.
' </summary>
Private Sub InitializeComponent()
AddHandler Me.Load, AddressOf Me.Page_Load
End Sub
#End Region
End Class

Any insight is appreciated. This is my first attempt at a custom user
control, and coming from a Progress background I am still getting familiar
with the Microsoft world.
Thanks, Erik


[quoted text, click to view]
RE: Nested Datagrid spanning columns of Parent Datagrid Phillip Williams
3/1/2006 4:14:26 PM
The code you posted does not seem like the cause of the error message. Can
you post the code that uses this custom control? Also try to use the
debugger with break points at the line that caused the error and look at its
properties; does it have a public property with the name indicated in the
error message?
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


[quoted text, click to view]
RE: Nested Datagrid spanning columns of Parent Datagrid Erik
3/2/2006 7:28:23 AM
Good Morning Phillip,
When I run with errors, the following line:

<asp:DataGrid ID="ParentGrid" Runat="server" BorderStyle="None"
AutoGenerateColumns="False" DataKeyField="ID"
OnItemDataBound="ParentGrid_ItemDataBound" AllowPaging="False">

returns the compilation error:

'ParentGrid_ItemDataBound' is not a member of 'ASP.frmInvUser1_aspx'.

I researched the error number BC30456 but the description was vague. Below
is the vb codebehind:

Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Data.SqlClient
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls

Public Class frmInvUser1
Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()

End Sub
Protected WithEvents ParentGrid As System.Web.UI.WebControls.DataGrid

'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub

#End Region

Public Class DataGrid_Hierarchy
Inherits System.Web.UI.Page

Protected ParentGrid As DataGrid
Protected ChildGrid As MIMSearch.ChildDataGrid

Private Function CreateDS() As DataSet
Dim ds As DataSet
If Session("DataGrid_ParentChild") Is Nothing Then
ds = New DataSet
Dim dt As DataTable
dt.TableName = "Company"
Dim dr As DataRow
dt.Columns.Add(New DataColumn("ID", GetType(Integer)))
dt.Columns.Add(New DataColumn("CompanyName", GetType(String)))
dt.Columns.Add(New DataColumn("Address", GetType(String)))

Dim i As Integer

For i = 1 To i < 10
dr = dt.NewRow
dr(0) = i
dr(1) = "Company " + i + " Title " + Environment.NewLine
+ "Company Division " + i.ToString
dr(2) = "Street Address " + i + Environment.NewLine +
"City " + i + Environment.NewLine + "PostalCode " + i + Environment.NewLine +
"Country " + i.ToString
dt.Rows.Add(dr)
Next
ds.Tables.Add(dt)

Dim Parent_PKColumns(1) As DataColumn
Parent_PKColumns(0) = dt.Columns("ID")
dt.PrimaryKey = Parent_PKColumns

dt = New DataTable("Employees")
dt.Columns.Add(New DataColumn("ID", GetType(Integer)))
dt.Columns.Add(New DataColumn("CompanyID", GetType(Integer)))
dt.Columns.Add(New DataColumn("Name", GetType(String)))
dt.Columns.Add(New DataColumn("Dept", GetType(String)))
For i = 1 To i < 10
Dim y As Integer
For y = 1 To y < 3 '2 emplyees for each company
dr = dt.NewRow()
dr(0) = y + i * 5
dr(1) = i
dr(2) = "Employee # " + dr(0)
dr(3) = "Dept# " + (y + i)
dt.Rows.Add(dr)
Next
Next
Dim Child_PKColumns(1) As DataColumn
Child_PKColumns(0) = dt.Columns("ID")
dt.PrimaryKey = Child_PKColumns
ds.Tables.Add(dt)

Dim Child_FKColumns As ForeignKeyConstraint = New
ForeignKeyConstraint("Child_FKColumns", _
ds.Tables("Company").Columns("ID"), _
ds.Tables("Employees").Columns("CompanyID"))

Child_FKColumns.DeleteRule = Rule.None
' Cannot delete a customer value that has associated
existing orders.
Child_FKColumns.DeleteRule = Rule.None
ds.Tables("Employees").Constraints.Add(Child_FKColumns)

'DataRelation datarel= new DataRelation ("ParentChild",
Parent_PKColumns,
' Child_FKColumns);
'ds.Relations.Add (datarel);

Session("DataGrid_ParentChild") = ds
Else
ds = Session("DataGrid_ParentChild")
End If

Return ds
End Function

#Region " Web Form Designer Generated Code "
Protected Overrides Sub OnInit(ByVal e As System.EventArgs)
InitializeComponent()
MyBase.OnInit(e)
End Sub


' <summary>
' Required method for Designer support - do not modify
' the contents of this method with the code editor.
' </summary>
Private Sub InitializeComponent()
AddHandler Me.Load, AddressOf Me.Page_Load
End Sub
#End Region

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

Private Sub DataGrid_DataBind()
ParentGrid.DataSource = CreateDS.Tables("Company")
ParentGrid.DataBind()
End Sub

Private Sub ParentGrid_ItemDataBound(ByVal source As Object, ByVal e
As System.Web.UI.WebControls.DataGridCommandEventArgs)
If e.Item.ItemType = ListItemType.Item Or _
e.Item.ItemType = ListItemType.AlternatingItem
Or _
e.Item.ItemType = ListItemType.EditItem Then

Dim iPrimaryKey As Integer
iPrimaryKey = ParentGrid.DataKeys(e.Item.ItemIndex)
Dim ChildGrid As MIMSearch.ChildDataGrid
ChildGrid = e.Item.FindControl("ChildGrid1")
If ChildGrid Is Nothing Then
ChildGrid = e.Item.FindControl("ChildGrid2")
End If
If Not ChildGrid Is Nothing Then
ChildGrid.CompanyPrimaryKey = iPrimaryKey
End If
End If
End Sub
End Class

'class for the user control ChildDataGrid
Public Class ChildDataGrid
Inherits System.Web.UI.UserControl

Protected ChildGrid As System.Web.UI.WebControls.DataGrid

Public Property CompanyPrimaryKey() As Integer
Get
Dim ret As Integer = 0
RE: Nested Datagrid spanning columns of Parent Datagrid Phillip Williams
3/2/2006 9:48:30 AM
Good Morning Erik,

Yes, the error message this time is true. The structure of the code you
have here is not correct. You need to

1- modify the markup on your webform to inherit from the class named
“DataGrid_Hierarachy”. If you look back at my sample you would see a Page
directive like this:
<%@ Page Language="C#" AutoEventWireup="false"
src="DataGrid_Hierarchy.aspx.cs"
Inherits="Societopia.Samples.DataGrid_Hierarchy"%>

2- remove the extra class that is named as the webform name “frmInvUser1”
(basically all the code that you have enclosing the Public Class
DataGrid_Hierarchy…End Calss block of code) Because if you leave it you
would have 2 disconnected classes that inherited from the System.Web.UI.Page

Try that and let's take it from there.
--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


[quoted text, click to view]
AddThis Social Bookmark Button