Copy/Paste Cell Range in C1TrueDBGrid

ComponentOne's TrueDBGrid apart from being robust and persuasive, also provides an accessibility to its users to implement various functionalities which in turn widens the usage of the control. In this blog, we will be discussing one such functionality: Copy and Paste a Cell Range. RangeCP Implementing the Copy/Paste functionality for a range of cells in a grid is a bit more complex as compared to the normal Copy/Paste feature. To implement this functionality, it is required to be iterated through the selected range of cells one by one and store its values. To be more specific, navigate to each and every selected cell, copy its value, and finally save it for future reference i.e. for the paste functionality. To start with the implementation, the first thing required is a TrueDBGrid with data populated in it. Below are the steps that need to be implemented.

1. Select a Range of Cells

Select the CellRange (to be copied) using mouse.

2. Copy the Selected Cell Range

The copy operation can be implemented on a button click or on the click of a ContextMenu item. Here is the code snippet to copy the selected Cell Range.

private void menu_CopyClick(object sender, EventArgs e)  
        {  
            string strTemp = "";  
            if (this.c1TrueDBGrid1.SelectedRows.Count > 0)  
            {  
                selCol = c1TrueDBGrid1.SelectedCols.Count;  
                selRow = c1TrueDBGrid1.SelectedRows.Count;  
                foreach (int row in this.c1TrueDBGrid1.SelectedRows)  
                {  
                    //copy everything here  
                    foreach (C1.Win.C1TrueDBGrid.C1DataColumn col in this.c1TrueDBGrid1.SelectedCols)  
                    {  
                        strTemp = (strTemp + col.CellText(row)) + ControlChars.Tab;  
                    }  

                    strTemp = strTemp + Convert.ToString(ControlChars.Lf);  
                }  
                System.Windows.Forms.Clipboard.SetDataObject(strTemp, false);  
            }  
            else  
            {  
                MessageBox.Show("Please select a range of cells");  
            }  
        }  

The Selected range will then be copied from the Clipboard and stored in a Dictionary Collection. Here is the code snippet for the same.

private Dictionary<int, Dictionary<int, string>> ClipBoardValues(string clipboardValue)  
        {  
            Dictionary<int, Dictionary<int, string>> copyValues = new Dictionary<int, Dictionary<int, string>>();  
            String[] lines = clipboardValue.Split(ControlChars.Lf);  
            for (int i = 0; i <= lines.Length - 1; i++)  
            {  
                copyValues[i] = new Dictionary<int, string>();  
                String[] lineContent = lines[i].Split(ControlChars.Tab);  

                //if an empty cell value copied, then set the dictionay with an empty string  
                //else Set value to dictionary  
                if (lineContent.Length == 0)  
                {  
                    copyValues[i][0] = string.Empty;  
                }  
                else  
                {  
                    for (int j = 0; j <= lineContent.Length - 1; j++)  
                    {  
                        copyValues[i][j] = lineContent[j];  
                    }  
                }  
            }  
            return copyValues;  
        }  

3. Paste the Selected Range over any desired Cell Range

Once the data from the clipboard is successfully stored in the Dictionary Collection, it is time to Paste the data over another range of cells.

private void menu_PasteClick(object sender, EventArgs e)  
        {  
            Dictionary<int, Dictionary<int, string>> cbValue = ClipBoardValues(Clipboard.GetText());  

            if (this.c1TrueDBGrid1.SelectedRows.Count > 0)  
            {  
                if (selCol == c1TrueDBGrid1.SelectedCols.Count && selRow == c1TrueDBGrid1.SelectedRows.Count)  
                {  
                    int i1 = 0;  
                    foreach (int row in this.c1TrueDBGrid1.SelectedRows)  
                    {  
                        int j1 = 0;  
                        //copy everything here  
                        foreach (C1.Win.C1TrueDBGrid.C1DataColumn col in this.c1TrueDBGrid1.SelectedCols)  
                        {  
                            c1TrueDBGrid1[row, col.Caption] = cbValue[i1][j1];  
                            j1 += 1;  
                        }  
                        i1 += 1;  
                    }  
                }  
                else  
                {  
                    MessageBox.Show("Please select valid range of cells");  
                }  
            }  
            else  
            {  
                MessageBox.Show("Please select a range of cells");  
            }  
        }

Working Samples

Download Sample C# Download Sample VB

GrapeCity

GrapeCity Developer Tools
comments powered by Disqus