|
Article relates to
|
RadGridView for WinForms, Q3 2008 SP2
|
|
Created by
|
Nikolay Diyanov, Telerik
|
|
Last modified
|
Jan 30, 2009
|
|
Last modified by
|
Nikolay Diyanov, Telerik
|
HOW-TO
Implement copy/paste functionality for rows in one RadGridView instance or between different RadGridView instances (
CSV format).
SOLUTION
The most important methods in this approach are the
CopyRows and
PasteRows methods.
Here is how you should deal with the
CopyRows method (a VB.NET version is available in the attached projects):
| private void CopyRows(RadGridView radGridView) |
| { |
| StringBuilder sb = new StringBuilder(); |
| foreach (GridViewRowInfo row in radGridView.SelectedRows) |
| { |
| int i = 0; |
| while(i<row.Cells.Count) |
| { |
| if(i>0) |
| { |
| sb.Append(","); |
| } |
| sb.Append(row.Cells[i].Value.ToString()); |
| i++; |
| } |
| sb.Append(";"); |
| } |
| clipBoard = sb.ToString(); |
| } |
This method will execute for the RadGridView instance passed as parameter. It will take the data from the SelectedRows. The cells' values that are on one and the same row will be separated by comma "," while the rows will be separated by semi-colon ";". The whole string will be built with a StringBuilder.
The code snippet for the
PasteRows is:
| private void PasteRows(RadGridView radGridView) |
| { |
| if(clipBoard != string.Empty) |
| { |
| radGridView.GridElement.BeginUpdate(); |
| string[] lines = clipBoard.Split(';'); |
| int i = 0; |
| try |
| { |
| while (i < lines.Length - 1) |
| { |
| string[] cellVal = lines[i].Split(','); |
| DataRow row = ((DataTable)radGridView.DataSource).NewRow(); |
| row[0] = cellVal[0]; |
| row[1] = cellVal[1]; |
| row[2] = cellVal[2]; |
| Console.WriteLine("PasteRows" + radGridView.Name); |
| ((DataTable)radGridView.DataSource).Rows.Add(row); |
| i++; |
| |
| } |
| } |
| catch (Exception ex) |
| { |
| MessageBox.Show("Incompatible DataSources"); |
| } |
| radGridView.GridElement.EndUpdate(); |
| } |
| } |
The string built in the CopyRows method is split first into strings for the rows with the help of the semi-colon character. Then these strings are split once again for the values in the rows' cells. The ready values are put into a new DataRow for the DataTable bound to the
radGridView parameter. If there is a difference in the structure of the DataTables bound to the RadGridView instances (for example the source RadGridView has three columns and the destination DataTable has two column) a MessageBox is shown to indicate this.
This is the essence of the approach. Now let's examine the UI copy/paste approaches.
The first one is the context menu approach. You simply add Copy rows/Paste rows menu items to the default context menu:
| void radGridView_ContextMenuOpening(object sender, Telerik.WinControls.UI.ContextMenuOpeningEventArgs e) |
| { |
| if (e.ContextMenuProvider is GridDataCellElement || e.ContextMenuProvider is GridTableBodyElement) |
| { |
| if (!(((GridDataCellElement)e.ContextMenuProvider).RowInfo is GridViewNewRowInfo)) |
| { |
| RadMenuItem itemCopy = new RadMenuItem(); |
| itemCopy.Text = "Copy Row(s)"; |
| itemCopy.Click += new EventHandler(itemCopy_Click); |
| e.ContextMenu.Items.Insert(0, itemCopy); |
| |
| if (clipBoard != String.Empty) |
| { |
| RadMenuItem itemPaste = new RadMenuItem(); |
| itemPaste.Text = "Paste Row(s)"; |
| itemPaste.Click += new EventHandler(itemPaste_Click); |
| e.ContextMenu.Items.Insert(0, itemPaste); |
| } |
| } |
| } |
| contextMenuInvoker = ((GridDataCellElement)e.ContextMenuProvider).GridControl; |
| } |
In the Click event handler of the Copy/Paste menu items we execute the Copy/Paste methods:
| void itemCopy_Click(object sender, EventArgs e) |
| { |
| CopyRows(contextMenuInvoker); |
| } |
| |
| void itemPaste_Click(object sender, EventArgs e) |
| { |
| PasteRows(contextMenuInvoker); |
| } |
The second approach concerns keyboard shortcuts. We use the KeyDown event handler to implement our keyboard logic. If we press Ctrl+A, we select all the rows in the RadGridView, if we press Ctrl+C, we copy the selected rows, and if we press Ctrl+V, we paste them:
| void radGridView_KeyDown(object sender, KeyEventArgs e) |
| { |
| RadGridView radGridView = sender as RadGridView; |
| Console.WriteLine("KeyDown" + radGridView.Name); |
| if (e.KeyCode == Keys.C && e.Control) |
| { |
| CopyRows(radGridView); |
| } |
| else if (e.KeyCode == Keys.V && e.Control) |
| { |
| PasteRows(radGridView); |
| } |
| else if (e.KeyCode == Keys.A && e.Control) |
| { |
| radGridView.GridElement.BeginUpdate(); |
| for (int i = 0; i < radGridView.Rows.Count; i++) |
| { |
| radGridView.Rows[i].IsSelected = true; |
| } |
| radGridView.GridElement.EndUpdate(); |
| } |
| } |
The described approach is demonstrated in the attached C# and VB.NET solutions.
Please
Sign In
to rate this article.