Nevron Forum

NDataGridView Copy & Paste

https://www.nevron.com/Forum/Topic2967.aspx

By Fabian Baptista 1 - Thursday, January 28, 2010

I'm using this control but I can't find the way to support copy & paste rows.
Any suggestions?
Thanks in advance!
By Angel Chorbadzhiev - Friday, January 29, 2010

Hi Fabian,

The NDataGridView inherits from .NET DataGridView control and just adds ability to apply palette or skin.

The control doesn't have build in copy/paste row functionality.

Regards,

Angel.

By Fabian Baptista 1 - Friday, January 29, 2010

Thanks.
Here is the code c# to Copy, Paste and Delete data in DataGridView / NDataGridView.

//On event "Key_Down" of the NDataGridView:
private void DGV_KeyDown(object sender, KeyEventArgs e)
{
if (e.Control && e.KeyCode == Keys.C)
{//Copy
Copy();
e.Handled = true;
}
else if (e.Control && e.KeyCode == Keys.V)
{//Paste
Paste();
e.Handled = true;
}
else if (e.KeyCode == Keys.Delete)
{//Delete
DeleteSelectedCells();
e.Handled = true;
}
}

public void Copy()
{
DataObject d = DGV.GetClipboardContent();
Clipboard.SetDataObject(d);
}

internal void DeleteSelectedCells()
{
if (DGV.SelectedRows.Count > 0)
DeleteSelectedRows();
else
{
foreach (DataGridViewCell C in DGV.SelectedCells)
{
C.Value = "";
}
}
}
}

public void Paste()
{
try
{
string s = Clipboard.GetText();
string[] lines = s.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
int row = DGV.CurrentCell.RowIndex;
int col = DGV.CurrentCell.ColumnIndex;

//Add Rows (Optional)
try
{
if (lines.Length + row >= DGV.RowCount)
{
for (int i = DGV.RowCount; i <= lines.Length + row; i++)
{
AddNewRowProgramaticaly(i);
}

}
}
catch { }//end Optional Code

//Paste lines
foreach (string line in lines)
{
if (row < DGV.RowCount && line.Length > 0)
{
string[] cells = line.Split('\t');
for (int i = 0; i < cells.GetLength(0); ++i)
{
if (col + i < this.DGV.ColumnCount)
{
DGV[col + i, row].Value = Convert.ChangeType(cells[i], DGV[col + i, row].ValueType);
}
else
{
break;
}
}
row++;
}
else
{
break;
}
}
}
catch { }
}