Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Grid control - copy cell contents to clipboard without making read/write
 

Grid control - copy cell contents to clipboard without making read/write

Hi,

Simple question. I have a grid with a datasource. The users want to be able to copy the text from a given cell to the clipboard so they can paste it somewhere else.

If I make the grid read/write, then it works just fine. But the users can then edit the data. If I make the grid (or rows, cells, etc.) read only, then they can't select the text and copy it to the clipboard.

How can I allow them to select the contents of a cell and copy it to the clipboard?

FWIW, it appears that the cell contents are selected, but when I use CTRL-C, nothing happens.

My code for ^C in my menu is as follows:
Dim c As Control
If Me.HasChildren Then
   c = Me.ActiveMdiChild.ActiveControl
Else
   c = ActiveControl()
End If

If Not c Is Nothing AndAlso TypeOf c Is TextBoxBase Then<br/>  DirectCast(c, TextBoxBase).Copy()
End If
Again, this works fine if I make the grid read/write - which I don't want to do.

Oh, I did try some simple code to save the data on cell entry and restore it on cell leave, but that didn't seem to work reliably (but it may have been my approach.)

I am guessing there is a simple setting that allows this, but I can't find it anywhere.

Any help is appreciated.

Thanks,

FletcherJ

FletcherJ  Tuesday, August 25, 2009 9:28 PM
Hi FletcherJ,

It is possible to make a cell not editable but can select some text. Here is an example.
Public Class Form1
Private dt As DataTable = New DataTable()

Public Sub New()
InitializeComponent()
dt.Columns.Add("Column1")
dt.Rows.Add("Item1")
dt.Rows.Add("Item2")
dt.Rows.Add("Item3")
DataGridView1.DataSource = dt
End Sub

Private Sub DataGridView1_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
Dim textBox As TextBox = CType(e.Control, TextBox)
If textBox IsNot Nothing Then
textBox.ReadOnly = True
End If

End Sub
End Class

In the example, I have handled the EditingControlShowing event of DataGridView1 to set the textBox in the cell as ReadOnly. So you can enter the cell but cannot edit the text.

I think this can help you solve the problem.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
  • Marked As Answer byFletcherJ Thursday, August 27, 2009 4:27 PM
  •  
Kira Qian  Thursday, August 27, 2009 7:31 AM
Hi,
change the datagridview EditMode=EditProgrammatically.i hope it will help you
Best Regards, C.Gnanadurai ----------------------- Please mark the post as answer if it is helpfull to you
Gnanadurai  Wednesday, August 26, 2009 12:20 PM
Hi FletcherJ,

It is possible to make a cell not editable but can select some text. Here is an example.
Public Class Form1
Private dt As DataTable = New DataTable()

Public Sub New()
InitializeComponent()
dt.Columns.Add("Column1")
dt.Rows.Add("Item1")
dt.Rows.Add("Item2")
dt.Rows.Add("Item3")
DataGridView1.DataSource = dt
End Sub

Private Sub DataGridView1_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
Dim textBox As TextBox = CType(e.Control, TextBox)
If textBox IsNot Nothing Then
textBox.ReadOnly = True
End If

End Sub
End Class

In the example, I have handled the EditingControlShowing event of DataGridView1 to set the textBox in the cell as ReadOnly. So you can enter the cell but cannot edit the text.

I think this can help you solve the problem.

Sincerely,
Kira Qian
Please mark the replies as answers if they help and unmark if they don't.
  • Marked As Answer byFletcherJ Thursday, August 27, 2009 4:27 PM
  •  
Kira Qian  Thursday, August 27, 2009 7:31 AM
Gnanadurai,

I tried this, but it did not seem to have any effect. Could I be missing something?

Thanks,

FletcherJ
FletcherJ  Thursday, August 27, 2009 4:34 PM
Kira,

This worked perfectly, thanks!

Fletcher
FletcherJ  Thursday, August 27, 2009 4:34 PM

You can use google to search for other answers

Custom Search

More Threads

• an unhandled exception "system nullreferenceexception" occurred in "deven exe"
• Datatable column to array
• DataGridView Update when filled from Stored Proc
• Spreadsheet "Like" Data Entry
• Select the Entire Row when mouse right click is made in datagridview
• ENTER should behave like TAB in DataGridView
• How to display Complex Object in One DataGridView
• DataGridView in a modal dialog questions
• Datagridview row selection
• How to implement Complex DataBinding on Custom Controls?