deloreandude
New member
- Joined
- Sep 21, 2008
- Messages
- 4
- Programming Experience
- 3-5
Hey everyone,
I have a custom gridview extension class in .net 4.0 (aspx). The class handles sorting events etc as well as displaying the header and footer when the datasource is empty.
This works great except that in my onrowdatabound event I find and add options to dropdowns (Footer is insert row), which works great if there is a datasource. If there is no datasource and I call initialise row for the footer myself the controls are not bound and are left as empty dropdowns.
How can I solve this please. See code section below.
Gridview code
-------------
Code in page to populate dropdownlists
-----------------------------------
Any assistance would be great. Thanks in advance.
I have a custom gridview extension class in .net 4.0 (aspx). The class handles sorting events etc as well as displaying the header and footer when the datasource is empty.
This works great except that in my onrowdatabound event I find and add options to dropdowns (Footer is insert row), which works great if there is a datasource. If there is no datasource and I call initialise row for the footer myself the controls are not bound and are left as empty dropdowns.
How can I solve this please. See code section below.
Gridview code
-------------
VB.NET:
Protected Overrides Function CreateChildControls(dataSource As System.Collections.IEnumerable, dataBinding As Boolean) As Integer
' Create controls getting count of rows created
Dim createdRows As Integer = MyBase.CreateChildControls(dataSource, dataBinding)
'If numRows = 0 AndAlso ShowWhenEmpty Then
If createdRows = 0 AndAlso Me._showWhenEmpty Then
Dim table As New Table() With {.ID = MyBase.ID}
Dim fields(MyBase.Columns.Count - 1) As DataControlField
' Copy the exisiting columns into array and initialize
MyBase.Columns.CopyTo(fields, 0)
' If header to be shown
If MyBase.ShowHeader Then
' Create header row
Dim headerRow As GridViewRow = MyBase.CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal)
MyBase.InitializeRow(headerRow, fields)
table.Rows.Add(headerRow)
End If
' If emptydatatemplate not nothing
If Not MyBase.EmptyDataTemplate Is Nothing Then
' Create empty data row
Dim emptyRow As GridViewRow = New GridViewRow(-1, -1, DataControlRowType.EmptyDataRow, DataControlRowState.Normal)
Dim cell As New TableCell() With {.ColumnSpan = MyBase.Columns.Count, _
.Width = Unit.Percentage(100)}
EmptyDataTemplate.InstantiateIn(cell)
emptyRow.Cells.Add(cell)
table.Rows.Add(emptyRow)
End If
' If footer to be shown
If MyBase.ShowFooter Then
' Create footer row
Dim footerRow As GridViewRow = MyBase.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Insert)
MyBase.InitializeRow(footerRow, fields)
OnRowCreated(New GridViewRowEventArgs(footerRow))
table.Rows.Add(footerRow)
End If
MyBase.Controls.Clear()
' Add table to grid
MyBase.Controls.Add(table)
End If
' Return count of rows created
Return createdRows
End Function
Code in page to populate dropdownlists
-----------------------------------
VB.NET:
Protected Sub grvData_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles grvData.RowDataBound
If e.Row.RowType = DataControlRowType.Footer Then
' Area Group dropdown
Dim ddlAreaGroup As DropDownList = DirectCast(e.Row.FindControl("ddlAreaGroup"), DropDownList)
BindAreaGroupsToDDL(ddlAreaGroup)
' Layers dropdown
Dim ddlLayer As DropDownList = DirectCast(e.Row.FindControl("ddlLayer"), DropDownList)
BindLayersToDDL(ddlLayer)
End If
End Sub
Any assistance would be great. Thanks in advance.