Dynamic Gridview - ITemplate bound dropdownlist

Posted on 2011-03-06
Medium Priority
Last Modified: 2013-11-27
I am building a dynamic gridview, and adding a bound dropdownlist to each column header.
This works perfectly.  However, I cannot find the dropdownlist values, after a postback.  I build the
gridview with one button, and find the information after another button.  I have
searched everywhere, and I cannot find a solution that fits.  How do I find the selected values from the
drop down lists in the dynamic gridview?

Here is the important code:
'Parse the text file into lines and columns.
    'Load into dataset, and bind to column mapping datagrid
    Protected Sub ProcessImportFile(ByVal ImportFilePath As String, ByVal FileDelimeter As String)

        'The DataSet to Return
        Dim result As New DataSet()

        Dim File As String = ImportFilePath
        Dim delimiter As String = FileDelimeter
        Dim TableName As New DataTable
        Dim i As Integer = 0

        'Open the file in a stream reader.
        Dim s As New StreamReader(File)

        'Split the first line into the columns      
        Dim columns As String() = s.ReadLine().Split(delimiter.ToCharArray())

        'Add the new DataTable to the RecordSet

        For Each col As String In columns
            result.Tables(0).Columns.Add("Column" & i)
            i += 1

        'Read all of the data in the file.        
        Dim AllData As String = s.ReadToEnd()

        'Split off each row at the Carriage Return/Line Feed
        'Default line ending in most windows exports.  
        'You may have to edit this to match your particular file.
        'This will work for Excel, Access, etc. default exports.
        Dim rows As String() = AllData.Split(vbCr & vbLf.ToCharArray())

        'Now add each row to the DataSet        
        For Each r As String In rows
            'Split the row at the delimiter.
            Dim items As String() = r.Split(delimiter.ToCharArray())

            'Add the item


    End Sub

    'Creates the Column Mapping gridview
    Protected Sub CreateGridView(ByVal TheDataSet As DataSet)
        Dim i As Integer = 0
        Dim ds As New DataSet()
        ds = TheDataSet
        Dim strAppName As String = Me.ddlApplications.SelectedValue.ToString
        Dim strClientID As String = Nothing
        If ds.Tables(i).Rows.Count > 0 Then
            Dim gvColumnMapping As New GridView()
            gvColumnMapping.ID = "gvColumnMapping"
            gvColumnMapping.Width = Unit.Pixel(700)
            gvColumnMapping.BorderWidth = Unit.Pixel(0)
            gvColumnMapping.AutoGenerateColumns = False
            gvColumnMapping.ShowFooter = True

            For e As Integer = 0 To ds.Tables(0).Columns.Count - 1
                Dim tf As TemplateField = Nothing

                tf = New TemplateField()
                tf.HeaderTemplate = New DynamicGridViewTextTemplate(ds.Tables(i).Columns(e).ColumnName.ToString, strAppName, DataControlRowType.Header)
                tf.ItemTemplate = New DynamicGridViewTextTemplate(ds.Tables(i).Columns(e).ColumnName.ToString, strAppName, DataControlRowType.DataRow)
                tf.FooterTemplate = New DynamicGridViewTextTemplate(DataControlRowType.Footer, strAppName, ds.Tables(0).Rows.Count)

                'strClientID = gvColumnMapping.FindControl("Column" & e).ClientID

            'AddHandler gvColumnMapping.RowDataBound, New GridViewRowEventHandler(AddressOf Me.DynamicGrid_RowDataBound)
            'AddHandler gvColumnMapping.Init, New EventHandler(AddressOf Me.gvColumnMapping_Init)

            gvColumnMapping.DataSource = ds.Tables(0)
        End If

    End Sub

    'Protected Sub DynamicGrid_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    '    If e.Row.RowType = DataControlRowType.Footer Then
    '    End If
    'End Sub

    'Get the import file path and delimeter
    'And call processimportfile, so the column mapping gridview can be created
    Protected Sub btnLoadColumnMapping_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnLoadColumnMapping.Click
        Dim strImportFilePath As String
        Dim strDelimeter As String
        strImportFilePath = Me.fuImportFile.PostedFile.FileName
        Me.lblFileUploadFile.Text = strImportFilePath
        strDelimeter = ","
        ProcessImportFile(strImportFilePath, strDelimeter)

    End Sub

End Class

