Reputation: 51
I would like to copy Datagridview selected rows to clipboard and paste them in notepad or Microsoft Word. What is the best method to achieve this?
Thank you..
Upvotes: 0
Views: 5656
Reputation: 1501
This will copy the currently selected cells in the DataGridView called myDataGridView
data to the clipboard.
Clipboard.SetDataObject(myDataGridView.GetClipboardContent())
Upvotes: 1
Reputation:
I use a Copy menu item. If you want to use Ctrl+C, then you'll have to implement keyboard events. Here is my code:
private void copyToolStripMenuItem_Click(object sender, EventArgs e)
{
foreach (Control myControl in tabControl1.SelectedTab.Controls)
{
if (myControl is DataGridView))
{
DataGridView tempdgv = (DataGridView)myControl;
DataObject dataObj = tempdgv.GetClipboardContent();
try
{
Clipboard.SetDataObject(dataObj, true);
}
catch (Exception ex)
{
// Do Something
}
finally
{
if (selectAllToolStripMenuItem.Checked)
{
selectAllToolStripMenuItem_Click(this, EventArgs.Empty);
}
}
}
}
}
Upvotes: 4
Reputation: 73253
Upvotes: 1