Profile Picture

NDataGridView Copy & Paste

Posted By Fabian Baptista 14 Years Ago
Author
Message
Fabian Baptista
Posted 14 Years Ago
View Quick Profile
Junior Member

Junior Member (18 reputation)Junior Member (18 reputation)Junior Member (18 reputation)Junior Member (18 reputation)Junior Member (18 reputation)Junior Member (18 reputation)Junior Member (18 reputation)Junior Member (18 reputation)Junior Member (18 reputation)

Group: Forum Members
Last Active: 14 Years Ago
Posts: 18, Visits: 1
I'm using this control but I can't find the way to support copy & paste rows.
Any suggestions?
Thanks in advance!

Angel Chorbadzhiev
Posted 14 Years Ago
View Quick Profile
Supreme Being

Supreme Being (142 reputation)Supreme Being (142 reputation)Supreme Being (142 reputation)Supreme Being (142 reputation)Supreme Being (142 reputation)Supreme Being (142 reputation)Supreme Being (142 reputation)Supreme Being (142 reputation)Supreme Being (142 reputation)

Group: Forum Members
Last Active: Last Month
Posts: 139, Visits: 184

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.



Fabian Baptista
Posted 14 Years Ago
View Quick Profile
Junior Member

Junior Member (18 reputation)Junior Member (18 reputation)Junior Member (18 reputation)Junior Member (18 reputation)Junior Member (18 reputation)Junior Member (18 reputation)Junior Member (18 reputation)Junior Member (18 reputation)Junior Member (18 reputation)

Group: Forum Members
Last Active: 14 Years Ago
Posts: 18, Visits: 1
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 { }
}






Similar Topics


Reading This Topic