加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

机房收费系统之DataGridView

发布时间:2020-12-17 07:56:21 所属栏目:百科 来源:网络整理
导读:在我们平时的学习中不少见用到将数据库与界面连接的一个控件——DataGridView,在我们敲第一遍机房的时候我们用到的相似的控件是——MSHFlexGrid,随着学习的深入,发现我们用到的平台越来越人性化了,现在用的VS2013的控件—— DataGridView可以直接和数据

在我们平时的学习中不少见用到将数据库与界面连接的一个控件——DataGridView,在我们敲第一遍机房的时候我们用到的相似的控件是——MSHFlexGrid,随着学习的深入,发现我们用到的平台越来越人性化了,现在用的VS2013的控件——DataGridView可以直接和数据库相连接,今天重点说一下DataGridView删除行并同时更新数据库功能的实现:

这是删除前的效果,我们要实现的是如图的效果,左图为界面,右图为数据库中的数据,但是还需要考虑要删除的用户是否正在登录,如果正在登录,则不能删除。

删除后的效果:

实现这个功能主要是在U层加了一个方法,B层、D层和其他删除时是大相径庭的,下面看一下代码实现部分:

1、D层:

  Public Function DelUser(enUser As UserInfoEntity) As Integer Implements IUserInfo.DelUser
        Dim cmdText As String
        Dim sqlParams As SqlParameter()
        Dim sqlHelper As New SqlHelper
        Dim intResult As Integer

        cmdText = "Delete from T_UserInfo where UserID=@UserID"
        sqlParams = {New SqlParameter("@UserID",enUser.UserID)}
        intResult = sqlHelper.ExecuteAddDelUpdate(cmdText,CommandType.Text,sqlParams)

        Return intResult
    End Function
2、B层:

 Public Function DelUser(ByVal enUser As UserInfoEntity) As Integer
        Dim iUserInfo As IUserInfo
        Dim intResult As Integer

        iUserInfo = factory.CreateSelectUser()
        intResult = iUserInfo.DelUser(enUser)

        Return intResult

    End Function
3、U层:

a.定义一个删除行的过程:

 ''' <summary>
    ''' 删除用户的自定义过程
    ''' </summary>
    ''' <remarks></remarks>
    Public Sub DelUser()
        Dim intRows As Integer = DataGridView1.SelectedRows.Count '判断选中的行数
        Dim intDelRow As Integer
        Dim enUser As New UserInfoEntity
        Dim bllAddDelUser As New AddDelUserBLL
        Dim strUserID As String '获取选中数据的第一列值
        Dim intResult As Integer

        If intRows > 0 Then
            '从下往上删除,防止漏行
            For intDelRow = intRows To 1 Step -1
                strUserID = DataGridView1.SelectedRows(intDelRow - 1).Cells("UserIDDataGridViewTextBoxColumn").Value.ToString()
                '如果该用户正在工作,则不能删除
                If pubshare.strUserName = strUserID.Trim() Then
                    MsgBox("该用户正在工作,不能删除",vbOKOnly + vbExclamation,"提示")
                    Exit Sub
                Else
                    '将要删除的用户ID传给实体
                    enUser.UserID = strUserID
                    '同时将该实体的信息从数据库中删除
                    intResult = bllAddDelUser.DelUser(enUser)
                    If intResult > 0 Then
                        '将数据库中所删除的对应的信息在dataGridview表中也删除
                        DataGridView1.Rows.RemoveAt(DataGridView1.SelectedRows(intDelRow - 1).Index)
                        MsgBox("删除成功","提示")
                    Else
                        MsgBox("删除失败","提示")
                    End If
                End If

            Next
        Else
            DataGridView1.Rows.Clear()
        End If
    End Sub
b.通过点击删除按钮来实现这个过程:

 Private Sub btnDel_Click(sender As Object,e As EventArgs) Handles btnDel.Click
        If DataGridView1.SelectedRows.Count > 0 Then
            If MessageBox.Show("确定要删除所选信息吗?","提示",MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                DelUser()
            End If
        Else
            MessageBox.Show("请选择要删除的用户!",MessageBoxButtons.OK,MessageBoxIcon.Exclamation)
            Return
        End If
       
    End Sub
仅仅是个人的一点想法,希望和大家交流!

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读