vb.net 无法从 datagridview 复制粘贴
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22869793/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
cannot copy paste from datagridview
提问by BobNoobGuy
I have a simple blank windows form as a test project and a datagridview and a query to fill the datagridview
我有一个简单的空白窗口窗体作为测试项目和一个 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
采纳答案by BobNoobGuy
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 只是拒绝工作
回答by Saulius ?im?ikas
I am guessing that you have to set MultiSelectproperty of the DataGridView to true
我猜你必须MultiSelect将 DataGridView 的属性设置为true
回答by Sarvesh Mishra
I guess you have set ClipboardCopyModeto Disable. Change this property to any other three available options.
我猜你已经设置ClipboardCopyMode为Disable. 将此属性更改为任何其他三个可用选项。
回答by QuickDanger
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)
看起来其他人已经取得了成功ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText(请参阅此 SO 问题)。如果这没有帮助,您能否粘贴 dataGridView 的设计器代码?(在项目文件 [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 ClipboardCopyModeproperty 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
回答by Divyek
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
其他开发人员使用 Ctrl+C 作为其项目中的某些快捷键。如果您可以检查他的设计,请通过 Menubar->ShortcutKeys 的属性
回答by Rao Ch
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 .
为了从 datagridview 复制数据,我们收到多个错误。因此,我为此目的在 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

