2
votes

I am developing a winform application and I used Datagridview to show records.

Now I have a requirement that the cell contents should be readonly but at same time I want that end users can copy the cell content to a clipboard and not to edit it.

Please suggest if you have any solution.

4

4 Answers

4
votes
DataGridView.ReadOnly = True;   
DataGridView.SelectionMode = DataGridViewSelectionMode.CellSelect;

Then user can select data(string) from cell and CTRL+V put it to clipboard

2
votes

You could also add a ContextMenu to the DataGrid, and add a Copy and Paste item, add the Ctrl+V and Ctrl+C shortcuts to it

Then you handle their events. If you want, you can set the menu to Visible = false and the menu wont show when you right click, but the shortcuts will still work.

Personally I like the touch the right click menu adds to the DataGrid, but thats your call.

-1
votes

You can try with this code - based on DataGridViewCell.OnKeyPress

Link : http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.onkeypress.aspx

private void DataGridViewCell_KeyPress(KeyPressEventArgs e, int index)
{
     if ( (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl) 
          && e.Key == Key.C)
     {
        //You can get your cell based on index
        Clipboard.SetText(.....);
     }
}
-1
votes

you can add to the XAML: ClipboardCopyMode= "IncludeHeader"/"ExcludeHeader"/"NONE"