How to: Create, Edit and Copy Cell Comments
- 3 minutes to read
This example demonstrates how to create a new comment, edit its text and copy the existing comment to another cell.
To create a new comment and associate it with a cell, access the worksheet's collection of cell comments from the Worksheet.Comments property and call the CommentCollection.Add method. Pass the following parameters:
- a Cell object that specifies the cell to be commented;
- a string that specifies the author of the comment. In this example, the system username is used. Access it from the Workbook.CurrentAuthor property;
- a string that specifies the text of the comment.
To make the added comment visible in the cell, set the Comment.Visible property to true.
To edit the comment text, modify the CommentRunCollection collection, which is accessed from the Comment.Runs property. This collection stores the CommentRun objects that define separate regions of the comment text. After a comment is created, its text is defined by a single run that is contained in the CommentRunCollection collection. You can add more runs to the comments, edit the text of the existing runs or completely remove them from the comment.
Edit the text of the inserted comment by adding the author's name at the beginning of the comment text. To do this, use the CommentRunCollection.Insert method.
To copy the existing comment to another cell, call the CellRange.CopyFrom method of the Cell object corresponding to the cell into which you wish to insert the copied comment. Pass the source cell and the PasteSpecial.Comments enumerator value to insert only the comment and ignore cell content. The copied comment will replace any existing comment in the destination area.
Note that the copied comment preserves the structure of the source comment and contains the same number of comment runs. To edit the text of the copied comment, get access to the inserted comment by using the CommentCollection.GetComments method, and then modify the CommentRun.Text property of the second comment run.
A complete sample project is available at https://github.com/DevExpress-Examples/winforms-spreadsheetcontrol-api-e4655
// Get the system username. string author = workbook.CurrentAuthor; // Add a comment to the "A2" cell. Cell commentedCell = worksheet.Cells["A2"]; Comment commentA2 = worksheet.Comments.Add(commentedCell, author, "This is a comment"); commentA2.Visible = true; // Insert the author's name at the beginning of the comment. CommentRunCollection commentRunsA2 = commentA2.Runs; commentRunsA2.Insert(0, author + ": \r\n"); // Copy the comment to the "E2" cell. worksheet.Cells["E2"].CopyFrom(commentedCell, PasteSpecial.Comments); // Get the added comment and make it visible. Comment commentE2 = worksheet.Comments.GetComments(worksheet["E2"]); commentE2.Visible = true; // Modify text of the copied comment. CommentRunCollection commentRunsE2 = commentE2.Runs; commentRunsE2.Text = "This comment is copied from the cell " + commentedCell.GetReferenceA1();
The image below shows the result.