Web Custom Control Inherited from DataGrid

I am developing a Multilevel datagrid Control derived from DataGrid.In that I have one collectionproperty ChildLayouts which is ChildLayoutColelction , where ChildLayout is a class derived from Datagrid
So When I click on the Ellipsis I am able to edit all the properties for a DataGrid. Click the Columns property, the datagrid property builder is shown where I am able to link a typeddatasource and assign datamember to the childlayout.
<cc1:multilevelgrid id=MultiLevelGrid1 runat="server" AllowExpandEvent="True" AutoGenerateColumns="False" Width="100%" BorderColor="DarkSeaGreen" BorderWidth="1px" ForeColor="Black" BackColor="Transparent" CellPadding="2" GridLines="None" ImageFolder="images/" AllowSorting="True" Font-Names="Tahoma" Font-Size="12px" AllowPaging="True" PageSize="5" CompanyDescription="kkkk" DataSource="<%# DsTestNew1 %>" DataMember="Customers" DataKeyField="CustomerID">
<AlternatingItemStyle BackColor="Silver"></AlternatingItemStyle>
<FooterStyle BackColor="Aquamarine"></FooterStyle>

<ChildLayouts>
<cc1:ChildLayout DataKeyField="OrderID" DataMember="Orders" DataSource='<%# DsTestNew1 %>'>
<Columns>
<asp:BoundColumn DataField="OrderID" SortExpression="OrderID" HeaderText="OrderID"></asp:BoundColumn>
<asp:BoundColumn DataField="CustomerID" SortExpression="CustomerID" HeaderText="CustomerID"></asp:BoundColumn>
<asp:BoundColumn DataField="EmployeeID" SortExpression="EmployeeID" HeaderText="EmployeeID"></asp:BoundColumn>
<asp:BoundColumn DataField="OrderDate" SortExpression="OrderDate" HeaderText="OrderDate"></asp:BoundColumn>
</Columns>
</cc1:ChildLayout>
<cc1:ChildLayout DataKeyField="ProductID" DataMember="Order Details" DataSource='<%# DsTestNew1 %>'>
<Columns>
<asp:BoundColumn DataField="OrderID" HeaderText="OrderID"></asp:BoundColumn>
<asp:BoundColumn DataField="ProductID" HeaderText="ProductID"></asp:BoundColumn>
<asp:BoundColumn DataField="UnitPrice" HeaderText="UnitPrice"></asp:BoundColumn>
<asp:BoundColumn DataField="Quantity" HeaderText="Quantity"></asp:BoundColumn>
<asp:BoundColumn DataField="Discount" HeaderText="Discount"></asp:BoundColumn>
</Columns>
</cc1:ChildLayout>
</ChildLayouts>
<Columns>
<asp:BoundColumn DataField="CustomerID" SortExpression="CustomerID" HeaderText="CustomerID"></asp:BoundColumn>
<asp:BoundColumn DataField="CompanyName" SortExpression="CompanyName" HeaderText="CompanyName"></asp:BoundColumn>
<asp:BoundColumn DataField="ContactName" SortExpression="ContactName" HeaderText="ContactName"></asp:BoundColumn>
<asp:BoundColumn DataField="ContactTitle" SortExpression="ContactTitle" HeaderText="ContactTitle"></asp:BoundColumn>
</Columns>

<HeaderStyle Font-Bold="True" ForeColor="White" BackColor="DarkSeaGreen"></HeaderStyle>
<SelectedItemStyle ForeColor="GhostWhite" BackColor="DarkSlateBlue"></SelectedItemStyle>
<PagerStyle BackColor="MediumAquamarine"></PagerStyle>

</cc1:multilevelgrid>

This is the HTML Generated .


This is working fine for first time. Also All levels are expanding and colllapsing correctly. Once I compiled this then the columns property is not working.It shows "Object Reference Not Set to an Instance of an object".
But runtime it wirks fine .But at the designer it shows this error.I notice that first time when I adds childlayout it's added as ChildLayout1 and it's declaration protected withevents ChildLayout1 as MyControlLibrary.ChildLayout is generated automatically and it's property editor has the title Chil;dLayout1 properties.
But once I compile it becomes ChildLayout . Actuallly I am not sure whether the structure I used is correct.

I am pasting the class structure Here.

Imports System.ComponentModel
Imports System.Web.UI
Imports System.io
Imports System.Web.UI.WebControls
Imports System.ComponentModel.Design
Imports System.Web.UI.Design
Imports System.Drawing.Design


'<ParseChildren(True), PersistChildren(False), Designer(GetType(MultiLevelGrid), GetType(System.Web.UI.Design.WebControls.DataGridDesigner)), ToolboxData("<{0}:MultiLevelGrid runat=server></{0}:MultiLevelGrid>")> Public Class MultiLevelGrid
<PersistChildren(False), Designer(GetType(MultiLevelGridDesigner)), DefaultProperty("Text"), ToolboxData("<{0}:MultiLevelGrid runat=server></{0}:MultiLevelGrid>")> Public Class MultiLevelGrid
Inherits DataGrid

Public HostColumnWidth As Unit
Public MasterGrid As DataGrid
Public MasterExpandItem As DataGridItem

Dim _CompanyDescription As String = ""

Private Shared CompanyName As String = "NetFrontiers"

Dim _ImageFolder As String

Dim _RelationName As New ArrayList
Dim _Members As New ArrayList

Dim _AllowExpandEvent As Boolean


<Category("InnerGrid"), PersistenceMode(PersistenceMode.InnerProperty), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
Public ReadOnly Property ChildLayouts() As ChildLayoutCollection
Get
''If IsNothing(_ChildLayouts) Then
'' Dim arrExpandItem As New ChildLayoutCollection
'' arrExpandItem.Add(New ChildLayout)
'' _ChildLayouts = arrExpandItem
''End If
Return CType(_ChildLayouts, ChildLayoutCollection)

End Get
End Property


<Editor(GetType(DescriptionEditor), GetType(UITypeEditor))> _
Public Property CompanyDescription() As String
Get
If IsNothing(_CompanyDescription) Then
_CompanyDescription = ""
End If
Return _CompanyDescription
End Get
Set(ByVal Value As String)
_CompanyDescription = Value
End Set
End Property

Public Property ImageFolder() As String
Get
If IsNothing(_ImageFolder) Then
Return ""
Else
Return _ImageFolder
End If
End Get
Set(ByVal Value As String)
_ImageFolder = Value
End Set
End Property

<Browsable(False), DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)> Public Property ExpandedItem() As ArrayList
Get
If IsNothing(ViewState("ExpandedItem")) Then
Dim arrExpandItem As New ArrayList
arrExpandItem.Add(-1)
ViewState("ExpandedItem") = arrExpandItem
Return ViewState("ExpandedItem")
Else
Return ViewState("ExpandedItem")
End If
End Get
Set(ByVal Value As ArrayList)
ViewState("ExpandedItem") = Value
End Set
End Property

Private Property RelationName() As ArrayList
Get

Return _RelationName
End Get
Set(ByVal Value As ArrayList)
_RelationName = Value
End Set
End Property

Private Property Members() As ArrayList
Get

Return _Members
End Get
Set(ByVal Value As ArrayList)
_Members = Value
End Set
End Property

Private Property childPageIndex() As ArrayList
Get
If IsNothing(viewstate("childPageIndex")) Then
Dim arrchildPageIndex As New ArrayList
arrchildPageIndex.Add(0)
ViewState("childPageIndex") = arrchildPageIndex
End If
Return ViewState("childPageIndex")

End Get
Set(ByVal Value As ArrayList)
ViewState("childPageIndex") = Value
End Set
End Property

Private Property arrSortExpression() As ArrayList
Get
Return ViewState("childSortExpression")
End Get
Set(ByVal Value As ArrayList)
ViewState("childSortExpression") = Value
End Set
End Property

Public Property AllowExpandEvent() As Boolean
Get
If IsNothing(_AllowExpandEvent) Then
Return False
Else
Return _AllowExpandEvent
End If
End Get
Set(ByVal Value As Boolean)
_AllowExpandEvent = Value
End Set
End Property
Public Event UpdateView As EventHandler
Public Event ExpandCommand(ByVal Source As Object, ByVal ItemIndex As Integer, ByVal GridLevel As Integer)
Public Event DetailsGridItemDataBound(ByVal Source As Object, ByVal item As DataGridItem, ByVal GridLevel As Integer)

Private Sub OnUpdateView()
RaiseEvent UpdateView(Me, EventArgs.Empty)
End Sub

Private Sub MultiLevelGrid_PageIndexChanged(ByVal source As Object, ByVal e As DataGridPageChangedEventArgs) Handles MyBase.PageIndexChanged
Me.CurrentPageIndex = e.NewPageIndex
Me.SelectedIndex = -1
Me.EditItemIndex = -1
childPageIndex(0) = e.NewPageIndex
ExpandedItem(0) = -1
OnUpdateView()
End Sub

Private Sub MultiLevelGrid_ItemCommand(ByVal source As Object, ByVal e As DataGridCommandEventArgs) Handles MyBase.ItemCommand
If (e.CommandName <> "Expand") Then
Return
End If
ExpandItem(e.Item, source)
End Sub

Private Sub ExpandItem(ByVal item As DataGridItem, ByVal Source As Object)
Dim MemberName As String
If CStr(Source.id).IndexOf("_") <> -1 Then
MemberName = CStr(Source.id).Substring(0, CStr(Source.id).IndexOf("_"))
Else
MemberName = Source.id
End If

Dim level As Integer = Members.IndexOf(MemberName)
level += 1
If (item.ItemIndex = (ExpandedItem(level) Mod Source.PageSize)) Then
SetExpandedItem(item, False, Source, level)
Else
SetExpandedItem(item, True, Source, level)
End If
OnUpdateView()
If AllowExpandEvent Then
RaiseEvent ExpandCommand(Source, item.ItemIndex, level)
End If
End Sub

Private Sub SetExpandedItem(ByVal item As DataGridItem, ByVal expand As Boolean, ByVal Source As Object, ByVal level As Integer)
Dim MemberName As String
If CStr(Source.id).IndexOf("_") <> -1 Then
MemberName = CStr(Source.id).Substring(0, CStr(Source.id).IndexOf("_"))
Else
MemberName = Source.id
End If
If (expand) Then
If Members.Contains(MemberName) Then
ExpandedItem(level) = (Source.PageSize * childPageIndex(level) + item.ItemIndex)
Else
ExpandedItem(level) = (Me.PageSize * Me.CurrentPageIndex + item.ItemIndex)
End If
Else
ExpandedItem(level) = -1
End If
For i As Integer = level + 1 To ExpandedItem.Count - 1
ExpandedItem(i) = -1
childPageIndex(i) = 0
If ChildAllowSorting = True Then
arrSortExpression(i - 1) = ""
End If
Next
End Sub

