当前位置:编程学习 > VB >>

删除Datagridview选中行并更新数据库

     我们通常会遇到删除dataGridview中选中的一和或都多行数据,在网上查了好多方法,有些只能删除dataGridview中的数据,而不能删除数据库中的源数据,或者有些只能删除选中的一行,而不能同时删除多行,经过一番尝试,终于解决了这个问题:

    实现的功能:可以删除一行或者多行数据,并在删除前提醒是否确定进行删除!

    效果如下:

    1、选中两行,点击删除按钮

   \

   

    2、选择“是”

   \

    3、数据库变化

   

   \

    \

   

    实现代码如下:

    D层:负责更新数据库
[vb] 
''' <summary>  
''' 删除用户信息  
''' </summary>  
''' <param name="enUserInfo">用户信息实体</param>  
''' <returns>布尔值类型</returns>  
''' <remarks></remarks>  
Public Function DelUser(ByVal enUserInfo As Entity.UserInfoEntity) As Boolean 
 
    Dim strSql As String 
    Dim DBHelper As New SqlHelper 
    Dim param As SqlParameter() 
    Dim bln As Boolean 
 
    strSql = "Delete From T_User Where UserID=@ID"                               '删除用户sql语句  
    param = New SqlParameter() {New SqlParameter("@ID", enUserInfo.UserID)}      '给参数ID传值  
    bln = DBHelper.ExecuteUID(strSql, CommandType.Text, param)                   '调用SqlHelper,执行sql语句  
    Return bln 
 
End Function 

    ''' <summary>
    ''' 删除用户信息
    ''' </summary>
    ''' <param name="enUserInfo">用户信息实体</param>
    ''' <returns>布尔值类型</returns>
    ''' <remarks></remarks>
    Public Function DelUser(ByVal enUserInfo As Entity.UserInfoEntity) As Boolean

        Dim strSql As String
        Dim DBHelper As New SqlHelper
        Dim param As SqlParameter()
        Dim bln As Boolean

        strSql = "Delete From T_User Where UserID=@ID"                               '删除用户sql语句
        param = New SqlParameter() {New SqlParameter("@ID", enUserInfo.UserID)}      '给参数ID传值
        bln = DBHelper.ExecuteUID(strSql, CommandType.Text, param)                   '调用SqlHelper,执行sql语句
        Return bln

    End Function

    B层:
[vb] 
Public Function DelUserManager(ByVal enUserInfo As Entity.UserInfoEntity) As Boolean 
 
    Dim DelUser As New DAL.UserDataDAL 
    Return DelUser.DelUser(enUserInfo) 
 
End Function 

    Public Function DelUserManager(ByVal enUserInfo As Entity.UserInfoEntity) As Boolean

        Dim DelUser As New DAL.UserDataDAL
        Return DelUser.DelUser(enUserInfo)

    End Function  

     U层:给实体层传值,执行B层方法
[vb] 
''' <summary>  
   ''' 自定义的删除Datagridview记录的方法  
   ''' </summary>  
   ''' <remarks></remarks>  
   Public Sub Del() 
 
       Dim k As Integer = Me.DataGridView1.SelectedRows.Count 
       Dim enUserInfo As New Entity.UserInfoEntity 
       Dim DelUserUI As New BLL.AddDelUserBLL 
 
       'Datagridview中是否存在数据  
       If DataGridView1.Rows.Count > 0 Then 
 
           '从下往上删,避免沙漏效应  
           For i As Integer = k To 1 Step -1 
 
               '获取第N行的第一列的数据  
               Dim usercode As String = DataGridView1.SelectedRows(i - 1).Cells("用户名").Value.ToString() 
 
               '判断是否该行用户是否为当前登录用户  
               If frmLogin.txtUserID.Text = usercode Then 
                   MsgBox("当前用户不能被删除,请重新选择!", vbOKOnly + vbExclamation, "系统提示") 
                   Exit Sub 
               Else 
                   '将usercode传值给enUserInfo实体的UserID属性(即根据用户名删除)  
                   enUserInfo.UserID = usercode 
               End If 
 
               '从数据库执行删除操作  
               If DelUserUI.DelUserManager(enUserInfo) Then 
                   MsgBox("删除成功!", vbOKOnly

补充:软件开发 , Vb ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,