creating instance of an inner nested datagrid

S

Sue

I've got a web application I'm working on in Visual Studio 2003 in which I
have a nested datagrid. The inner datagrid is the problem child. (code
sample towards end)

Outer datagrid (PositionDG) works fine. For layout purposes, the web
controls (dropdownlists and textboxes) are in a table within a
templatecolumn. The itemcreated sub creates the various controls, and adds
them to tablecells. The itemdatabound sub populates them.

The inner datagrid (EmployeeDG) is embedded in one of the PositionDG's
tablecells, and a table is used for layout putposes here also. There are
itemcreated and itemdatabound subs for this datagrid also. I'm having trouble
getting the EmployeeDG to create itself, much less populate the controls, and
have been trying for a while to find a solution to this problem.

I don't use the container data binding method since almost all the data has
to be massaged before assigning the value to the web controls.

My questions:

Where and how is an instance of the inner datagrid declared? I've added it
to the aspx page so the layout and containers for the web controls
(tablecells and placeholders) are in place. My understanding (correct if
wrong please) is that an instance of the web control is created in the
itemcreated sub, properties set, then added to the container tablecell or
placeholder. Then the itemdatabound sub set the values from the dataset.

If the inner datagrid is added to the outer datagrid so it's layout and
containers for its web controls are in place, then how does one create an
instance of it in the outer datagrid's itemcreated sub?

And where are the handlers for the inner datagrids ItemCreated and
ItemDatabound subs added? I've heard the page_load, the outer datagrid's
itemcreated sub, and the outer datagrid's itemdatabound sub. Adding them in
the page_load doesn't work since the inner datagrid isn't created yet. I've
tried in the
itemcreated and itemdatabound subs, but the inner datagrid's itemcreated and
itemdatabound subs don't fire then.

tia,
Sue

<asp:datagrid id="PositionDG" EnableViewState="true" Runat="server"
Width="100%">
<Columns>
<asp:templatecolumn>
<ItemTemplate>
<asp:table ID="PositionTable" runat="server">
<asp:tablerow>
<asp:TableCell Text="Classification: " />
<asp:TableCell ID="PositionClassificationCell" Runat="server" />
</asp:TableRow>
<asp:tablerow>
<asp:TableCell columnspan=2>
<asp:DataGrid ID="EmployeeDG" Runat="server">
<Columns>
<asp:templatecolumn HeaderText="Employee">
<ItemTemplate>
<asp:table ID="EmployeeTable" runat="server">
<asp:TableRow> <asp:TableCell
CssClass="DescriptionCell" Text="Name: " />
<asp:TableCell ID="EmployeeNameCell" Runat="server" />
</asp:TableRow>
</asp:Table>
</ItemTemplate>
</asp:templatecolumn>
</Columns>
</asp:DataGrid>
</asp:TableCell>
</asp:tablerow>
</asp:Table>
</ItemTemplate>
</asp:templatecolumn>
</Columns>
</asp:datagrid>


code behind:

Protected WithEvents PositionDG As DataGrid

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

private sub loaddata()
PositionDG.datasource = some dataset
PositionDG.databind
end sub

Private Sub PositionDG_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
PositionDG.ItemCreated
Select Case e.Item.ItemType
Case ListItemType.Item, ListItemType.AlternatingItem,
ListItemType.SelectedItem

Dim PositionClassificationCell As TableCell
PositionClassificationCell =
CType(e.Item.FindControl("PositionClassificationCell"), TableCell)

If Not PositionClassificationCell Is Nothing Then
Dim PositionClassification As New DropDownList
With PositionClassification
.ID = "PositionClassification"
.DataSource = Classification_DataSet
.DataValueField = "ID"
.DataTextField = "FullDescription"
.DataBind()
End With
PositionClassificationCell.Controls.Add(PositionClassification)
End If

' ========== how to code this? ==============

Dim EmployeeDG As DataGrid
EmployeeDG = CType(e.Item.FindControl("EmployeeDG"), DataGrid)
If Not EmployeeDG Is Nothing Then
' ???
end if

end select

End Sub


Private Sub PositionDG_ItemDataBound(ByVal sender As System.Object, ByVal e
As System.Web.UI.WebControls.DataGridItemEventArgs) Handles
PositionDG.ItemDataBound

Dim EmployeeDG As DataGrid
EmployeeDG = CType(e.Item.FindControl("EmployeeDG"), DataGrid)
If Not EmployeeDG Is Nothing Then
If Not IsDBNull(e.Item.DataItem("PositionNumber")) Then
EmployeeDG.DataSource = fresh dataset here....
EmployeeDG.DataBind()
End If
End If

end sub

Private Sub EmployeeDG_ItemDataBound(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs) Handles
EmployeeDG.ItemDataBound
Private Sub EmployeeDG_ItemCreated(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs)
 

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,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top