Click here to Skip to main content
15,895,084 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i have a grid view which has data tabe inside
there is a column with drop down list
i need to maintain the selected value of dropdown list when adding a row to the table
here is the code
aspx:
ASP.NET
<asp:TemplateField HeaderText="job" Visible="true" >
          <ItemTemplate>
              <asp:DropDownList ID="dropjobqs" runat="server" Enabled="true"   AutoPostBack="true" OnSelectedIndexChanged="dropjobq_CheckedChanged">
              </asp:DropDownList>
              
          </ItemTemplate>
       </asp:TemplateField>

vb:
VB
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.Cache.SetCacheability(HttpCacheability.NoCache)
        setMenu()
        Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache)
        Response.Cache.SetNoStore()
        If Not Page.IsPostBack Then
            strtDate_CalendarExtender.TargetControlID = "strtDate"
            strtDate.CssClass = "~/css/calendar.css"
            strtDate_CalendarExtender.StartDate = DateTime.Now.ToShortDateString
            strtDate_CalendarExtender.EndDate = DateTime.Now.AddDays(1)
            ' Place1.Controls.Add(myCalExt)
            lbldte.Text = DateAndTime.Now.ToShortDateString
            SetInitialRow()
        End If

    End Sub

Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)

        If (e.Row.RowType = DataControlRowType.DataRow) Then
            'Find the DropDownList in the Row
            Try
                Dim dropjobqs As DropDownList = CType(e.Row.FindControl("dropjobqs"), DropDownList)
                dropjobqs.DataSource = GetData("Select JBNME,JBCDE from JOBBSDB")
                dropjobqs.DataTextField = "JBNME"
                dropjobqs.DataValueField = "JBCDE"
                dropjobqs.DataBind()

                'Add Default Item in the DropDownList
                dropjobqs.Items.Insert(0, New ListItem("Please select"))
            Catch ex As NullReferenceException
                Response.Write("Processor Usage" + ex.Message)
                'the error in start generated here
            End Try
        End If
        '   dr.Dispose()

    End Sub

Protected Sub dropjobq_CheckedChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim row As GridViewRow = DirectCast(DirectCast(sender, DropDownList).NamingContainer, GridViewRow)
        Dim index As Integer = row.RowIndex
        Dim dt As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
        Dim rowIndex As Integer = 0
        If dt.Rows.Count > 0 Then
            For i As Integer = 0 To dt.Rows.Count - 1
                Dim dropjobqs As DropDownList = DirectCast(Gridview1.Rows(index).FindControl("dropjobqs"), DropDownList)
                If dropjobqs.SelectedIndex Then
                    ViewState(dropjobqs.UniqueID) = dropjobqs.SelectedIndex

                Else
                    ViewState.Remove(dropjobqs.UniqueID)

                End If
                ' If dropjobqs.SelectedIndex Then
                'ViewState(dropjobqs.UniqueID) = dropjobqs.SelectedIndex

                ' Else
                'ViewState.Remove(dropjobqs.UniqueID)

                ' End If

                rowIndex += 1
                dropjobqs.DataSource = dr
                dropjobqs.DataBind()
            Next


        End If
    End Sub
Private Function GetData(ByVal query As String) As DataSet
        If Not IsPostBack Then

            da = clnconn.MyDataAdapterclnc("Select JBNME,JBCDE from JOBBSDB")
            da.Fill(ds)
        End If
        Return ds

    End Function


please help
Posted
Comments
You are binding the DropDown in OnRowDataBound event.

So, which value you want to select ? You should have some criteria.
shery_l 2-Jun-13 4:12am    
the user select a value from dropdown and click add row to datatable
i need tokeep the selected value in dropdown
__________________________
ONE MORE PROBLEM IS :

on add new row the dropdownlist duplicates each time
here is the code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900