Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. How set selected item of dropdownlist in dynamic template columns of Datagrid

How set selected item of dropdownlist in dynamic template columns of Datagrid

Scheduled Pinned Locked Moved ASP.NET
helptutorialquestion
3 Posts 2 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • V Offline
    V Offline
    vivaldi16
    wrote on last edited by
    #1

    I generate template columns in datagrid dynamic and I want dropdownlist in the EditItemTemplate, I did it But I can't set the selected item of the dropdownlist All the dropdownlists are selected at the 1st one item How to set it?Somebody can help me Set when Databinding the dropdownlist?? :-D:-D:-D

    M 1 Reply Last reply
    0
    • V vivaldi16

      I generate template columns in datagrid dynamic and I want dropdownlist in the EditItemTemplate, I did it But I can't set the selected item of the dropdownlist All the dropdownlists are selected at the 1st one item How to set it?Somebody can help me Set when Databinding the dropdownlist?? :-D:-D:-D

      M Offline
      M Offline
      Michela 0
      wrote on last edited by
      #2

      I had the same problem and I solved it in this way. First populate CartView with data, Bind to the datagrid MyDataGrid.DataSource = CartView MyDataGrid.DataBind() Dim i As Integer Dim rowview As DataRowView Dim itm As DataGridItem For Each itm In MyDataGrid.Items rowview = CartView.Item(i) i += 1 ' Find the control in the datagrid Dim lst As System.Web.UI.WebControls.DropDownList lst = itm.FindControl("ListCausali_CL") lst.DataValueField = "ID" lst.DataTextField = "FullName" ' Populate the dropdownlist lst.DataSource = SOMEDATA lst.DataBind() ' Set the selected Item lst.Items.FindByValue(CInt(rowview("ID_Causale_CL"))).Selected = True Next "ListCausali_CL" is the name of the the DDL list in the DataGrid If you have the value in the Datagrid you can find the control and than use it for selecting the item. I had to use my dataview because I didn't have it in the datagrid. Don't put the declaration of lst outside of FOR, you could have problem. Hope it helps you.

      V 1 Reply Last reply
      0
      • M Michela 0

        I had the same problem and I solved it in this way. First populate CartView with data, Bind to the datagrid MyDataGrid.DataSource = CartView MyDataGrid.DataBind() Dim i As Integer Dim rowview As DataRowView Dim itm As DataGridItem For Each itm In MyDataGrid.Items rowview = CartView.Item(i) i += 1 ' Find the control in the datagrid Dim lst As System.Web.UI.WebControls.DropDownList lst = itm.FindControl("ListCausali_CL") lst.DataValueField = "ID" lst.DataTextField = "FullName" ' Populate the dropdownlist lst.DataSource = SOMEDATA lst.DataBind() ' Set the selected Item lst.Items.FindByValue(CInt(rowview("ID_Causale_CL"))).Selected = True Next "ListCausali_CL" is the name of the the DDL list in the DataGrid If you have the value in the Datagrid you can find the control and than use it for selecting the item. I had to use my dataview because I didn't have it in the datagrid. Don't put the declaration of lst outside of FOR, you could have problem. Hope it helps you.

        V Offline
        V Offline
        vivaldi16
        wrote on last edited by
        #3

        You mean that set selected item after Datagrid generated? Can't set selected item of dropdownlist in the controls I Defined myself my code: Public Class DataGridTemplate Implements ITemplate Dim templateType As ListItemType Dim columnName As String Dim Path As String Dim CurrDropValue As String Sub New(ByVal type As ListItemType, ByVal ColName As String, Optional ByVal strPath As String = "", Optional ByVal strCurrDropValue As String = "") templateType = type columnName = ColName Path = strPath CurrDropValue = strCurrDropValue End Sub Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn Dim lcTemp As New Literal() Dim labelTemp As New Label() Dim dropTemp As New DropDownList() Select Case templateType Case ListItemType.Header labelTemp.Text = columnName container.Controls.Add(labelTemp) Case ListItemType.Item container.Controls.Add(lcTemp) AddHandler lcTemp.DataBinding, AddressOf Literal_DataBinding Case ListItemType.EditItem AddHandler dropTemp.Load, AddressOf DropDownList_DataBinding dropTemp.DataTextField = "drop_value" dropTemp.DataValueField = "drop_id" dropTemp.DataSource = CmFunction.s_LoadListData(columnName, Path) dropTemp.DataBind() container.Controls.Add(dropTemp) 'Case ListItemType.Footer ' labelTemp.Text = "Footer" ' container.Controls.Add(labelTemp) End Select End Sub Private Sub Literal_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Dim lcTemp As Literal Dim container As DataGridItem Dim row As DataRowView lcTemp = sender container = lcTemp.NamingContainer row = container.DataItem lcTemp.Text = row.Item(columnName.ToString()) End Sub Private Sub DropDownList_DataBinding(ByVal sender As Object, ByVal e As System.EventArgs) Dim dropTemp As New DropDownList() Dim container As DataGridItem Dim row As DataRowView Dim i As Integer dropTemp = sender dropTemp.Items(1).Selected = True 'It doesnt work:confused::confused::confused: 'Dim data As String 'row = container.DataItem 'data

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups