I need to populate a dropdown list(DD_CostCodeEdit) in my gridview
based on a textbox(job) value in the gridview.  I already have code to
populate the dropdown list on the textbox changed event but I also
need to populate it when in edit mode. I'm having trouble tweaking the
textbox changed event code to work in the row editing event code.
Could someone help me out?
I've posted my textbox changed code below:

Protected Sub Job_TextChanged(ByVal sender As Object, ByVal e As
System.EventArgs)

        'Get the row of the gridview based on the text box that raised
this event (realize that there is 1 textbox that raises this event in
each row)
        Dim jobTextBox As TextBox = DirectCast(sender, TextBox)
        'Create a local dropdown instance
        Dim costcodeDDL As KeySortDropDownList.KeySortDropDownList =
Nothing
        Dim jobNumber As String = ""

        'Loop thru the rows in the grid view to...
        For Each row As GridViewRow In Me.TimecardGridView.Rows
            '... find the job text box
            Dim jobNoTextBox As TextBox = CType(row.FindControl
("job"), TextBox)
            If jobNoTextBox IsNot Nothing Then
                'compare the Unique IDs to ensure we found the correct
row in the grid view then...
                If jobNoTextBox.UniqueID = jobTextBox.UniqueID Then
                    '...get a reference to the costcodeDDL so we can
bind the correct data to it (based on the value in job textbox)

                    costcodeDDL = CType(row.FindControl
("DD_CostCodeEdit"), DropDownList)
                    jobNumber = jobNoTextBox.Text
                    Exit For
                End If
            End If
        Next

        'Lets make sure we have a costcode drop down, if not, throw an
exception since there is a fatal error
        Try
            If costcodeDDL Is Nothing Then
                Throw New Exception("CostCode drop down list is null")
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try

        Dim oConn As New System.Data.SqlClient.SqlConnection
(ConfigurationManager.ConnectionStrings
("CMSConnectionString").ConnectionString)
        Dim sql As New System.Data.SqlClient.SqlCommand()

        sql.Connection = oConn
        oConn.Open()
        If jobNumber.Length > 0 Then
            sql.Parameters.AddWithValue("jobNumber", jobNumber)
            sql.CommandText = "SELECT CJCDI FROM dbo.Job_Cost_Codes
Where gjb...@jobnumber Order by CJCDI ASC"


            Try
                Dim reader As System.Data.SqlClient.SqlDataReader =
sql.ExecuteReader()
                'Now the user could have entered in a value that does
not exist, again no need to choke, just re-execute the reader and get
all the values
                If reader.HasRows Then
                    costcodeDDL.DataSource = reader
                    costcodeDDL.DataBind()
                Else

                    costcodeDDL.Items.Insert(0, "")

                End If

            Catch ex As Exception
                MsgBox(ex.Message)
            Finally
                oConn.Close()
            End Try
        End If

    End Sub

Reply via email to