Private Sub MultiLevelGrid_ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles MyBase.ItemDataBound
Try
If (e.Item.ItemType <> ListItemType.Item And e.Item.ItemType <> ListItemType.AlternatingItem) Then
Return
End If

If ImageFolder.Trim <> "" And Me.TemplateSourceDirectory() <> "" Then
Dim dirInfo As New IO.DirectoryInfo(Me.MapPathSecure(Me.TemplateSourceDirectory()) & "/" & ImageFolder)
If dirInfo.Exists() Then
Dim fileInfo As New IO.FileInfo(Me.MapPathSecure(Me.TemplateSourceDirectory()) & "/" & ImageFolder & "/plus.gif")
If fileInfo.Exists() Then
Dim img As New HtmlControls.HtmlImage
img.Src = ImageFolder & "/Plus.gif"
img.Attributes.Add("onclick", "ExpandItem('" & sender.id & "'," & e.Item.ItemIndex & ",0" & ");")
img.Border = 0
img.Width = 19
img.Height = 20
If Me.Columns(0).GetType() Is GetType(ButtonColumn) Then
CType(e.Item.Cells(0).Controls(0), LinkButton).Text = ""
CType(e.Item.Cells(0).Controls(0), LinkButton).Controls.Add(img)
End If
End If
End If
End If

If Not (e.Item.ItemIndex = (ExpandedItem(0) Mod Me.PageSize)) Then
e.Item.Cells(1).Width = HostColumnWidth
End If
If Not IsNothing(Members) And Not IsNothing(RelationName) Then
If Me.Columns.Count > 0 And Members.Count > 0 And RelationName.Count > 0 Then

If RelationName.Count <> 0 Then

Dim ds As DataSet
' If Me.DataSource.GetType() Is GetType(System.Data.DataSet) Then
ds = CType(Me.DataSource, DataSet)
' Else
' ds = CType(Me.DataSource.dataset, Object)
' End If

If Not IsNothing(ds) Then
Dim dt As DataTable
dt = ds.Tables(0)

Dim drel As DataRelation = ds.Relations(ds.Tables(0).ChildRelations(0).RelationName)
Dim dcol As DataColumn = drel.ParentKeyConstraint.Columns(0)
Dim keyValue As String = dt.Rows((Me.PageSize * childPageIndex(0)) + e.Item.ItemIndex).Item(dcol.ColumnName)
Dim theView As DataView = New DataView(dt)
If dcol.DataType Is GetType(String) Then
theView.RowFilter = dcol.ColumnName & "='" & keyValue & "'"
Else
theView.RowFilter = dcol.ColumnName & "=" & keyValue
End If
Dim drv As DataRowView = theView.Item(0)
Dim detailsView As DataView = drv.CreateChildView(ds.Relations(ds.Tables(0).ChildRelations(0).RelationName))
If detailsView.Count = 0 Then
If CType(e.Item.Cells(0).Controls(0), LinkButton).Controls.Count > 0 Then
CType(e.Item.Cells(0).Controls(0), LinkButton).Controls.Clear()
End If
CType(e.Item.Cells(0).Controls(0), LinkButton).Text = ""
End If
End If

If ExpandedItem(0) = (sender.pagesize * childPageIndex(0)) + e.Item.ItemIndex Then
BuildChildLayout(sender, e.Item, Members(0), RelationName(0))
End If
End If
End If
End If
Catch ex As Exception
Return
End Try
End Sub

Public Sub BuildChildLayout(ByVal grd As DataGrid, ByVal item As DataGridItem, ByVal Member As String, ByVal Relation As String)
Try
Dim row As DataGridItem = item
MasterGrid = grd
MasterExpandItem = item

Dim tcell(row.Cells.Count - 1) As TableCell
row.Cells.CopyTo(tcell, 0)

Dim listOfText As ArrayList = New ArrayList
Dim listOfWidth As ArrayList = New ArrayList

Dim i As Integer

Dim celltospanover As Integer = row.Cells.Count - 1

'New code
If CType(item.Cells(0).Controls(0), LinkButton).Controls.Count > 0 Then
CType(item.Cells(0).Controls(0), LinkButton).Controls.Clear()
End If
CType(row.Cells(0).Controls(0), LinkButton).Text = "-"
If ImageFolder.Trim <> "" And Me.TemplateSourceDirectory <> "" Then
Dim dirInfo As New IO.DirectoryInfo(Me.MapPathSecure(Me.TemplateSourceDirectory()) & "/" & ImageFolder)
If dirInfo.Exists() Then
Dim fileInfo As New IO.FileInfo(Me.MapPathSecure(Me.TemplateSourceDirectory()) & "/" & ImageFolder & "/minus.gif")
If fileInfo.Exists() Then
Dim img As New HtmlControls.HtmlImage
img.Src = ImageFolder & "/minus.gif"
img.Border = 0
img.Width = 19
img.Height = 20
CType(item.Cells(0).Controls(0), LinkButton).Text = ""
CType(item.Cells(0).Controls(0), LinkButton).Controls.Add(img)
End If
End If
End If

For i = row.Cells.Count - 1 To 1 Step -1
row.Cells.RemoveAt(i)
Next


