Click here to Skip to main content
15,860,943 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
please i need urget help
i am messed up with my drop down list
i have gride view in which data table with text boxes and drop down list when i choose value in ddl and write text in text boxes the click add row it preserve the text box values and loose selected item value
first the data is loaded from database more than 1 time

second i can not capture the selected index my code i wrote nothing in my Page_Load sub

VB
Private Function GetData(ByVal query As String) As DataSet
    da = clnconn.MyDataAdapterclnc(query)
    da.Fill(ds)

    Return ds

End Function
Protected Sub OnRowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
    If (e.Row.RowType = DataControlRowType.DataRow) Then
        'Find the DropDownList in the Row
        Dim dropjobqs As DropDownList = CType(e.Row.FindControl("dropjobqs"), DropDownList)
        Try
            dropjobqs.DataTextField = "JBNME"
            dropjobqs.DataValueField = "JBCDE"
            dropjobqs.DataSource = GetData("Select JBNME,JBCDE from JOBBSDB")
            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
Private Sub AddNewRowToGrid()
    Dim rowIndex As Integer = 0

    If ViewState("CurrentTable") IsNot Nothing Then
        Dim dtCurrentTable As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
        Dim drCurrentRow As DataRow = Nothing
        If dtCurrentTable.Rows.Count > 0 Then
            For i As Integer = 1 To dtCurrentTable.Rows.Count
                'extract the TextBox values
                Dim dropjobqs As DropDownList = DirectCast(Gridview1.Rows(rowIndex).Cells(6).FindControl("dropjobqs"), DropDownList)
                Dim box1 As TextBox = DirectCast(Gridview1.Rows(rowIndex).Cells(1).FindControl("empnum"), TextBox)
                Dim box2 As TextBox = DirectCast(Gridview1.Rows(rowIndex).Cells(2).FindControl("empnme"), TextBox)
                Dim box3 As TextBox = DirectCast(Gridview1.Rows(rowIndex).Cells(3).FindControl("brnch"), TextBox)
                Dim chkdelete As CheckBox = DirectCast(Gridview1.Rows(rowIndex).Cells(4).FindControl("chkdelete"), CheckBox)
                Dim chkdental As CheckBox = DirectCast(Gridview1.Rows(rowIndex).Cells(5).FindControl("chkdental"), CheckBox)
                Dim box4 As TextBox = DirectCast(Gridview1.Rows(rowIndex).Cells(4).FindControl("tel"), TextBox)
                Dim box5 As TextBox = DirectCast(Gridview1.Rows(rowIndex).Cells(5).FindControl("TextBox5"), TextBox)
                drCurrentRow = dtCurrentTable.NewRow()
                drCurrentRow("EBRNNME") = i + 1
                dtCurrentTable.Rows(i - 1)("Column1") = box1.Text
                dtCurrentTable.Rows(i - 1)("Column2") = box2.Text
                dtCurrentTable.Rows(i - 1)("Column3") = box3.Text
                dtCurrentTable.Rows(i - 1)("Column4") = box4.Text  'the value between brackets is where the value of checked box will be written
                dtCurrentTable.Rows(i - 1)("Column5") = box5.Text  'the value between brackets is where the value of checked box will be written
                dtCurrentTable.Rows(i - 1)("dropjobqs") = dropjobqs.SelectedItem
                box3.Enabled = False
                box5.Enabled = False
                rowIndex += 1
            Next

            dtCurrentTable.Rows.Add(drCurrentRow)
            ViewState("CurrentTable") = dtCurrentTable
            Gridview1.DataSource = dtCurrentTable
            Gridview1.DataBind()
        End If
    Else
        Response.Write("ViewState is null")
    End If
    SetPreviousData()
