I have an excel sheet with the following:
So, what I am trying to achi
Have your gridview columns defined, if not you have to define the columns first.
private void PasteClipboard(DataGridView myDataGridView)
{
DataObject o = (DataObject)Clipboard.GetDataObject();
if (o.GetDataPresent(DataFormats.Text))
{
string[] pastedRows = Regex.Split(o.GetData(DataFormats.Text).ToString().TrimEnd("\r\n".ToCharArray()), "\r\n");
foreach (string pastedRow in pastedRows)
{
string[] pastedRowCells = pastedRow.Split(new char[] { '\t' });
using (DataGridViewRow myDataGridViewRow = new DataGridViewRow())
{
myDataGridViewRow = (DataGridViewRow) myDataGridView.RowTemplate.Clone();
for (int i = 0; i < pastedRowCells.Length; i++)
myDataGridViewRow.Cells[i].Value = pastedRowCells[i];
myDataGridView.Rows.Add(myDataGridViewRow);
}
}
}
}
IF not definced columns
private void PasteClipboard(DataGridView myDataGridView)
{
//Create COlumns in datagridView
myDataGridView = new DataGridView();
myDataGridView.Columns.Add("col1", "Col1");
myDataGridView.Columns.Add("col2", "Col2");
myDataGridView.Columns.Add("col3", "Col3");
myDataGridView.Columns.Add("col4", "Col4");
DataObject o = (DataObject)Clipboard.GetDataObject();
if (o.GetDataPresent(DataFormats.Text))
{
string[] pastedRows = Regex.Split(o.GetData(DataFormats.Text).ToString().TrimEnd("\r\n".ToCharArray()), "\r\n");
foreach (string pastedRow in pastedRows)
{
string[] pastedRowCells = pastedRow.Split(new char[] { '\t' });
using (DataGridViewRow myDataGridViewRow = new DataGridViewRow())
{
myDataGridViewRow = (DataGridViewRow) myDataGridView.RowTemplate.Clone();
for (int i = 0; i < pastedRowCells.Length; i++)
myDataGridViewRow.Cells[i].Value = pastedRowCells[i];
myDataGridView.Rows.Add(myDataGridViewRow);
}
}
}
}
In case you are dealing with Unicode here is the code to paste to a DataTable that is binded to a the DataGridView
DataObject o = (DataObject)Clipboard.GetDataObject();
if (o.GetDataPresent(DataFormats.Text))
{
string[] pastedRows = Regex.Split(o.GetText().TrimEnd("\r\n".ToCharArray()), "\r\n");
foreach (string pastedRow in pastedRows)
{
string[] pastedRowCells = pastedRow.Split(new char[] { '\t' });
var temp = dt1.NewRow();
for (int i = 0; i < pastedRowCells.Length; i++)
temp[i] = pastedRowCells[i];
dt1.Rows.Add(temp);
}
}