Dim newCell As TableCell = New TableCell
newCell.ColumnSpan = celltospanover
newCell.Width = Unit.Empty
row.Cells.Add(newCell)


Dim t As Table = New Table
t.Font.Name = Me.Font.Name
t.Font.Size = Me.Font.Size
t.BorderWidth = Me.BorderWidth
t.Width = Unit.Percentage(100)

Dim rowHeader As TableRow = New TableRow
t.Rows.Add(rowHeader)
Dim rowSubGrid As TableRow = New TableRow
t.Rows.Add(rowSubGrid)
newCell.VerticalAlign = VerticalAlign.Top
newCell.Controls.Add(t)

For i = 1 To tcell.Length - 1
Dim c As TableCell = New TableCell
c = tcell(i)
rowHeader.Cells.Add(c)
Next
rowHeader.Font.Name = ChildFont.Name
rowHeader.Font.Size = ChildFont.Size


Dim outerPanel As Panel = Nothing
' If (ChildAllowPaging = False) Then
outerPanel = New Panel
'outerPanel.Height = Unit.Pixel(100)
outerPanel.Style("overflow") = "auto"
outerPanel.Width = Unit.Percentage(100)
' End If


Dim cellSubGrid As TableCell = New TableCell
cellSubGrid.ColumnSpan = celltospanover
cellSubGrid.Width = Unit.Percentage(100)
cellSubGrid.BackColor = ChildBackColor

rowSubGrid.Cells.Add(cellSubGrid)

Dim detailsGrid As DataGrid


detailsGrid = New DataGrid 'ctype(grd,MultiLevelGrid).ChildLayouts.Current'
Dim LayoutIndex As Integer
LayoutIndex = CInt(Member)


Dim level As Integer = Members.IndexOf(Member)

detailsGrid.ID = Member & "_" & item.ItemIndex & Now.Date.Ticks

detailsGrid.BackColor = ChildLayouts.Item(LayoutIndex).BackColor 'ChildBackColor
detailsGrid.GridLines = ChildLayouts.Item(LayoutIndex).GridLines 'ChildGridLines
detailsGrid.Width = Unit.Percentage(100)

If Not IsNothing(ChildLayouts.Item(LayoutIndex).ItemStyle) Then detailsGrid.ItemStyle.CopyFrom(ChildLayouts.Item(LayoutIndex).ItemStyle)
If Not IsNothing(ChildLayouts.Item(LayoutIndex).AlternatingItemStyle) Then detailsGrid.AlternatingItemStyle.CopyFrom(ChildLayouts.Item(LayoutIndex).AlternatingItemStyle)
If Not IsNothing(ChildLayouts.Item(LayoutIndex).FooterStyle) Then detailsGrid.FooterStyle.CopyFrom(ChildLayouts.Item(LayoutIndex).FooterStyle)
If Not IsNothing(ChildLayouts.Item(LayoutIndex).HeaderStyle) Then detailsGrid.HeaderStyle.CopyFrom(ChildLayouts.Item(LayoutIndex).HeaderStyle)
If Not IsNothing(ChildLayouts.Item(LayoutIndex).PagerStyle) Then detailsGrid.PagerStyle.CopyFrom(ChildLayouts.Item(LayoutIndex).PagerStyle)
detailsGrid.ShowFooter = ChildLayouts.Item(LayoutIndex).ShowFooter
detailsGrid.ShowHeader = ChildLayouts.Item(LayoutIndex).ShowHeader
detailsGrid.CssClass = ChildLayouts.Item(LayoutIndex).CssClass
detailsGrid.HorizontalAlign = ChildLayouts.Item(LayoutIndex).HorizontalAlign

If Not IsNothing(ChildLayouts.Item(LayoutIndex).Font) Then detailsGrid.Font.CopyFrom(ChildLayouts.Item(LayoutIndex).Font)

If (ChildLayouts.Item(LayoutIndex).AllowPaging = True) Then
detailsGrid.AllowPaging = True
detailsGrid.PageSize = ChildLayouts.Item(LayoutIndex).PageSize
AddHandler detailsGrid.PageIndexChanged, AddressOf detailsGrid_PageIndexChanged
End If

AddHandler detailsGrid.ItemDataBound, AddressOf detailsgrid_ItemDataBound
AddHandler detailsGrid.ItemCommand, AddressOf detailsgrid_ItemCommand
AddHandler detailsGrid.DataBinding, AddressOf detailsgrid_DataBinding


If ChildAllowSorting Then
detailsGrid.AllowSorting = True
AddHandler detailsGrid.SortCommand, AddressOf detailsgrid_SortCommand
End If

If ChildLayouts.Item(LayoutIndex).AllowSorting Then
detailsGrid.AllowSorting = True
AddHandler detailsGrid.SortCommand, AddressOf detailsgrid_SortCommand
Else
detailsGrid.AllowSorting = False
End If

BindDetails(detailsGrid, Member, Relation)


' If (ChildAllowPaging = False) Then
outerPanel.Controls.Add(detailsGrid)
cellSubGrid.Controls.Add(outerPanel)

'Else
' cellSubGrid.Controls.Add(detailsGrid)
'End If
Catch ex As Exception
Return
End Try
End Sub