Public Class DynamicGridViewTextTemplate
    Implements ITemplate
    Private _ColName As String
    Private _rowType As DataControlRowType
    Private _Count As Integer
    Private _AppName As String

    Public Sub New(ByVal ColName As String, ByVal AppName As String, ByVal RowType As DataControlRowType)
        _ColName = ColName
        _rowType = RowType
        _AppName = AppName
    End Sub
    Public Sub New(ByVal RowType As DataControlRowType, ByVal ArticleCount As Integer)
        _rowType = RowType
        _Count = ArticleCount
    End Sub
    Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements ITemplate.InstantiateIn
        Select Case _rowType
            Case DataControlRowType.Header
                Dim ddl As New DropDownList()
                ddl.ID = _ColName
                AddHandler ddl.DataBinding, New EventHandler(AddressOf Me.ddl_DataBind)
                Exit Select
            Case DataControlRowType.DataRow
                Dim lbl As New Label()
                AddHandler lbl.DataBinding, New EventHandler(AddressOf Me.lbl_DataBind)
                Exit Select
            Case DataControlRowType.Footer
                Dim flc As New Literal()
                flc.Text = "<b>Total No Items" & _Count & "</b>"
                Exit Select
            Case Else
                Exit Select
        End Select
    End Sub

    Private Sub lbl_DataBind(ByVal sender As [Object], ByVal e As EventArgs)
        Dim lbl As Label = DirectCast(sender, Label)
        Dim row As GridViewRow = DirectCast(lbl.NamingContainer, GridViewRow)
        lbl.Text = DataBinder.Eval(row.DataItem, _ColName).ToString()
    End Sub

    Private Sub ddl_DataBind(ByVal sender As [Object], ByVal e As EventArgs)
        Dim ddl As DropDownList = DirectCast(sender, DropDownList)
        Dim row As GridViewRow = DirectCast(ddl.NamingContainer, GridViewRow)
        Dim ds As New DataSet
        ds = SelectAllAttributeNames()
        ddl.DataSource = ds.Tables(0)
        ddl.DataTextField = ds.Tables(0).Columns(0).ColumnName.ToString
        ddl.DataValueField = ds.Tables(0).Columns(0).ColumnName.ToString
    End Sub

    Private Function SelectAllAttributeNames() As DataSet
        Dim ds As New DataSet
        Dim cbApplication As New cBApplication
        cbApplication.AppName = _AppName
        ds = cbApplication.SelectAllAssignedAttributes()
        Return ds
    End Function
Question by:frontback45
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 35055200
try this :-

 Dim ddl1 As DropDownList = CType(GridView1.HeaderRow.FindControl("ddl1"), DropDownList)

Open in new window


Author Comment

ID: 35058259
I  put this in the second button click event, and I receive the following error:

Object reference not set to an instance of an object.
LVL 33

Accepted Solution

raterus earned 750 total points
ID: 35062315
Do you understand how dynamic controls work in asp.net?  They aren't persisted, so when the page posts back, you have to recreate them just like they were or they will disappear.  I don't ever recommend their usage unless they are absolutely needed.


There is still the Request.Form collection, and while I don't normally recommend it's usage, if you just need a single value they changed, and you know where they changed it, you can use Request.Form("MyControlUniqueID").  You'll still have to rebuild the gridview to update according to the change, but it can save you a ton of time.
Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.


Assisted Solution

xiong8086 earned 750 total points
ID: 35080169
you need to bind data to the dropdown list in RowDataBound event of the gridview when your page was posted back.


Author Comment

ID: 35084969
Thanks for all responses.  Since there is not a good solution for this issue, I am going to try a different approach.  Just an FYI to anyone else looking at this issue... MSDN provided this approach, but failed to mention there is not a "good" way to get values from the gridview, after post-back.  I am going to split the points.

Author Closing Comment

ID: 35084991
I wish someone would have offererd an alternative approach.
LVL 33

Expert Comment

ID: 35085175
For an alternative, typically I use asp:repeaters nested within each other to draw the table as required, that way I don't have to worry about recreating controls again on postback.  You don't get all the fancy features of the gridview, and it gets confusing in it's own way.

You could also use a custom control which is more like you were doing, you just embed all the dynamic nature into the control, which automatically will recreate controls on a postback, but this has a steep learning curve if you are unfamiliar with this process.

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

New Relic: Our company recently started researching several products to figure out what were the best ways for us to increase our web page speed and to quickly identify performance problems that we may be having. One of the products we evaluated wa…
A publishing tool, a Version Control System, or a Collaboration Platform! These can be some of the defining words for the two very famous web-hosting Git repositories: Bitbucket and Github. Git is widely used amongst the programmers and developers f…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question