简体   繁体   中英

cannot copy paste from datagridview

I have a simple blank windows form as a test project and a datagridview and a query to fill the datagridview

in this test project. I can highlight all the columns and rows

click CTRL + C

open excel

then click ctrl + V

and the data is there.

but on a program that I inherited from someone in the company I have a similar data grid view. but I cannot copy paste

how can I identify what is the difference here? I check the data grid view properties for both test project and the program I inherited. both has the same following: 1. the read only = false 2. the copyclipboardmode = EnableWithAutoHeaderText

what else could prevent me from copy paste this value?

in the inherited program the only way to copy paste ist by double click the cell and copy, but. this limits me to copy one cell at a time , instead of multiple cell

please advise?

thank you

this is the code snippet from my test project

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
    DataGridView1.Rows.Add(New String() {"TEST", "TEST2", "TEST3"})
    DataGridView1.Rows.Add(New String() {"TEST", "TEST2", "TEST3"})
    DataGridView1.Rows.Add(New String() {"TEST", "TEST2", "TEST3"})
    DataGridView1.Rows.Add(New String() {"TEST", "TEST2", "TEST3"})
    DataGridView1.Rows.Add(New String() {"TEST", "TEST2", "TEST3"})
    DataGridView1.Rows.Add(New String() {"TEST", "TEST2", "TEST3"})
    DataGridView1.Rows.Add(New String() {"TEST", "TEST2", "TEST3"})
End Sub

