none
GridView 選取後刪除,判斷是否已選取 RRS feed

  • 問題

  • 小弟最近才開始接觸asp.net
    這二天自已寫了一個簡單的GridView選取、刪除、選取

    在查詢後的GridView是正常的
    刪除則一定要先選取才能刪除該筆資料行
    現在遇到的問題...
    如果不先選取直接按刪除則會跳錯
    有試著去寫判斷空值,但是還是會跳錯
    爬了不少文,大多都是checkbox配上刪除

    請各位前輩指點一下小弟
    感謝

    程式碼如下:

    Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
    
    
    
    
    
    
    
      Dim i As String = Nothing
    
    
    
      i = GridView1.SelectedDataKey.Value '選取的資料行 DataKeyNames:ID
    
    
    
    
    
    
    
      If i Is Nothing = False Then
    
    
    
    
    
    
    
      Dim dbCon As OleDbConnection = Nothing
    
    
    
      dbCon = New OleDbConnection("連線字串")
    
    
    
      dbCon.Open()
    
    
    
    
    
    
    
      Dim dbCmd As OleDbCommand
    
    
    
      Dim dbRead As Integer
    
    
    
      dbCmd = New OleDbCommand(("DELETE table_menu WHERE id='" & i & "'"), dbCon)
    
    
    
      dbRead = dbCmd.ExecuteNonQuery
    
    
    
      dbCon.Close()
    
    
    
    
    
    
    
      binddata() '資料重新繫結
    
    
    
    
    
    
    
      End If
    
    
    
    
    
    
    
    End Sub
    
    
    
    
    
    
    
    

     


    to:tihs 大大

    我的DataSource是自已寫的,發問時忘了先說一聲...sor

    因為有關聯到二個table,而且我不是用sql的db,我用oracle的db,怕用工具會出錯^^"

    還是謝謝你了^^

    to:91 大大

    自已測試測出來了...感謝

    Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
    
    
    
    
    
    
    
      Dim i As String = Nothing
    
    
    
      'i = GridView1.SelectedDataKey.Value '這行改掉
      i = GridView1.DataKeys(e.RowIndex).Value.ToString().Trim()
    
    
    
    
    
    
      If i Is Nothing = False Then
    
    
    
    
    
    
    
      Dim dbCon As OleDbConnection = Nothing
    
    
    
      dbCon = New OleDbConnection("連線字串")
    
    
    
      dbCon.Open()
    
    
    
    
    
    
    
      Dim dbCmd As OleDbCommand
    
    
    
      Dim dbRead As Integer
    
    
    
      dbCmd = New OleDbCommand(("DELETE table_menu WHERE id='" & i & "'"), dbCon)
    
    
    
      dbRead = dbCmd.ExecuteNonQuery
    
    
    
      dbCon.Close()
    
    
    
    
    
    
    
      binddata() '資料重新繫結
    
    
    
    
    
    
    
      End If
    
    
    
    
    
    
    
    End Sub
    
    

     

    • 已編輯 田各 2011年4月13日 上午 02:06
    2011年4月12日 上午 09:16

解答

所有回覆