1. 程式人生 > >DateGridView刪除選中行

DateGridView刪除選中行

    機房收費中刪除使用者,要實現刪除選中行,總體思路:先刪除資料庫中的記錄,再顯示在DateGridView中

介面:

           

U層:

Private Sub btnDel_Click(sender As Object, e As EventArgs) Handles btnDel.Click

        Dim userBLL As New UserBLL
        Dim user As New UserEntity
        Dim strUserID As String
        Dim dt As New DataTable
        If cmbLevel.Text = "" Then
            MsgBox("請先選擇使用者級別", vbInformation, "提示")
        Else
            strUserID = DGV1.CurrentRow.Cells("userID").Value.ToString()  '獲取選中行的使用者名稱
            user._userID = strUserID  
            user._level = cmbLevel.Text
            If userBLL.delUser(user) = True The'從資料庫中先刪除記錄
                dt = userBLL.manageUser(user)  '按照選中的級別重新查詢記錄
                DGV1.DataSource = dt
                DGV1.AutoGenerateColumns = False
                DGV1.Refresh()
                MsgBox("刪除成功")
            End If
        End If
    End Sub
End Class

B層:

 ''' <summary>
    ''' 刪除使用者資訊
    ''' </summary>
    ''' <param name="enUser">使用者實體的UserID</param>
    ''' <returns>Boolean</returns>
    ''' <remarks></remarks>
    Public Function delUser(ByVal enUser As UserEntity) As Boolean
        Dim userDAl As New UserDAL
        If userDAl.DelUser(enUser) > 0 Then
            Return True
        Else
            Return False

        End If
    End Function


  ''' <summary>
    ''' 管理使用者,根據級別查詢資訊
    ''' </summary>
    ''' <param name="enUser">使用者實體的Level</param>
    ''' <returns>DataTable</returns>
    ''' <remarks></remarks>
    Public Function manageUser(ByVal enUser As UserEntity) As DataTable
        Dim userDAL As New UserDAL
        Dim dt As New DataTable
        dt = userDAL.ManageUser(enUser)
        Return dt
    End Function

D層:

 ''' <summary>
    ''' 刪除使用者時,根據使用者名稱刪除
    ''' </summary>
    ''' <param name="enUser">使用者實體的UserID</param>
    ''' <returns>受影響的行數</returns>
    ''' <remarks></remarks>
    Public Function DelUser(ByVal enUser As UserEntity) As Integer
        Dim sqlHelper As New SqlHelper
        Dim strCon As String
        strCon = "delete from T_User where userID= '" & enUser._userID & "'"
        Return sqlHelper.ExecAddDelUpdate(strCon, CommandType.Text)
    End Function
End Class

  ''' <summary>
    ''' 管理使用者時選擇級別顯示資訊
    ''' </summary>
    ''' <param name="enUser">使用者實體的Level</param>
    ''' <returns>DataTable</returns>
    ''' <remarks></remarks>
    Public Function ManageUser(ByVal enUser As UserEntity) As DataTable
        Dim sqlHelper As New SqlHelper
        Dim strCon As String
        strCon = "select userID,level,head from T_User where level= '" & enUser._level & "'"
        Return sqlHelper.ExecSelect(strCon, CommandType.Text)
    End Function

   功能的實現先有巨集觀把控,確定整體思路,再具體實現就簡單的多。