NDataGridView Copy & Paste


Author
Message
Fabian Baptista
Fabian Baptista
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
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
Angel Chorbadzhiev
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
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
Fabian Baptista
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
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 { }
}



GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic
1 active, 1 guest, 0 members, 0 anonymous
No members currently viewing this topic!

Login

Explore
Messages
Mentions
Search