Aspx Gridview event problems


New member
Sep 21, 2008
Programming Experience
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
        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)
                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)}
                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))
                End If
                ' Add table to grid
            End If
            ' Return count of rows created
            Return createdRows
        End Function
Code in page to populate dropdownlists
    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)
            ' Layers dropdown
            Dim ddlLayer As DropDownList = DirectCast(e.Row.FindControl("ddlLayer"), DropDownList)
        End If
    End Sub
Any assistance would be great. Thanks in advance.
Top Bottom