I have successful generate a dropdownlist inside a datagrid's template edit item. But I don't know why whatever I select in the dropdownlist, it return the result same as before (That means no change at all). I have already spent 3 days and night in it.... Thank you a lot.
Code:
Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim DS As New DataSet
If Not IsPostBack Then
loadDStoSession()
Else
Response.Write("Old session")
End If
DS = Session("scheduleDS")
BindGridtoSource()
End Sub
Sub loadDStoSession()
Response.Write("Session cleared!")
Dim DS As New DataSet
Dim schtable As New DataTable
Dim StartDay, EndDay As Date
StartDay = #7/6/2004#
Dim ConnStr As String = ("Persist Security Info=False; Data Source=localhost; Initial Catalog=SSS; User ID=sa; Password=pwd;")
Dim Conn As New SqlConnection
Conn.ConnectionString = ConnStr
Dim objAdapter As New SqlDataAdapter
objAdapter.SelectCommand = New SqlCommand("Schedule3Query", Conn)
objAdapter.SelectCommand.CommandType = CommandType.StoredProcedure
objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@StartDay", SqlDbType.SmallDateTime))
' The store procedure return 3 tables: 1st is a VIEW, 2nd is a TABLE carrying
'the choice for the dropdownlist, 3rd is the TABLE which the View created
'from.
objAdapter.SelectCommand.Parameters("@StartDay").Value = StartDay
Dim objCB As SqlCommandBuilder = New SqlCommandBuilder(objAdapter)
objAdapter.Fill(DS)
Dim DV As DataView = New DataView(DS.Tables(0))
Session.Add("scheduleDS", DS)
Session.Add("scheduleDV", DV)
End Sub
Sub BindGridtoSource()
Dim DS As New DataSet
DS = Session("scheduleDS")
Dim DV As DataView = New DataView(DS.Tables(0)) editSchDG.DataSource = DV
editSchDG.DataBind()
End Sub
Sub edit_ScheduleEditItem(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
editSchDG.EditItemIndex = e.Item.ItemIndex
BindGridtoSource()
End Sub
Sub cancel_ScheduleEditItem(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
editSchDG.EditItemIndex = -1
BindGridtoSource()
End Sub
Sub update_ScheduleEditItem(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
Dim hNameDropList As New DropDownList
Dim Note As TextBox
Dim hId, i As Integer
hNameDropList = e.Item.FindControl("hNameDropList")
Note = e.Item.Cells(3).Controls(0)
hId = hNameDropList.SelectedValue
Dim schId As Integer = editSchDG.DataKeys(e.Item.ItemIndex)
Response.Write("<BR>hId = " & hId)
Response.Write("<BR>schId = " & schId)
Response.Write("<BR>Note = " & Note.Text)
Dim DS As New DataSet
Dim Dt As New DataTable
DS = Session("scheduleDS")
Dt = DS.Tables(2)
Dim DV As New DataView(Dt)
DV.AllowEdit = True
DV.AllowNew = True
DV.AllowDelete = True
With DV
.RowFilter = "schId =" & schId
End With
For i = 0 To DV.Count - 1
DV(i).Item("hId") = hId
Next
Session.Add("scheduleDV", DV)
Session.Add("scheduleDS", DS)
editSchDG.EditItemIndex = -1
BindGridtoSource()
End Sub
Function GethNameList()
Dim TempDataView As New DataView
Dim DS As New DataSet
Dim Dt As New DataTable
DS = Session("scheduleDS")
TempDataView = DS.Tables(1).DefaultView
Return TempDataView
End Function
Function GethIdIndex(ByVal hId As Integer)
Dim i As Integer
Dim DS As DataSet
DS = Session("scheduleDS")
Dim Dt As New DataTable
Dt = DS.Tables(1)
For i = 0 To Dt.DefaultView.Count - 1
If Dt.DefaultView(i).Item("hId") = hId Then
Return i
End If
Next
Return 0
End Function