Public Sub BindDetails(ByVal d As DataGrid, ByVal Member As String, ByVal Relation As String)
Try
If IsNothing(Me.DataSource) Then
Return
Else
Dim ds As DataSet
' If Me.DataSource.GetType() Is GetType(System.Data.DataSet) Then
ds = CType(Me.DataSource, DataSet)
' Else
' ds = CType(Me.DataSource.dataset, Object)
' End If
If (ds Is Nothing) Then
Return
Else

Dim dt As DataTable
dt = ds.Tables(CInt(Member))

Dim drel As DataRelation = ds.Relations(Relation)
Dim dcol As DataColumn = drel.ParentKeyConstraint.Columns(0)
Dim dtable As DataTable
Dim keyValue As String
Dim level As Integer = Members.IndexOf(Member)

'SortExpression
If ChildLayouts.Item(level).AllowSorting Then
If IsNothing(arrSortExpression) Then
arrSortExpression = New ArrayList
arrSortExpression.Add("")
ElseIf arrSortExpression.Count - 1 < level Then
arrSortExpression.Add("")
End If
End If

If MasterGrid.DataSource.GetType() Is GetType(DataSet) Then
Dim dset As DataSet
dset = CType(MasterGrid.DataSource, DataSet)
dtable = dset.Tables(0)
keyValue = dtable.Rows((MasterGrid.PageSize * childPageIndex(level)) + MasterExpandItem.ItemIndex).Item(dcol.ColumnName)
ElseIf (MasterGrid.DataSource.GetType() Is GetType(DataView)) Then
Dim dview As New DataView
dview = CType(MasterGrid.DataSource, DataView)
keyValue = dview.Item((MasterGrid.PageSize * childPageIndex(level)) + MasterExpandItem.ItemIndex).Item(dcol.ColumnName)
ElseIf MasterGrid.DataSource.GetType.ToString() = "System.Data.RelatedView" Then
Dim dview As New DataView
dview = CType(MasterGrid.DataSource, DataView)
keyValue = dview.Item((MasterGrid.PageSize * childPageIndex(level)) + MasterExpandItem.ItemIndex).Item(dcol.ColumnName)
Else
Dim dset As Object
dset = CType(MasterGrid.DataSource, Object)
dtable = dset.Tables(0)
keyValue = dtable.Rows((MasterGrid.PageSize * childPageIndex(level)) + MasterExpandItem.ItemIndex).Item(dcol.ColumnName)
End If

Dim theView As DataView = New DataView(dt)


If ExpandedItem.Count - 1 = level Then
ExpandedItem.Add(-1)
childPageIndex.Add(0)
End If
If dcol.DataType Is GetType(String) Then
theView.RowFilter = dcol.ColumnName & "='" & keyValue & "'"
Else
theView.RowFilter = dcol.ColumnName & "=" & keyValue
End If
Dim drv As DataRowView = theView.Item(0)
Dim detailsView As DataView = drv.CreateChildView(Relation)
If ExpandedItem(level) <> -1 Then
If detailsView.Count > 0 Then
d.AutoGenerateColumns = False
d.CurrentPageIndex = childPageIndex(level + 1)
If ChildLayouts.Count >= level + 1 Then
If ChildLayouts.Item(level).Columns.Count > 0 Then
d.Columns.Clear()
For i As Integer = 0 To ChildLayouts.Item(level).Columns.Count - 1
d.Columns.Add(ChildLayouts.Item(level).Columns.Item(i))
Next
Else
Return
End If
Else
d.AutoGenerateColumns = True
End If

If ChildAllowSorting Then
If arrSortExpression(level) <> "" Then
detailsView.Sort = arrSortExpression(level)
End If
End If

d.DataSource = detailsView
d.DataBind()
End If
End If

End If
End If
Catch ex As Exception
Return
End Try
End Sub

Private Sub detailsGrid_PageIndexChanged(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs)
Dim detGrid As DataGrid = CType(sender, DataGrid)
detGrid.CurrentPageIndex = e.NewPageIndex
Dim MemberName As String = CStr(sender.id).Substring(0, CStr(sender.id).IndexOf("_"))
Dim level As Integer = Members.IndexOf(MemberName)

detGrid.SelectedIndex = -1
detGrid.EditItemIndex = -1
For i As Integer = level + 1 To ExpandedItem.Count - 1
ExpandedItem(i) = -1
Next
childPageIndex(level + 1) = e.NewPageIndex
OnUpdateView()
End Sub

Private Sub detailsgrid_ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
Try
Dim MemberName As String = CStr(sender.id).Substring(0, CStr(sender.id).IndexOf("_"))
Dim level As Integer = Members.IndexOf(MemberName)
Dim GridLevel As Integer = level
If (e.Item.ItemType <> ListItemType.Item And e.Item.ItemType <> ListItemType.AlternatingItem) Then
RaiseEvent DetailsGridItemDataBound(sender, e.Item, GridLevel)
Return
End If

If ImageFolder.Trim <> "" And Me.TemplateSourceDirectory <> "" Then
Dim dirInfo As New IO.DirectoryInfo(Me.MapPathSecure(Me.TemplateSourceDirectory()) & "/" & ImageFolder)
If dirInfo.Exists() Then
Dim fileInfo As New IO.FileInfo(Me.MapPathSecure(Me.TemplateSourceDirectory()) & "/" & ImageFolder & "/plus.gif")
If fileInfo.Exists() Then
Dim img As New HtmlControls.HtmlImage
img.Src = ImageFolder & "/plus.gif"
img.Attributes.Add("onclick", "ExpandItem('" & sender.id & "'," & e.Item.ItemIndex & "," & level + 1 & ");")
img.Border = 0
img.Width = 19
img.Height = 20
If sender.Columns(0).GetType() Is GetType(ButtonColumn) Then
CType(e.Item.Cells(0).Controls(0), LinkButton).Text = ""
CType(e.Item.Cells(0).Controls(0), LinkButton).Controls.Add(img)
End If
End If
End If
End If

