前言:
我们在添加删除用户这个功能时,有一个选中行删除,下面就让我来告诉你们怎么在DataGridView来实现这个功能吧
内容:
1、绑定数据源
参考这篇博客:http://blog.csdn.net/tgbyn/article/details/74858291
2、代码的展示
(1)窗体加载中添加可选择用户级别
Private Sub frmAdDeleteAddUser_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: 这行代码将数据加载到表“LoginDataSet4.Y_User_Info”中。您可以根据需要移动或删除它。
Me.Y_User_InfoTableAdapter.Fill(Me.LoginDataSet4.Y_User_Info)
With cmbSelect
.Items.Add("管理员")
.Items.Add("操作员")
.Items.Add("一般用户")
End With
'隐藏工具栏
FillByToolStrip.Hide()
'清空DataGridView表中数据
While (DataGridView1.RowCount > 0)
DataGridView1.Rows.Remove(DataGridView1.Rows(0))
End While
End Sub
(2)用户级别的选择-控件改变事件,同时根据选择显示相应的信息
Private Sub cmbSelect_SelectedIndexChanged(sender As Object, e As EventArgs) Handles cmbSelect.SelectedIndexChanged
LevelToolStripTextBox.Text = cmbSelect.SelectedItem.ToString() '将自己添加的Combo中的内容赋给ToolStripTextBox控件
Try
Me.Y_User_InfoTableAdapter.FillBy(Me.LoginDataSet4.Y_User_Info, LevelToolStripTextBox.Text)
Catch ex As System.Exception
System.Windows.Forms.MessageBox.Show(ex.Message)
End Try
End Sub
(3)删除选中行
Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
'判断是否选中
If Me.DataGridView1.SelectedRows.Count > 0 Then
If MessageBox.Show("删除后不可恢复,确定要删除选中的用户吗?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
Delete()
End If
Else
MessageBox.Show("请选择要删除的用户!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning)
Return
End If
End Sub
Public Sub Delete()
Dim k As Integer = Me.DataGridView1.SelectedRows.Count
Dim user As New Entity.Entity
Dim deleteUser As New Facade.DeleteUserFacade ‘外观层
If DataGridView1.Rows.Count > 0 Then
'从下往上删,避免沙漏效应
For i As Integer = k To 1 Step -1
'获取选中行的列名,并将用户名UserID赋值给userDele
Dim userDele As String = DataGridView1.SelectedRows(i - 1).Cells("UserID").Value.ToString
'如果选中行用户为正在登录的用户,则不能删除
If Form1.txtUserID.Text =userDele Then
MsgBox("当前用户不能被删除,请重新选择!", vbOKOnly + vbExclamation, "系统提示")
Exit Sub
Else
user.userID = userDele
End If
If deleteUser.deleuserFacade(user) Then
MsgBox("删除成功", vbOKOnly + vbInformation, "系统提示")
Else
MessageBox.Show("删除失败!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Error)
Return
End If
'将从数据库中删除的信息从Datagridview1中删除
Me.DataGridView1.Rows.RemoveAt(DataGridView1.SelectedRows(i - 1).Index)
Next
Else
DataGridView1.Rows.Clear()
End If
End Sub
(4)获取选中行的用户名
'获取选中行的列名,并将用户名UserID赋值给userDele
Dim userDele As String = DataGridView1.SelectedRows(i - 1).Cells("UserID").Value.ToString
(5)外观层代码 ,后面几层自己写就好
Imports BLL
Imports Entity
Public Class DeleteUserFacade
Public Function deleuserFacade(user As Entity.Entity)
Dim deleuserBll As New BLL.DeleteUserBLL
Dim dt As Integer
dt = deleuserBll.deleteUser(user)
Return dt
End Function
End Class
(6)D层代码--数据库删除选中行数据
Imports System.Data.SqlClient
Imports System.Data
Imports Entity
Imports IDAL
Imports SQLHelper
Public Class DeleteUserDAL : Implements IDAL.IDeleteAddUser
Public Function Ideleteadduser(ByVal user As Entity.Entity) Implements IDAL.IDeleteAddUser.Ideleteadduser
Dim sqlhelper As New SQLHelper.sqlhelper
Dim Dt As Integer
Dim sqlDelete As String
Dim paras As SqlParameter() = {New SqlParameter("@UserID", user.userID)}
sqlDelete = "delete from Y_User_Info where UserID = @UserID "
Dt = sqlhelper.ExecAddDelUpdate(sqlDelete, CommandType.Text, paras)
Return Dt
End Function
End Class
3、总结:
这次显示查询到的数据是直接用的DataGridView连接数据源查询的,只有删除选中行是走的七层,感觉挺方便的,没有费多少事。在DataGridView中的添加查询里面,那个查询语句写成删除语句,也是可以执行的。很多东西需要我们去探索。