End Sub
Private Sub SetPreviousData()
    Dim rowIndex As Integer = 0
    If ViewState("CurrentTable") IsNot Nothing Then
        Dim dt As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)

        If dt.Rows.Count > 0 Then
            For i As Integer = 0 To dt.Rows.Count - 1
                Dim dropjobqs As DropDownList = DirectCast(Gridview1.Rows(rowIndex).Cells(6).FindControl("dropjobqs"), DropDownList)
                Dim box1 As TextBox = DirectCast(Gridview1.Rows(rowIndex).Cells(1).FindControl("empnum"), TextBox)
                Dim box2 As TextBox = DirectCast(Gridview1.Rows(rowIndex).Cells(2).FindControl("empnme"), TextBox)
                Dim box3 As TextBox = DirectCast(Gridview1.Rows(rowIndex).Cells(3).FindControl("brnch"), TextBox)
                Dim chkdelete As CheckBox = DirectCast(Gridview1.Rows(rowIndex).Cells(4).FindControl("chkdelete"), CheckBox)
                Dim chkdental As CheckBox = DirectCast(Gridview1.Rows(rowIndex).Cells(5).FindControl("chkdental"), CheckBox)
                Dim box4 As TextBox = DirectCast(Gridview1.Rows(rowIndex).Cells(4).FindControl("tel"), TextBox)
                Dim box5 As TextBox = DirectCast(Gridview1.Rows(rowIndex).Cells(5).FindControl("TextBox5"), TextBox)
                ' box5.Visible = True
                box3.Enabled = False
                'box4.Visible = True
                ' box5.Enabled = False
                box1.Text = dt.Rows(i)("Column1").ToString()
                box3.Text = dt.Rows(i)("Column3").ToString()
                box2.Text = dt.Rows(i)("Column2").ToString()
                box4.Text = dt.Rows(i)("Column4").ToString()  'write the value of checked box in box4
                box5.Text = dt.Rows(i)("Column5").ToString()
                dropjobqs.SelectedIndex = ViewState(dropjobqs.UniqueID) Is Nothing
                chkdelete.Checked = ViewState(chkdelete.UniqueID) IsNot Nothing
                chkdental.Checked = ViewState(chkdental.UniqueID) IsNot Nothing
                'the coming lines to make the checkbox uneditable if checked
                If ViewState(dropjobqs.UniqueID) IsNot Nothing Then
                    dropjobqs.SelectedValue = ViewState(dropjobqs.UniqueID)
                End If
                'If ViewState(chkdental.UniqueID) IsNot Nothing Then
                'chkdental.Enabled = False
                'End If
                rowIndex += 1
            Next
        End If
    End If
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

            'ViewState.Remove(dropjobqs.UniqueID)

            ' End If

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


    End If
End Sub
please its urgent
Posted
Updated 5-Jun-13 23:39pm
v2
Comments
Aydin Homay 6-Jun-13 5:40am    
Hi you should check IsPostBack on FormLoad event before than binding ddl ;)
shery_l 6-Jun-13 5:41am    
when i add grideview.databind
in page load
it give error"
"A field or property with the name 'JBNME' was not found on the selected data source"
shery_l 9-Jun-13 5:09am    
would u please tell me in details
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
lbldte.Text = DateAndTime.Now.ToShortDateString
End If
If Not Page.IsPostBack Then
Generate_Code("select Case DCCDE When NULL Then 0 END from ADDRCNT")
GVRNT.DataSource = GetData("Select CODE,CNAME from EMPDIC where KEY='10' order by code")
GVRNT.DataBind()
SPCLNME.DataSource = GetData_Major("select distinct spclcde, spclnme||''||spclnmea from adnwspcl order by spclnme||''||spclnmea asc")
SPCLNME.DataBind()
DSTRCT.DataSource = GetData_District("Select CODE,CNAME from EMPDIC where KEY='10' order by code")
DSTRCT.DataBind()
CTY.DataSource = GetData_City("Select CODE,CNAME from EMPDIC where KEY='10' order by code")
CTY.DataBind()
End If
End Sub
Private Function GetData_Major(ByVal query As String) As DataSet
da = clnconn.MyDataAdapterclnc(query)
da.Fill(ds)
Try
SPCLNME.DataTextField = "spclnme||''||spclnmea"
SPCLNME.DataValueField = "SPCLCDE"
'SPCLNME.DataBind()
'Add Default Item in the DropDownList
SPCLNME.Items.Insert(0, New ListItem("Please selectjhkkjh-----"))
Catch ex As NullReferenceException
Response.Write("Processor Usage" + ex.Message)
'the error in start generated here
End Try
Return ds

End Function

1 solution

Use:
VB
dropjobqs.DataSource = GetData("Select DISTINCT JBNME,JBCDE from JOBBSDB")


More about: SELECT DISTINCT (T-SQL)[^]
 
Share this answer
 
Comments
shery_l 6-Jun-13 5:57am    
where should i use "dropjobqs.DataSource = GetData("Select DISTINCT JBNME,JBCDE from JOBBSDB")"

plus the problem is it reload from database
i mean first time it load data once
on add row button clicked
it load data twice
and on add row clicked thisrd time it load 3 times and so on
Maciej Los 6-Jun-13 5:59am    
Don't you know where?
Is this your code or not?
Have a look at your code and find it!
shery_l 6-Jun-13 6:02am    
am new in this so things get messed up
could not find it
tht is why am posting...........
plus i have another problem tht it does not preserve the selected index
could u help
Maciej Los 6-Jun-13 6:08am    
Have a look inside OnRowDataBound procedure ;)
shery_l 6-Jun-13 6:09am    
ok tell me where
can not find it :(

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