If Not (e.Item.ItemIndex = (ExpandedItem(level) Mod sender.PageSize)) Then
e.Item.Cells(1).Width = HostColumnWidth
End If
If sender.Columns.Count > 0 Then
If RelationName.Count <> level + 1 Then
level = level + 1


Dim ds As DataSet
' If Me.DataSource.GetType() Is GetType(System.Data.DataSet) Then
ds = CType(Me.DataSource, DataSet)
' Else
' ds = CType(Me.DataSource.dataset, Object)
' End If
If Not IsNothing(ds) Then
Dim dt As DataTable
dt = ds.Tables(level)


Dim drel As DataRelation = ds.Relations(ds.Tables(level).ChildRelations(0).RelationName)
Dim dcol As DataColumn = drel.ParentKeyConstraint.Columns(0)
Dim dtable As DataTable
Dim keyValue As String
If sender.DataSource.GetType() Is GetType(DataSet) Then
Dim dset As DataSet
dset = CType(sender.DataSource, DataSet)
dtable = dset.Tables(0)
keyValue = dtable.Rows((sender.pagesize * childPageIndex(level)) + e.Item.ItemIndex).Item(dcol.ColumnName)
Else
Dim dview As New DataView
dview = CType(sender.DataSource, DataView)
keyValue = dview.Item((sender.pagesize * childPageIndex(level)) + e.Item.ItemIndex).Item(dcol.ColumnName)
End If

Dim theView As DataView = New DataView(dt)
If dcol.DataType Is GetType(String) Then
theView.RowFilter = dcol.ColumnName & "='" & keyValue & "'"
Else
theView.RowFilter = dcol.ColumnName & "=" & keyValue
End If

Dim drv As DataRowView = theView.Item(0)
Dim detailsView As DataView = drv.CreateChildView(ds.Relations(ds.Tables(level).ChildRelations(0).RelationName))
If detailsView.Count = 0 Then
If CType(e.Item.Cells(0).Controls(0), LinkButton).Controls.Count > 0 Then
CType(e.Item.Cells(0).Controls(0), LinkButton).Controls.Clear()
End If
CType(e.Item.Cells(0).Controls(0), LinkButton).Text = ""
End If
End If

If ExpandedItem(level) = (sender.pagesize * childPageIndex(level)) + e.Item.ItemIndex Then
BuildChildLayout(sender, e.Item, Me.Members(level), RelationName(level))
End If
End If
End If
RaiseEvent DetailsGridItemDataBound(sender, e.Item, GridLevel)
Catch ex As Exception
Return
End Try
End Sub

Private Sub detailsgrid_ItemCommand(ByVal source As Object, ByVal e As DataGridCommandEventArgs)
If (e.CommandName <> "Expand") Then
Return
End If
ExpandItem(e.Item, source)
End Sub

Private Sub MultiLevelGrid_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.DataBinding
Try
Dim ds As DataSet

ds = CType(Me.DataSource, Object)
If (ds Is Nothing) Then
Return
Else
If Members.Count = 0 Then
Dim _Members As New ArrayList
For i As Integer = 0 To ds.Tables.Count - 1
_Members.Add(CStr(i))
Next
Members = _Members
End If
If RelationName.Count = 0 Then
Dim _RelationName As New ArrayList
For Each drel As DataRelation In ds.Relations
_RelationName.Add(drel.RelationName)
Next
RelationName = _RelationName
End If
End If
If Not IsNothing(RelationName) Then
If RelationName.Count > 0 Then
If Me.Columns.Count > 0 Then
If Me.Columns(0).GetType() Is GetType(ButtonColumn) Then
If CType(Me.Columns(0), ButtonColumn).CommandName = "Expand" Then
Exit Sub
End If
End If
End If
Else
Exit Sub
End If
Dim bclmExpand As New ButtonColumn
bclmExpand.Text = "+"
bclmExpand.ItemStyle.VerticalAlign = VerticalAlign.Top
bclmExpand.ButtonType = ButtonColumnType.LinkButton
bclmExpand.CommandName = "Expand"
Me.Columns.AddAt(0, bclmExpand)
End If
Catch ex As Exception
Return
End Try
End Sub


Private Sub detailsgrid_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs)
Dim MemberName As String = CStr(sender.id).Substring(0, CStr(sender.id).IndexOf("_"))
Dim level As Integer = Members.IndexOf(MemberName)
If Not IsNothing(RelationName) Then
If RelationName.Count <> level + 1 Then
If sender.columns.count > 0 Then
If sender.Columns(0).GetType() Is GetType(ButtonColumn) Then
If CType(sender.Columns(0), ButtonColumn).CommandName = "Expand" Then
Exit Sub
End If
End If
End If
Else
Exit Sub
End If
Dim bclmExpand As New ButtonColumn
bclmExpand.Text = "+"
bclmExpand.ButtonType = ButtonColumnType.LinkButton
bclmExpand.ItemStyle.VerticalAlign = VerticalAlign.Top
bclmExpand.CommandName = "Expand"
sender.Columns.AddAt(0, bclmExpand)
End If
End Sub

