Skip to content

Instantly share code, notes, and snippets.

@dj1711572002
Last active March 25, 2021 22:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dj1711572002/9c18c37a19eb670052f1e65d99c34294 to your computer and use it in GitHub Desktop.
Save dj1711572002/9c18c37a19eb670052f1e65d99c34294 to your computer and use it in GitHub Desktop.
VB.NET DataGridView Row Editing Mouse RightButtonClick COntextMenuSprit -Insert,Delete,Copy,Paste-
Private Sub Form1_Activated(sender As Object, e As EventArgs) Handles Me.Activated
'----------DGV contetmenustrip add--------------------
cMenu1 = New ContextMenuStrip()
cMenu1.Items.Add("Insert", Nothing, New System.EventHandler(AddressOf Rinsert))
cMenu1.Items.Add("Delete", Nothing, New System.EventHandler(AddressOf Rdelete))
cMenu1.Items.Add("Copy", Nothing, New System.EventHandler(AddressOf Rcopy))
cMenu1.Items.Add("Paste", Nothing, New System.EventHandler(AddressOf Rpaste))
'------ETC---
End Sub
'=====================================================
'Class 全体の変数宣言に配列宣言を追加しておいてください
Public  copyCol()
Public copyRow()
'======================================================
'=============RowInsert===============================
Private Sub Rinsert()
Debug.Print("Rinsert")
dgv1.Rows.Insert(dgv1.CurrentCell.RowIndex)
For i = 0 To dgv1.Rows.Count - 1
dgv1.Rows(i).HeaderCell.Value = i.ToString()
Next i
End Sub
'=============Row Delete=================================
Private Sub Rdelete()
Debug.Print("Rdelete")
dgv1.Rows.RemoveAt(dgv1.CurrentCell.RowIndex)
For i = 0 To dgv1.Rows.Count - 1
dgv1.Rows(i).HeaderCell.Value = i.ToString()
Next i
End Sub
'==========Row Copy===================================
Private Sub Rcopy()
Debug.Print("Rcopy")
Dim cX As Integer = dgv1.CurrentCell.ColumnIndex
Dim cY As Integer = dgv1.RowCount
Dim rX As Integer = dgv1.ColumnCount
Dim rY As Integer = dgv1.CurrentCell.RowIndex
ReDim copyCol(cY)
ReDim copyRow(rX)
For i = 0 To rX - 1
copyRow(i) = CStr(dgv1.Rows(rY).Cells(i).Value)
Next i
For i = 0 To dgv1.Rows.Count - 1
dgv1.Rows(i).HeaderCell.Value = i.ToString()
Next i
End Sub
'==============Row Paste===================
Private Sub Rpaste()
Debug.Print("Rpaste")
'-------------列ペースト-----------------------------
'If RadioButton1.Checked = True Then
Dim cX As Integer = dgv1.CurrentCell.ColumnIndex
Dim cY As Integer = dgv1.RowCount
Dim rX As Integer = dgv1.ColumnCount
Dim rY As Integer = dgv1.CurrentCell.RowIndex
For i = 0 To rX - 1
dgv1.Rows(rY).Cells(i).Value = copyRow(i)
Next i
For i = 0 To dgv1.Rows.Count - 1
dgv1.Rows(i).HeaderCell.Value = i.ToString()
Next i
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment