I am trying to copy and paste row within the datagridview. It works fine but the new row over which I paste the copied row stays selected and new row after that is NOT created. If I add sendkeys.send(keys.control+keys.enter) then new row is created but the first cell in the copied row goes in edit mode ! Below is the code :
Private Sub gridRange_KeyUp(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles gridRange.KeyUp If (e.KeyCode = Keys.V AndAlso Keys.Control) Then Dim o As IDataObject = Clipboard.GetDataObject() Try Dim dt As DataTable = ParseCSV(o.GetData(DataFormats.CommaSeparatedValue)) If (gridRange.SelectedRows.Count > 0) Then Dim strRangeName As String = dt.Rows(1).Item(0).ToString.Trim Dim strRangeProperty As String = dt.Rows(1).Item(1).ToString.Trim
gridRange.Item(0, gridRange.CurrentRow.Index).Value = strRangeName
gridRange.Item(1, gridRange.CurrentRow.Index).Value = strRangeProperty
gridRange.CommitEdit(DataGridViewDataErrorContexts.Commit)
gridRange.EndEdit()
End If Catch ex As Exception End Try End If
datagridview problems
ManInTheBox
Thanks for the reply, I saw this example in MSDN but it not reallly helpful since it just pastes the contents in a textbox, while I am trying to copy and paste within the datagridview... I figured set the current cell to be the first cell in the new row which makes the pasted row save. So to make it work I had to take following steps:
1) Found a ParseCSV function which parses the copied row and puts it in a datatable format.
2) Add the datatable contents to the datagrid,
3) if .IsNewRow then .CommitEdit(DataGridViewDataErrorContexts.Commit) , .EndEdit() , .Update() ,
4) datasource.AcceptChanges() ,
5) grid.CurrentCell = grid.Item(0, grid.Rows.GetLastRow(DataGridViewElementStates.None))
To bad there weren't any inbuilt methods to handle this.
Hope this helps someone.
Robin E Davies
p_shah,
The following complete code example demonstrates how cells are copied to the Clipboard. This example includes a button that copies the selected cells to the Clipboard using the System.Windows.Forms.DataGridView.GetClipboardContent method and displays the Clipboard contents in a text box.
Imports System
Imports System.Windows.Forms
Public Class Form1
Inherits Form
Private WithEvents DataGridView1 As New DataGridView()
Private WithEvents PasteButton As New Button()
Private TextBox1 As New TextBox()
<STAThreadAttribute()> _
Public Shared Sub Main()
Application.Run(New Form1())
End Sub
Public Sub New()
Me.DataGridView1.AllowUserToAddRows = False
Me.DataGridView1.Dock = DockStyle.Fill
Me.Controls.Add(Me.DataGridView1)
Me.PasteButton.Text = "paste selected cells"
Me.PasteButton.Dock = DockStyle.Top
Me.Controls.Add(Me.PasteButton)
Me.TextBox1.Multiline = True
Me.TextBox1.Height = 100
Me.TextBox1.Dock = DockStyle.Bottom
Me.Controls.Add(Me.TextBox1)
Me.Text = "DataGridView Clipboard demo"
End Sub
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Load
' Initialize the DataGridView control.
Me.DataGridView1.ColumnCount = 5
Me.DataGridView1.Rows.Add(New String() {"A", "B", "C", "D", "E"})
Me.DataGridView1.Rows.Add(New String() {"F", "G", "H", "I", "J"})
Me.DataGridView1.Rows.Add(New String() {"K", "L", "M", "N", "O"})
Me.DataGridView1.Rows.Add(New String() {"P", "Q", "R", "S", "T"})
Me.DataGridView1.Rows.Add(New String() {"U", "V", "W", "X", "Y"})
Me.DataGridView1.AutoResizeColumns()
Me.DataGridView1.ClipboardCopyMode = _
DataGridViewClipboardCopyMode.EnableWithoutHeaderText
End Sub
Private Sub PasteButton_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles PasteButton.Click
If Me.DataGridView1.GetCellCount( _
DataGridViewElementStates.Selected) > 0 Then
Try
' Add the selection to the clipboard.
Clipboard.SetDataObject( _
Me.DataGridView1.GetClipboardContent())
' Replace the text box contents with the clipboard text.
Me.TextBox1.Text = Clipboard.GetText()
Catch ex As System.Runtime.InteropServices.ExternalException
Me.TextBox1.Text = _
"The Clipboard could not be accessed. Please try again."
End Try
End If
End Sub
End Class
Please read the following link on How to: Enable Users to Copy Multiple Cells to the Clipboard from the Windows Forms DataGridView Control for the further information:
http://msdn2.microsoft.com/en-us/library/dec5efh1.aspx