This is the frmForm.Designer.vb as requested by QuickDanger

    <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class frmForm
    Inherits System.Windows.Forms.Form

    'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        Try
            If disposing AndAlso components IsNot Nothing Then
                components.Dispose()
            End If
        Finally
            MyBase.Dispose(disposing)
        End Try
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> _
    Private Sub InitializeComponent()
        Me.dgvSpecials = New System.Windows.Forms.DataGridView()
        Me.btnCreate = New System.Windows.Forms.Button()
        Me.PartCode = New System.Windows.Forms.DataGridViewTextBoxColumn()
        Me.PDF = New System.Windows.Forms.DataGridViewTextBoxColumn()
        Me.SLDDRW = New System.Windows.Forms.DataGridViewTextBoxColumn()
        Me.SLDPRT = New System.Windows.Forms.DataGridViewTextBoxColumn()
        Me.BasePartCode = New System.Windows.Forms.DataGridViewTextBoxColumn()
        Me.TEMPLATESLDDRW = New System.Windows.Forms.DataGridViewTextBoxColumn()
        Me.TEMPLATESLDPRT = New System.Windows.Forms.DataGridViewTextBoxColumn()
        Me.TEMPLATEDRWFILE = New System.Windows.Forms.DataGridViewTextBoxColumn()
        Me.TEMPLATEPRTFILE = New System.Windows.Forms.DataGridViewTextBoxColumn()
        Me.MakeSpecial = New System.Windows.Forms.DataGridViewCheckBoxColumn()
        CType(Me.dgvSpecials, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'dgvSpecials
        '
        Me.dgvSpecials.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize
        Me.dgvSpecials.Columns.AddRange(New System.Windows.Forms.DataGridViewColumn() {Me.PartCode, Me.PDF, Me.SLDDRW, Me.SLDPRT, Me.BasePartCode, Me.TEMPLATESLDDRW, Me.TEMPLATESLDPRT, Me.TEMPLATEDRWFILE, Me.TEMPLATEPRTFILE, Me.MakeSpecial})
        Me.dgvSpecials.Dock = System.Windows.Forms.DockStyle.Fill
        Me.dgvSpecials.Location = New System.Drawing.Point(0, 0)
        Me.dgvSpecials.Name = "dgvSpecials"
        Me.dgvSpecials.Size = New System.Drawing.Size(917, 246)
        Me.dgvSpecials.TabIndex = 0
        '
        'btnCreate
        '
        Me.btnCreate.Dock = System.Windows.Forms.DockStyle.Bottom
        Me.btnCreate.Location = New System.Drawing.Point(0, 246)
        Me.btnCreate.Name = "btnCreate"
        Me.btnCreate.Size = New System.Drawing.Size(917, 30)
        Me.btnCreate.TabIndex = 1
        Me.btnCreate.Text = "Create"
        Me.btnCreate.UseVisualStyleBackColor = True
        '
        'PartCode
        '
        Me.PartCode.HeaderText = "Part Code"
        Me.PartCode.Name = "PartCode"
        Me.PartCode.ReadOnly = True
        Me.PartCode.Width = 200
        '
        'PDF
        '
        Me.PDF.HeaderText = "PDF"
        Me.PDF.Name = "PDF"
        Me.PDF.ReadOnly = True
        '
        'SLDDRW
        '
        Me.SLDDRW.HeaderText = "SLDDRW"
        Me.SLDDRW.Name = "SLDDRW"
        Me.SLDDRW.ReadOnly = True
        '
        'SLDPRT
        '
        Me.SLDPRT.HeaderText = "SLDPRT"
        Me.SLDPRT.Name = "SLDPRT"
        Me.SLDPRT.ReadOnly = True
        '
        'BasePartCode
        '
        Me.BasePartCode.HeaderText = "Base Part"
        Me.BasePartCode.Name = "BasePartCode"
        Me.BasePartCode.ReadOnly = True
        '
        'TEMPLATESLDDRW
        '
        Me.TEMPLATESLDDRW.HeaderText = "DRW Template"
        Me.TEMPLATESLDDRW.Name = "TEMPLATESLDDRW"
        Me.TEMPLATESLDDRW.ReadOnly = True
        '
        'TEMPLATESLDPRT
        '
        Me.TEMPLATESLDPRT.HeaderText = "PRT Template"
        Me.TEMPLATESLDPRT.Name = "TEMPLATESLDPRT"
        '
        'TEMPLATEDRWFILE
        '
        Me.TEMPLATEDRWFILE.HeaderText = "Column1"
        Me.TEMPLATEDRWFILE.Name = "TEMPLATEDRWFILE"
        Me.TEMPLATEDRWFILE.ReadOnly = True
        Me.TEMPLATEDRWFILE.Visible = False
        '
        'TEMPLATEPRTFILE
        '
        Me.TEMPLATEPRTFILE.HeaderText = "Column1"
        Me.TEMPLATEPRTFILE.Name = "TEMPLATEPRTFILE"
        Me.TEMPLATEPRTFILE.ReadOnly = True
        Me.TEMPLATEPRTFILE.Visible = False
        '
        'MakeSpecial
        '
        Me.MakeSpecial.HeaderText = "MakeSpecial"
        Me.MakeSpecial.Name = "MakeSpecial"
        '
        'frmForm
        '
        Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
        Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
        Me.ClientSize = New System.Drawing.Size(917, 276)
        Me.Controls.Add(Me.dgvSpecials)
        Me.Controls.Add(Me.btnCreate)
        Me.Name = "frmForm"
        Me.Text = "frmForm"
        CType(Me.dgvSpecials, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub
    Friend WithEvents dgvSpecials As System.Windows.Forms.DataGridView
    Friend WithEvents btnCreate As System.Windows.Forms.Button
    Friend WithEvents PartCode As System.Windows.Forms.DataGridViewTextBoxColumn
    Friend WithEvents PDF As System.Windows.Forms.DataGridViewTextBoxColumn
    Friend WithEvents SLDDRW As System.Windows.Forms.DataGridViewTextBoxColumn
    Friend WithEvents SLDPRT As System.Windows.Forms.DataGridViewTextBoxColumn
    Friend WithEvents BasePartCode As System.Windows.Forms.DataGridViewTextBoxColumn
    Friend WithEvents TEMPLATESLDDRW As System.Windows.Forms.DataGridViewTextBoxColumn
    Friend WithEvents TEMPLATESLDPRT As System.Windows.Forms.DataGridViewTextBoxColumn
    Friend WithEvents TEMPLATEDRWFILE As System.Windows.Forms.DataGridViewTextBoxColumn
    Friend WithEvents TEMPLATEPRTFILE As System.Windows.Forms.DataGridViewTextBoxColumn
    Friend WithEvents MakeSpecial As System.Windows.Forms.DataGridViewCheckBoxColumn
End Class

Here is the actual frmForm.VB SldWorks is member of SolidWorks.Interop.sldworks

Imports SolidWorks.Interop.sldworks

Public Class frmForm
    Dim App As SldWorks
    Public Sub Setup(App As SldWorks, Arr As ArrayList)

    End Sub

    Private Sub btnCreate_Click(sender As Object, e As EventArgs) Handles btnCreate.Click

    End Sub
End Class

我猜你必须将DataGridView的MultiSelect属性设置为true

I guess you have set ClipboardCopyMode to Disable . Change this property to any other three available options.

It looks like others have had success with ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText (see this SO question ). If that doesn't help, could you paste the designer code for the dataGridView? (it is found in the project file [formname].Designer.vb)

You might also want to search the code for references to this control, and verify that the original developer did not modify the ClipboardCopyMode property at run-time.

Edit: Since none of the suggestions seem to be working, you could "roll your own" version of copy/paste using a tab-delimited format:

Note: This copies all data, not just selected cells, but you can modify it to only do selected cells.

Private Sub DataGridView1_KeyUp(sender As Object, e As KeyEventArgs) Handles DataGridView1.KeyUp
    If e.KeyCode = Keys.C AndAlso e.Control Then
        Dim sb = New StringBuilder()

        ' If you want the headers, keep these two lines
        Dim headers = DataGridView1.Columns.Cast(Of DataGridViewColumn)()
        sb.AppendLine(String.Join(vbTab, headers.[Select](Function(column) Convert.ToString(column.HeaderText)).ToArray()))

        For Each row As DataGridViewRow In DataGridView1.Rows
            Dim cells = row.Cells.Cast(Of DataGridViewCell)()
            sb.AppendLine(String.Join(vbTab, cells.[Select](Function(cell) Convert.ToString(cell.Value)).ToArray()))
        Next
        My.Computer.Clipboard.SetText(sb.ToString)
    End If
End Sub

Other developer use Ctrl+C for some shortcut key in his project. If you can check his design,Please go through property of Menubar->ShortcutKeys

Solved: this problem is due to solidWorks.Interop.sldworks and the application being in Solidworks environment

I've heard from other solidworks developers the ctrl C just refused to work

For copying data from the datagridview, we are getting multiple errors. So, I have developed following code for my application for this purpose using the help of QuickDanger from the above .

   Private Sub grdDetails_KeyUp(sender As Object, e As KeyEventArgs) Handles grdDetails.KeyUp
    Try
        If e.KeyCode = Keys.C AndAlso e.Control Then
            Dim mGrid As DataGridView, mCol As DataGridViewColumn, mRow As DataGridViewRow, mCell As DataGridViewCell
            Dim mStrBld As New StringBuilder
            mGrid = CType(sender, DataGridView)
            ' Copying Header 
            For Each mCol In mGrid.Columns
                If mCol.HeaderText IsNot Nothing Then
                    mStrBld.Append(vbTab & mCol.HeaderText)
                Else
                    mStrBld.Append(vbTab & "")
                End If
            Next
            mStrBld.Append(vbCrLf)
            '  Copying Data
            If mGrid.Rows.Count > 0 Then
                'Display progress in Progress Bar if rows are more
                'ProgressBar1.Minimum = 0 : ProgressBar1.Value = 0 : ProgressBar1.Maximum = mGrid.Rows.Count : ProgressBar1.Step = 1 : ProgressBar1.Visible = True
                For Each mRow In mGrid.Rows
                    For Each mCell In mRow.Cells
                        If mCell IsNot Nothing AndAlso mCell.Value IsNot Nothing Then
                            mStrBld.Append(vbTab & mCell.Value.ToString)
                        Else
                            mStrBld.Append(vbTab & "")
                        End If
                    Next
                    mStrBld.Append(vbCrLf)
                    'ProgressBar1.PerformStep()
                Next
                ' sending data to clipboard, so we can safely past the same
                My.Computer.Clipboard.SetText(mStrBld.ToString)
                'ProgressBar1.Visible = False
            Else
                MsgBox("No Data...1", MsgBoxStyle.Critical)
            End If
        End If
            e.Handled = True
    Catch ex As Exception
        MsgBox("Error:" & ex.Message)
    End Try
End Sub

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM