简体   繁体   English

无法从datagridview复制粘贴

[英]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 我有一个简单的空白Windows窗体作为测试项目和一个datagridview以及一个查询以填充datagridview

in this test project. 在这个测试项目中。 I can highlight all the columns and rows 我可以突出显示所有列和行

click CTRL + C 单击CTRL + C

open excel 打开Excel

then click ctrl + V 然后点击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 两者具有相同的以下内容:1.只读= false 2. 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 这是QuickDanger要求的frmForm.Designer.vb

    <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 这是实际的frmForm.VB SldWorks是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 . 我猜您已经将ClipboardCopyMode设置为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 ). 看来其他人已经成功使用ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText (请参见此SO问题 )。 If that doesn't help, could you paste the designer code for the dataGridView? 如果那没有帮助,您可以粘贴dataGridView的设计器代码吗? (it is found in the project file [formname].Designer.vb) (可以在项目文件[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. 您可能还希望在代码中搜索对此控件的引用,并验证原始开发人员没有在运行时修改ClipboardCopyMode属性。

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. 其他开发人员在项目中使用Ctrl + C作为某些快捷键。 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 解决:此问题是由于solidWorks.Interop.sldworks和应用程序在Solidworks环境中引起的

I've heard from other solidworks developers the ctrl C just refused to work 我从其他SolidWorks开发人员那里听说ctrl C只是拒绝工作

For copying data from the datagridview, we are getting multiple errors. 为了从datagridview复制数据,我们遇到了多个错误。 So, I have developed following code for my application for this purpose using the help of QuickDanger from the above . 因此,为此,我在上面的QuickDanger的帮助下为此应用开发了以下代码。

   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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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