整体思路:
具体操作:
1、控件:ContextMenuStrip
2、代码:
'右击事件 Private Sub DgvLineManage_CellMouseDown(sender As Object,e As DataGridViewCellMouseEventArgs) Handles DgvLineManage.CellMouseDown If e.Button = Windows.Forms.MouseButtons.Right Then If e.RowIndex >= 0 Then '若行已是选中状态,就不再进行设置。 If DgvLineManage.Rows(e.RowIndex).Selected = False Then DgvLineManage.ClearSelection() DgvLineManage.Rows(e.RowIndex).Selected = True End If '只选中一行时,设置活动单元格 If DgvLineManage.SelectedRows.Count = 1 Then DgvLineManage.CurrentCell = DgvLineManage.Rows(e.RowIndex).Cells(e.RowIndex) End If '获取鼠标右击位置后,弹出操作菜单 ContextMenuStrip1.Show(MousePosition.X,MousePosition.Y) End If End If
end sub
Private Sub 删除ToolStripMenuItem_Click(sender As Object,e As EventArgs) Handles 删除ToolStripMenuItem.Click For Each r As DataGridViewRow In DgvLineManage.SelectedRows If Not r.IsNewRow Then '添加下机操作的代码 '**************我的下机代码********** If (MsgBox("确定要强制此用户下机吗?",MsgBoxStyle.OkCancel + MsgBoxStyle.Information,"提示") = MsgBoxResult.Ok) Then '选中下机 Dim CardId As String Dim RowNum As Integer '要删除的行 '获取选中行的卡号 CardId = Trim(DgvLineManage.SelectedRows(RowNum).Cells(0).Value.ToString()) Call OffLine(CardId) MessageBox.Show("强制下机成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information) End If '**************我的下机代码********** '将选中的行移除。 DgvLineManage.Rows.Remove(r) End If Next End Sub
这是选中一行下机的操作,选中多行下机的操作,添加一个循环就可以了,读者可以自行探索一下。