Skip to main content Skip to footer

How to Copy & Paste Multi-line text in C1FlexGrid

In order to enable copying text with new line character and pasting it as it is in another cell you would need to handle the clipboard functionality manually. The implementation is simple. Cancel the default copy feature of C1FlexGrid and implement a customized way of copying the text in C1FlexGrid's PreviewKeyDown event and handling Ctrl+C. Here is the code to copy the multiline text:

void fg_PreviewKeyDown(object sender, KeyEventArgs e)  
{  
  if (e.Key == Key.C && Keyboard.Modifiers == ModifierKeys.Control)  
            {  
                e.Handled = true;  
                CopyData.Clear();  
                for (int r = fg.Selection.TopRow; r <= fg.Selection.BottomRow; r++)  
                {  
                    CopyData.Add(new List<string>());  
                    for (int c = fg.Selection.LeftColumn; c <= fg.Selection.RightColumn; c++)  
                    {  
                        CopyData[r - fg.Selection.TopRow].Add(fg[r, c].ToString());  
                    }  
                }  
            }  
}  

In the code above CopyData is global List of String type to store the copied text. Once the data is stored in CopyData list now you need to transfer this to data to selected cell. Here again in PreviewKeyDown event of C1FlexGrid lets handle Ctrl+V:

void fg_PreviewKeyDown(object sender, KeyEventArgs e)  
{  
 if (e.Key == Key.V && Keyboard.Modifiers == ModifierKeys.Control)  
            {  
                e.Handled = true;  
                for (int r = 0; r < CopyData.Count && fg.Selection.Row + r < fg.Rows.Count; r++)  
                {  
                    for (int c = 0; c < CopyData[r].Count && fg.Selection.Column + c < fg.Columns.Count; c++)  
                    {  
                        fg[fg.Selection.Row + r, fg.Selection.Column + c] = CopyData[r][c];  
                    }  
                }  
            }  
}  

A .gif showcasing how to copy and paste multiple lines within Flexgrid at once

Hunter Haaf