Private Sub MultiLevelGrid_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles MyBase.SortCommand
Dim i As Integer
For i = 0 To ExpandedItem.Count - 1
ExpandedItem(i) = -1
Next
End Sub

Private Sub detailsgrid_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs)
If ChildAllowSorting Then
If e.SortExpression <> "" Then
Dim MemberName As String = CStr(source.id).Substring(0, CStr(source.id).IndexOf("_"))
Dim level As Integer = Members.IndexOf(MemberName)
arrSortExpression(level) = e.SortExpression
For i As Integer = level + 1 To ExpandedItem.Count - 1
ExpandedItem(i) = -1
Next
OnUpdateView()
End If
End If
End Sub


Protected Overrides Sub Render(ByVal output As System.Web.UI.HtmlTextWriter)
Page.RegisterClientScriptBlock(Me.UniqueID & "ExpandItem", "<script>function ExpandItem(){}</script>")
MyBase.Render(output)
End Sub

Public Class DescriptionEditor
Inherits System.Drawing.Design.UITypeEditor

Public Sub New()
End Sub

' Indicates whether the UITypeEditor provides a form-based (modal) dialog,
' drop down dialog, or no UI outside of the properties window.
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Public Overloads Overrides Function GetEditStyle(ByVal context As System.ComponentModel.ITypeDescriptorContext) As System.Drawing.Design.UITypeEditorEditStyle
Return UITypeEditorEditStyle.Modal
End Function

' Displays the UI for value selection.
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Public Overloads Overrides Function EditValue(ByVal context As System.ComponentModel.ITypeDescriptorContext, ByVal provider As System.IServiceProvider, ByVal value As Object) As Object
Dim edSvc As System.Windows.Forms.design.IWindowsFormsEditorService = CType(provider.GetService(GetType(System.Windows.Forms.design.IWindowsFormsEditorService)), System.Windows.Forms.design.IWindowsFormsEditorService)
If Not (edSvc Is Nothing) Then
Dim f As New frmCompanyDescription
f.Text = MultiLevelGrid.CompanyName
f.txtDescription.Text = value
Dim dr As System.Windows.Forms.DialogResult = edSvc.ShowDialog(f)
Return f.txtDescription.Text
End If
Return value
End Function

' Draws a representation of the property's value.
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Public Overloads Overrides Sub PaintValue(ByVal e As System.Drawing.Design.PaintValueEventArgs)
End Sub

' Indicates whether the UITypeEditor supports painting a
' representation of a property's value.
<System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Public Overloads Overrides Function GetPaintValueSupported(ByVal context As System.ComponentModel.ITypeDescriptorContext) As Boolean
Return True
End Function
End Class

End Class


Public Class ChildLayout
Inherits System.Web.UI.WebControls.DataGrid

Private _Columns As DataGridColumnCollection
<Bindable(True), Editor(GetType(System.Web.UI.Design.WebControls.DataGridComponentEditor), GetType(ComponentEditor)), DefaultValue(True)> _
Public Overrides ReadOnly Property Columns() As System.Web.UI.WebControls.DataGridColumnCollection
Get
If IsNothing(_Columns) Then
_Columns = New DataGridColumnCollection(New DataGrid, New ArrayList)
End If
Return _Columns
End Get
End Property

End Class


Public Class ChildLayoutCollection
Inherits System.Collections.CollectionBase


Dim _intCurrentIndex As Integer = 0

Sub Add(ByVal item As ChildLayout)
List.Add(item)
_intCurrentIndex += 1
End Sub
Function Remove(ByVal intIndex As Integer) As Boolean
If intIndex > Count - 1 Or intIndex < 0 Then
Return False
Else
List.RemoveAt(intIndex)
_intCurrentIndex = Count
Return True
End If
End Function
Public Sub Reset()
List.Clear()
End Sub
Public ReadOnly Property Item(ByVal intIndex As Integer) As ChildLayout
Get
Return CType(List.Item(intIndex), ChildLayout)
End Get
End Property
Function MovePrev() As Boolean
If (_intCurrentIndex <= 0) Then
Return False
Else
_intCurrentIndex -= 1
Return True
End If
End Function
Function MoveNext() As Boolean
If _intCurrentIndex - 1 = (List.Count) - 1 Then
Return False
Else
_intCurrentIndex += 1
Return True
End If
End Function
Function MoveFirst() As Boolean
If (_intCurrentIndex <= 0) Then
Return False
Else
_intCurrentIndex = 1
Return True
End If
End Function
Function MoveLast() As Boolean
If (_intCurrentIndex > list.Count) Then
Return False
Else
_intCurrentIndex = list.Count
Return True
End If
End Function
Function Search(ByVal strFieldValue As String) As Integer
Dim i As Integer
For i = 0 To list.Count - 1
If list.Item(i).Name = strFieldValue Then
Return i
End If
Next i
End Function
Public ReadOnly Property Current() As ChildLayout
Get
If _intCurrentIndex <> -1 Then

Return CType(List.Item(_intCurrentIndex - 1), ChildLayout)
End If
End Get
End Property
End Class

Public Class MultiLevelGridDesigner
Inherits System.Web.UI.Design.WebControls.DataGridDesigner

Dim result As DesignerVerbCollection = MyBase.verbs

