title: Dynamically populating a DropDown Template Control in a Gridview
body: <p>I have a GRIDVIEW with columns, one of which contains a CSV of possible values for the dropdown that must appear on each row.</p>
  
  <pre><code>   Private Sub GridViewParameters_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridViewParameters.RowDataBound
  
          If e.Row.RowType = DataControlRowType.DataRow Then
              'Get value of third column. Index is zero based, to 
              'get text of third column we use Cells[2].Text
              Dim CellValue As Integer = Convert.ToSingle(e.Row.Cells(0).Text)
              Dim ntextbox As New TextBox
              Dim ncheckbox As New CheckBox
              Dim ndropdown As New DropDownList
              Dim CellType As String = Convert.ToString(e.Row.Cells(3).Text)
              'MsgBox(CellType)
              Dim DropDownItems(0) As String
              Dim cnt As Integer
              Dim marker As Integer
              Dim arraydim As Integer = 0
              'MsgBox(Convert.ToString(e.Row.Cells(2).Text))
              If Trim(CellType) = "select" Then
                  'e.Row.Cells(2).Controls.Remove(ntextbox)
                  'e.Row.Cells(2).Controls.Add(ndropdown)
                  If Len(Trim(Convert.ToString(e.Row.Cells(4).Text))) &gt; 0 Then
                      For cnt = 1 To Len(Trim(Convert.ToString(e.Row.Cells(4).Text)))
                          If Mid(Trim(Convert.ToString(e.Row.Cells(4).Text)), cnt, 1) = "," Then
                              'e.Row.Cells(2).Controls.
                              'MsgBox("lll")
                          End If
                      Next
                      arraydim = arraydim + 1
                      ReDim Preserve DropDownItems(arraydim)
                      DropDownItems(arraydim) = Mid(Trim(Convert.ToString(e.Row.Cells(4).Text)), marker + 1, cnt - marker - 1)
                  End If
                  e.Row.Cells(5).Controls.Item(5).Visible = False
                  e.Row.Cells(5).Controls.Item(1).Visible = False
                  e.Row.Cells(5).Controls.Item(2).Visible = False
                  e.Row.Cells(5).Controls.Item(3).Visible = True 'select drop down
                  'e.Row.Cells(5).Controls.Item(3).Controls.
              End If
              If Trim(CellType) = "textbox" Then
                  'e.Row.Cells(2).Controls.Add(ntextbox)
                  'e.Row.Cells(2).Text = Convert.ToString(e.Row.Cells(3).Text)
                  e.Row.Cells(5).Controls.Item(5).Visible = True
                  e.Row.Cells(5).Controls.Item(1).Visible = False
                  e.Row.Cells(5).Controls.Item(2).Visible = False ' textbox
                  e.Row.Cells(5).Controls.Item(3).Visible = False
                  'e.Row.DataItem(
              End If
              If Trim(CellType) = "boolean" Then
                  'e.Row.Cells(2).Controls.Add(ndropdown)
                  'e.Row.Cells(2).Text = Convert.ToString(e.Row.Cells(3).Text)
                  e.Row.Cells(5).Controls.Item(5).Visible = False
                  e.Row.Cells(5).Controls.Item(1).Visible = True 'checkbox
                  e.Row.Cells(5).Controls.Item(2).Visible = False
                  e.Row.Cells(5).Controls.Item(3).Visible = False
              End If
              If Trim(CellType) = "hidden" Then
                  e.Row.Visible = False
              End If
              'e.Row.Cells(2).Controls.Remove(ntextbox)
  
              '' If value is greater of 10, change format
              If CellValue &gt; 0 Then
                  ' Use this syntax to change format of complete row
                  e.Row.BackColor = System.Drawing.Color.LightGreen
                  e.Row.Cells(0).ForeColor = System.Drawing.Color.LightGreen
                  e.Row.Cells(3).ForeColor = System.Drawing.Color.LightGreen
                  'e.Row.Cells(5).ForeColor = System.Drawing.Color.LightGreen
              Else
                  e.Row.BackColor = System.Drawing.Color.LightPink
                  e.Row.Cells(0).ForeColor = System.Drawing.Color.LightPink
                  e.Row.Cells(3).ForeColor = System.Drawing.Color.LightPink
                  'e.Row.Cells(5).ForeColor = System.Drawing.Color.LightPink
  
                  ' Use this syntax to change format of single cell
                  'e.Row.Cells(2).BackColor = System.Drawing.Color.Red
              End If
          End If
      End Sub
  </code></pre>
  
  <p>Depending on whether the type of control is a dropdown, textbox, or check box (feined in the table) it hides or shows the relevant control type. If it is a dropdown it needs to get its values from a CSV in a corresponding column. I have a SP that returns a these in table form if passed the id of the relevant row.</p>
  
  <p>How do I get that is from the gridview to pass to the drop down and load it correctly?</p>
  
  <p>Thanks</p>