Here's the relevant code. Note that errorHandling and non-relevant
function calls have been removed for (hopefully) easier reading
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
paintPage()
End Sub
Private Sub paintPage()
' removed code gets the number of grids that need to be created
' get rid of existing controls
tblMain.Rows.Clear()
drGet = cmdGet.ExecuteReader
While drGet.Read
' create grid
Dim dg As New DataGrid
' set datagrid attributes, including event handlers
assignDGProperties(dg, drGet("staffHeading"))
' fill w/ data
dg.DataSource = fillGrid(drGet("staffHeading"))
dg.DataBind()
' create new table row
Dim tr As New TableRow
Dim tc As New TableCell
Dim lbl As New Label
lbl.Text = "<b><u>" & drGet("staffHeading") & "</u></b><br>"
'add controls to dynamic table
tc.Controls.Add(lbl)
tc.Controls.Add(dg)
tr.Cells.Add(tc)
tblMain.Rows.Add(tr)
End While
End Sub
Private Sub assignDGProperties(ByVal dg As DataGrid, ByVal heading As
String)
AddHandler dg.ItemCreated, AddressOf dg_ItemCreated
Dim ecc As New EditCommandColumn
With ecc
.ButtonType = ButtonColumnType.LinkButton
.CancelText = "Cancel"
.EditText = "Edit"
.UpdateText = "Update"
End With
dg.Columns.AddAt(0, ecc)
AddHandler dg.EditCommand, AddressOf dg_Edit
AddHandler dg.CancelCommand, AddressOf dg_Cancel
AddHandler dg.UpdateCommand, AddressOf dg_Update
Dim d As New ButtonColumn
With d
.CommandName = "Delete"
.Text = "Del"
End With
dg.Columns.AddAt(1, d)
AddHandler dg.DeleteCommand, AddressOf dg_Delete
' code removed from here adds bound and template columns
' format grid
With dg
.Font.Size = FontUnit.Point(8)
.ID = "dg" & heading
.Width = Unit.Percentage(100)
.AllowPaging = False
.HeaderStyle.Font.Size = New FontUnit(New Unit(8, UnitType.Point))
.HeaderStyle.Font.Name = "Arial"
.HeaderStyle.Font.Bold = True
.HeaderStyle.ForeColor.FromArgb(11, 61, 145)
.HeaderStyle.BackColor.FromArgb(238, 198, 129)
.AutoGenerateColumns = False
.EnableViewState = True
End With
End Sub
Private Function fillGrid(ByVal heading As String)
' build datasource and pass back to datagrid
End Function
Private Sub dg_ItemCreated(ByVal sender As System.Object, ByVal e As
System.Web.UI.WebControls.DataGridItemEventArgs)
' code here sizes columns and controls if listitemtype=edititem
End Sub
Sub dg_Edit(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
' called when edit link in clicked in column(0)
CType(sender, DataGrid).EditItemIndex = e.Item.ItemIndex
CType(sender, DataGrid).DataSource = fillGrid(Right(CType(sender,
DataGrid).ID, Len(CType(sender, DataGrid).ID) - 2))
CType(sender, DataGrid).DataBind()
End Sub
Sub dg_Cancel(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
' called when cancel link in clicked in column(0)
CType(sender, DataGrid).EditItemIndex = -1
CType(sender, DataGrid).DataSource = fillGrid(Right(CType(sender,
DataGrid).ID, Len(CType(sender, DataGrid).ID) - 2))
CType(sender, DataGrid).DataBind()
End Sub
Sub dg_Delete(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
' called when cancel link in clicked in column(0)
CType(sender, DataGrid).EditItemIndex = -1
CType(sender, DataGrid).DataSource = fillGrid(Right(CType(sender,
DataGrid).ID, Len(CType(sender, DataGrid).ID) - 2))
CType(sender, DataGrid).DataBind()
End Sub
Sub dg_Update(ByVal sender As Object, ByVal e As
DataGridCommandEventArgs)
'code here posts changes to database
End Sub