Public Overrides ReadOnly Property AllowResize() As Boolean
Get
Return True
End Get
End Property

Protected Overrides Function GetCachedTemplateEditingVerbs() As System.Web.UI.Design.TemplateEditingVerb()

Dim cvEditingVerbs() As TemplateEditingVerb
Dim i, j, k As Integer
For i = 0 To MyBase.GetCachedTemplateEditingVerbs.Length
If i = MyBase.GetCachedTemplateEditingVerbs.Length Then
Dim _Tcount As Integer = i
For k = 0 To CType(Me.Component, MultiLevelGrid).ChildLayouts.Count - 1
For j = 0 To CType(Me.Component, MultiLevelGrid).ChildLayouts.Item(k).Columns.Count - 1
If CType(Me.Component, MultiLevelGrid).ChildLayouts.Item(k).Columns(j).GetType() Is GetType(TemplateColumn) Then
ReDim Preserve cvEditingVerbs(_Tcount)
cvEditingVerbs(_Tcount) = New TemplateEditingVerb("ChildLayout " & k & " - Columns[" & j & "] - " & CType(Me.Component, MultiLevelGrid).ChildLayouts.Item(k).Columns(j).HeaderText, _Tcount, Me)
_Tcount += 1
End If
Next
Next
Else
ReDim Preserve cvEditingVerbs(i)
cvEditingVerbs(i) = New TemplateEditingVerb(MyBase.GetCachedTemplateEditingVerbs(i).Text, i, Me)
End If
Next
Return cvEditingVerbs

End Function

Public Overrides Function GetTemplateContent(ByVal editingFrame As System.Web.UI.Design.ITemplateEditingFrame, ByVal templateName As String, ByRef allowEditing As Boolean) As String
Try
If editingFrame.Verb.Text.StartsWith("ChildLayout") Then
Dim verbText As String = editingFrame.Verb.Text.Trim
Dim startindex As Integer = verbText.IndexOf(" ") + 1
Dim length As Integer = verbText.IndexOf(" ", startindex) - startindex
Dim LayoutNo As Integer = verbText.Substring(startindex, length)
startindex = verbText.IndexOf("[") + 1
length = verbText.IndexOf("]") - startindex
Dim ColumnNo As Integer = verbText.Substring(startindex, length)
Dim Tcol As New TemplateColumn
Tcol = CType(Me.Component, MultiLevelGrid).ChildLayouts.Item(LayoutNo).Columns(ColumnNo)
If templateName = "HeaderTemplate" Then
If Not IsNothing(Tcol.HeaderTemplate) Then
Return gettextfromtemplate(Tcol.HeaderTemplate)
End If
ElseIf templateName = "ItemTemplate" Then
If Not IsNothing(Tcol.ItemTemplate) Then
Return gettextfromtemplate(Tcol.ItemTemplate)
End If
ElseIf templateName = "EditItemTemplate" Then
If Not IsNothing(Tcol.EditItemTemplate) Then
Return gettextfromtemplate(Tcol.EditItemTemplate)
End If
ElseIf templateName = "FooterTemplate" Then
If Not IsNothing(Tcol.FooterTemplate) Then
Return gettextfromtemplate(Tcol.FooterTemplate)
End If
End If

Else
Return MyBase.GetTemplateContent(editingFrame, templateName, allowEditing)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Function

Public Overrides Sub SetTemplateContent(ByVal editingFrame As System.Web.UI.Design.ITemplateEditingFrame, ByVal templateName As String, ByVal templateContent As String)
Try
If editingFrame.Verb.Text.StartsWith("ChildLayout") Then
Dim verbText As String = editingFrame.Verb.Text.Trim
Dim startindex As Integer = verbText.IndexOf(" ") + 1
Dim length As Integer = verbText.IndexOf(" ", startindex) - startindex
Dim LayoutNo As Integer = verbText.Substring(startindex, length)
startindex = verbText.IndexOf("[") + 1
length = verbText.IndexOf("]") - startindex
Dim ColumnNo As Integer = verbText.Substring(startindex, length)
Dim Tcol As New TemplateColumn
Tcol = CType(Me.Component, MultiLevelGrid).ChildLayouts.Item(LayoutNo).Columns(ColumnNo)
If templateName = "HeaderTemplate" Then
If Not IsNothing(templateContent) Then
Tcol.HeaderTemplate = GetTemplateFromText(templateContent)
Else
Tcol.HeaderTemplate = Nothing
End If
ElseIf templateName = "ItemTemplate" Then
If Not IsNothing(templateContent) Then
Tcol.ItemTemplate = GetTemplateFromText(templateContent)
Else
Tcol.ItemTemplate = Nothing
End If
ElseIf templateName = "EditItemTemplate" Then
If Not IsNothing(templateContent) Then
Tcol.EditItemTemplate = GetTemplateFromText(templateContent)
Else
Tcol.EditItemTemplate = Nothing
End If
ElseIf templateName = "FooterTemplate" Then
If Not IsNothing(templateContent) Then
Tcol.FooterTemplate = GetTemplateFromText(templateContent)
Else
Tcol.FooterTemplate = Nothing
End If
End If
Else
MyBase.SetTemplateContent(editingFrame, templateName, templateContent)
End If
Catch ex As Exception
MsgBox(ex.Message)
End Try

End Sub
End Class


I am Confused. Please Help me..It's very Urgent

[42639 byte] By [InduAswathi] at [2007-12-22]