I would like to copy a region of cells from a dataGridView and then paste that data into MS Excel. I am able to copy data and paste into MS Word or Notepad but not excel. There are lots of examples of copying from Excel and Pasting into a DataGridView but not the other way around.
private void frm_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.C)
{
this.dataGridView1.ClipboardCopyMode = DataGridViewClipboardCopyMode.EnableWithoutHeaderText;
if (this.dataGridView1.GetCellCount(DataGridViewElementStates.Selected) > 0)
{
try
{
DataObject d = dataGridView1.GetClipboardContent();
Clipboard.SetDataObject(d);
}
catch (System.Runtime.InteropServices.ExternalException)
{
MessageBox.Show("Could Not Copy To Clipboard");
}
}
}
}
Is there some way to paste into excel? I have struggled to find a solution.
Edit
It appears after trying some other programs with dataGridViews that you can by default copy and paste to excel or other programs after selecting a group of cells in dataGridView. I can't figure out right now if it was unsupported data in the DGV or properties of the DGV that I changed in the properties manager or I just needed to close and re-open excel because there was some error.