Implementing "Copy/Paste to Excel" functionality in RadGridView for WinForms
|
Article relates to
|
RadGridView Copy/Paste to Excel
|
|
Created by
|
Martin Vassilev
|
|
Last modified
|
6/24/2008
|
|
Last modified by
|
Martin Vasilev
|
HOW-TO
Implement "Copy/Paste to Excel" functionality in RadGridView for WinForms
SOLUTION
RadGridView does not internally support copy/paste selection to Excel (using Ctrl+C and Ctrl+V), but this is easily achievable by using the
KeyDown event and the standard
Clipboard.SetDataObject method. The code block below shows the implementation:
| private void radGridView1_KeyDown(object sender, KeyEventArgs e) |
| { |
| if (e.KeyCode == Keys.C && e.Control) |
| { |
| //you could change the code below according to your custom logic |
| string copyStr = ConvertSelectedDataToString(this.radGridView1); |
| Clipboard.SetDataObject(copyStr); |
| } |
| } |
| |
| /// <summary> |
| /// Prepare string in format suitable to paste in Excel sheet |
| /// </summary> |
| /// <param name="grid">Source selection RadGridView </param> |
| /// <returns>Formated string for clipboard</returns> |
| private string ConvertSelectedDataToString(RadGridView grid) |
| { |
| StringBuilder strBuild = new StringBuilder(); |
| |
| for (int row = 0; row < grid.SelectedRows.Count; row++) |
| { |
| for (int cell = 0; cell < grid.SelectedRows[row].Cells.Count; cell++) |
| { |
| strBuild.Append(grid.SelectedRows[row].Cells[cell].Value.ToString()); |
| strBuild.Append("\t"); |
| } |
| |
| strBuild.Append("\n"); |
| } |
| |
| return strBuild.ToString(); |
| } |
Feel free to modify this code according to your scenario
Comments
If you'd like to comment on this KB
article, please, send us a
Support Ticket.
Thank you!
Please
Sign In
to rate this article.