Provision to copy data from MS-Excel to any grid component is quite a common requirement. Here, we discuss the same implementation using C1DataGrid and C1FlexGrid for WPF. Now, C1DataGrid is designed to be used, primarily, in bound mode; which implies that it simply reflects the data in its ItemsSource. Consequently, to implement any Paste operation, we really need to update the data in its ItemsSource. With C1Flexgrid, however, we simply need to call its Paste() method. Here are the steps to implement the aforementioned operation with C1DataGrid and C1Flexgrid.

  1. Read data from Clipboard when user has pressed CTRL+V, in C1DataGrid's 'KeyDown' event and C1Flexgrid's 'PreviewKeyDown' event.
  2. Convert this string, received from Clipboard, to an array.
  3. In case of C1Datagrid, read the array, fetch the data and update the ItemsSource.
  4. In case of C1Flexgrid, call its Paste() method.

Here's the complete code that implements the aforementioned steps:

C1DataGrid


private void grid_KeyDown(object sender, KeyEventArgs e)  
{  
 if (e.Key == Key.V && KeyboardUtil.Ctrl)  
 {  
  var clipboard = System.Windows.Clipboard.GetText();  
  var values = GetValues(clipboard);  
  int x = 0, y = 0;  

  //Paste data to C1DataGrid  
  if (sender is C1.WPF.DataGrid.C1DataGrid)  
  {  
   var _grid = sender as C1.WPF.DataGrid.C1DataGrid;  
   var table = ((_grid.ItemsSource) as DataView).Table as DataTable;  
   if (_grid.Selection.SelectedCells.Count > 0)  
   {  
    var firstSelectedCell = _grid.Selection.SelectedCells[0];  
    x = firstSelectedCell.Column.Index;  
    y = firstSelectedCell.Row.Index;  
   }  

   for (int i = 0; i < values.GetLength(0); i++)  
   {  
    for (int j = 0; j < values.GetLength(1); j++)  
    {  
     table.Rows[y + i][x + j] = values[i, j];  
    }  
   }  
  }  
}

C1FlexGrid

private void grid_KeyDown(object sender, KeyEventArgs e)  
{  
  if (e.Key == Key.V && KeyboardUtil.Ctrl)  
  {  
   var clipboard = System.Windows.Clipboard.GetText();  
   var values = GetValues(clipboard);  
   int x = 0, y = 0;  

   //Paste data to  C1FlexGrid  
   if (sender is C1.WPF.FlexGrid.C1FlexGrid)  
   {  
      var _grid = sender as C1.WPF.FlexGrid.C1FlexGrid;  
      _grid.Paste();  
   }  
   e.Handled = true;  
 }  
}

And, here's the function 'GetValues()' that reads data from Clipboard and returns the array of strings.


private static string[,] GetValues(string clipboard)  
{  
  var rows = clipboard.Split(new string[1] { "\\r\\n" }, StringSplitOptions.RemoveEmptyEntries);  
  var columns = rows[0].Split(new string[1] { "\\t" }, StringSplitOptions.RemoveEmptyEntries);  
  string[,] result = new string[rows.Length, columns.Length];  

  for (int i = 0; i < rows.Length; i++)  
  {  
     columns = rows[i].Split(new string[1] { "\\t" }, StringSplitOptions.RemoveEmptyEntries);  
     for (int j = 0; j < columns.Length; j++)  
     {  
       result[i, j] = columns[j];  
     }  
  }  
  return result;  
}

Here's the XAML:


<Grid>  
 <Grid.RowDefinitions>  
  <RowDefinition Height="60" />  
  <RowDefinition Height="*" />  
  <RowDefinition Height="60" />  
  <RowDefinition Height="*" />  
 </Grid.RowDefinitions>  
 <TextBlock Height="30" Text="C1DataGrid" Grid.Row="0" FontSize="14" FontWeight="Bold" Margin="10,0,0,0" VerticalAlignment="Bottom" />  
 <c1:C1DataGrid x:Name="C1DataGrid" KeyDown="grid_KeyDown" SelectionMode="MultiRange" Grid.Row="1" />  
 <TextBlock Height="30" Text="C1Flexgrid" Grid.Row="2" FontSize="14" FontWeight="Bold" Margin="10,0,0,5" VerticalAlignment="Bottom"/>  
 <c1:C1FlexGrid Name="c1FlexGrid1" PreviewKeyDown="grid_KeyDown" Grid.Row="3" />  
</Grid>  

Please refer to the attached samples for complete implementation. Copy a range of cells from an excel sheet, select a range of cells in any grid, and press CTRL+V. The data gets pasted. However, since I've set the same DataTable as ItemSource of both the grid controls, any changes that happen to data in one grid get reflected in the other. The grid controls would work independently just as well, provided different data sources. Here's how how both grids will look, when some data is pasted from Excel to C1Datagrid/C1FlexGrid: Download C# Sample Download Vb.Net Sample