This blog explains an approach in C1TrueDbGrid to Copy and Paste entire Row when selecting a row using RecordSelector. Once the entire row is being selected using RecordSelector, then on pressing Ctrl+C, its default behavior is such that it only copies the cell in focus and not the entire row. This blog further implements our own key press functionality to handle the Copy/ Paste of the entire row rather than just the cell in focus.

The process used for this approach is :

  1. Bind C1TrueDbGrid with a DataSource.

  2. Set C1TrueDbGrid's AllowAddNew property to true.

  3. Then handle the C1TrueDbGrid's KeyDown event to copy the text in clipboard when row is being selected using RecordSelector and Ctrl+C is pressed and lately to get the text from clipboard and assign it to respective cells in selected row on pressing Ctrl+V.


For that we are binding C1TrueDbGrid with a DataTable

 // Creating DataTable
DataTable dataTable = new DataTable();

// Add columns to the data table
dataTable.Columns.Add("FirstName", typeof(string));
dataTable.Columns.Add("LastName", typeof(string));
dataTable.Columns.Add("Country", typeof(string));

// Add rows to the data table
dataTable.Rows.Add("Benjamin", "Jane", "United States");
dataTable.Rows.Add("Grace", "Greg", "Japan");
dataTable.Rows.Add("Seether", "Franklin", "Africa");
dataTable.Rows.Add("Alex", "Daniels", "United Kingdom");
dataTable.Rows.Add("Sean", "Chancey", "Australia");

// Setting the datasource to C1TrueDbGrid
c1TrueDBGrid1.SetDataBinding(dataTable, "");
c1TrueDBGrid1.AllowAddNew = true;


We need to subscribe KeyDown event of C1TrueDbGrid to handle the specific functionalities wherein it manually handles how to Copy/ Paste the entire row when user selects the row using RecordSelector.


void c1TrueDBGrid1_KeyDown(object sender, KeyEventArgs e)
{
string strTemp = null;
// Tto copy the entire row
if (e.KeyCode == Keys.C && e.Control)
{
if (this.c1TrueDBGrid1.SelectedRows.Count > 0)
{
foreach (int rowloop in this.c1TrueDBGrid1.SelectedRows)
{
foreach (C1DataColumn col1 in this.c1TrueDBGrid1.Columns)
{
strTemp = strTemp + col1.CellText(rowloop) + "\t";
}
strTemp = strTemp + "\n";
}
// Sets the text in clipboard
System.Windows.Forms.Clipboard.SetDataObject(strTemp, true);
}
}
// To paste entire row
else if (e.KeyCode == Keys.V && e.Control)
{
char[] rowSplitter = { '\r', '\n' };
char[] columnSplitter = { '\t' };

// If it is the newly added then to initialize the data while pasting data into new row
if (c1TrueDBGrid1.AddNewMode == AddNewModeEnum.AddNewCurrent)
c1TrueDBGrid1.EditActive = true;

// Gets the text from clipboard and sets it in each cell accordingly
IDataObject dataInClipboard = Clipboard.GetDataObject();
string stringInClipboard = (string)dataInClipboard.GetData(DataFormats.Text);
string[] rowsInClipboard = stringInClipboard.Split(rowSplitter, StringSplitOptions.RemoveEmptyEntries);

for (int iRow = 0; iRow < rowsInClipboard.Length; iRow++)
{
string[] valuesInRow = rowsInClipboard[iRow].Split(columnSplitter);
for (int iCol = 0; iCol < valuesInRow.Length - 1; iCol++)
{
c1TrueDBGrid1[c1TrueDBGrid1.Row, iCol] = valuesInRow[iCol];
}
}
}
e.SuppressKeyPress = true; // To prevent the default key press handling here
}


Download the sample for detailed implementation of the above code.

DownloadSample