close

1.        先將GridView的該欄位轉為Template,並且在EditItemTemplate>裡建立一個DropDownList來秀資料,

 再建立一個label來保留原資料,該Label設為不顯現於GridView上



asp:TemplateField HeaderText="課程類別序號">

    EditItemTemplate>   --編輯時使用的欄位

      asp:DropDownList ID="DpClasskind" runat="server">

      asp:DropDownList>

      asp:Label ID="LabClassKind" runat="server" Text='# Bind("ClassKindID") %>' 

        Visible
="False">asp:Label>

    EditItemTemplate>

    ItemStyle Wrap="False" /> --GridView在秀的時候使用的欄位


        asp:Label ID="LabClassKind" runat="server" Text='# Bind("ClassKindID") %>'>asp:Label       

       
ItemTemplate


    asp:TemplateField>

 2.    先建立好連線,並且放在DataTable裡以供DropDownList連接用 



    Public Sub LoadDpList() 

        --取出連線字串的Function,這是我們設定的哦

       Vconn = New SqlConnection(GetSqlConnStr("Certification")) 

       vCmd.Connection = Vconn 

       strSql = " SELECT ClassKIndID ,ClassKindName  from TrainingHour order by ClassKIndID" 

       Try 

            Vconn.Open() 

            vCmd.CommandText = strSql 

            Vreader = vCmd.ExecuteReader() 

            vDT1.Load(Vreader)  --將查出來的資料,先放在DataTable中

            Vreader.Close() 

        Catch ex As Exception 

            Response.Write("READ TrainingHour File Err !! " & ex.Message) 

            Response.End() 

        Finally 

            vCmd.Dispose() 

            Vconn.Dispose() 

            Vconn.Close() 

        End Try

    End Sub 



3.  
 
在按下編輯鍵時會觸發 RowEditing事件,故要在這裡將DropDownList的值放入



    Protected Sub GVQuery_RowEditing(ByVal sender As Object,

                 ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs 



      將dropdownlist與資料連結起來-------------------------------------------

      --由GridView中取出欄位,讓按下編輯鍵時,其dropdownlist 的值會停在原來的資料上

        Dim ddlClasskind As DropDownList = Me.GVQuery.Rows(e.NewEditIndex).FindControl("DpClasskind"

        ddlClasskind.Items.Clear() 

        ddlClasskind.DataSource = vDT1 --利用datatable的值提供給dropdownlist

        ddlClasskind.DataValueField = "ClassKindID" 

        ddlClasskind.DataTextField = "ClassKindName" 

        ddlClasskind.DataBind()



     '利用label來保留其值後,再供ddl來比對,設定其dropdownlist 的值要停在原來的資料上

       Dim strTxtOnTheJobDate As Label = Me.GVQuery.Rows(e.NewEditIndex).FindControl("LabClassKind")

       If ddlClasskind.Items.FindByValue(Trim(strTxtOnTheJobDate.Text)) Is Nothing Then

          ddlClasskind.Items.Insert(0, "    ")

       Else

          ddlClasskind.Items.FindByValue(Trim(strTxtOnTheJobDate.Text)).Selected = True

       End If

    End Sub

 





arrow
arrow
    全站熱搜
    創作者介紹
    創作者 可樂果公主 的頭像
    可樂果公主

    聽說幸福…在山的那邊 (浪機子)

    可樂果公主 發表在 痞客邦 留言(2) 人氣()