Click here to Skip to main content
16,022,309 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm developing a desktop application in c#. The copy seems to work well but on pasting clipboard values, it throws an error String must be exactly one character long at line:
string [] lines = clipboardvalues.Split(char.Parse(environment. NewLine));

How can I correct it to paste cells on datagridview.

What I have tried:

private DataGridViewCell GetStartingCell(DataGridView dgView)
{
    if (dgView.SelectedCells.Count == 0)
    {
        return null;
    }
    int rowIndex = dgView.Rows.Count - 1;
    int ColIndex = dgView.Columns.Count - 1;

    foreach (DataGridViewCell dgvcell in dgView.SelectedCells)
    {
        if (dgvcell.RowIndex < rowIndex)
        {
            rowIndex = dgvcell.RowIndex;
        }
        if (dgvcell.ColumnIndex < ColIndex)
        {
            ColIndex = dgvcell.ColumnIndex;
        }
    }
    return dgView[ColIndex, rowIndex];
}
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(char.Parse(Environment.NewLine));
    for (int i = 0; i < lines.Length; i++)
    {
        copyValues[i] = new Dictionary<int, string>();
        string[] linecontent = lines[i].Split((char)Keys.Tab);
        if (linecontent.Length == 0)
        {
            copyValues[i][0] = string.Empty;
        }
        else
        {
            for (int j = 0; j < linecontent.Length; j++)
            {
                copyValues[i][j] = linecontent[j];
            }
        }
    }
    return copyValues;
}
public void PasteClipboardValue ()
{
    if (STMDataGridView.Focus())
    {
        if (STMDataGridView.SelectedCells.Count == 0)
        {
            MessageBox.Show("Please, select a cell", "Paste", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            return;
        }
        DataGridViewCell StartingCell = GetStartingCell(STMDataGridView);
        Dictionary<int, Dictionary<int, string>> cbvalue = ClipboardValues(Clipboard.GetText());
        int irowindex = StartingCell.RowIndex;
        foreach (int rowkey in cbvalue.Keys)
        {
            int icolindex = StartingCell.ColumnIndex;
            foreach (int cellkey in cbvalue[rowkey].Keys)
            {
                if (icolindex <= STMDataGridView.Columns.Count - 1 && irowindex <= STMDataGridView.Rows.Count - 1)
                {
                    DataGridViewCell cell = STMDataGridView[icolindex, irowindex];
                    cell.Value = cbvalue[rowkey][cellkey].TrimStart();
                }
                icolindex += 1;
            }
            irowindex += 1;
        }
    }

}
Posted
Updated 5-Feb-19 6:32am
Comments
[no name] 31-Jan-19 4:36am    
I assume that the error message is telling you that your source string is empty. Your debugger will confirm this.
Robert S4r 31-Jan-19 8:11am    
But when I paste in any text editor, say MS office word, it pastes the data, meaning its copied in the clipboard.
[no name] 31-Jan-19 9:15am    
Well that has nothing to do with your problem. Use your debugger to find out what is happening.

Why to force doors wide open?

Check this: DataGridViewRow.Clone Method (System.Windows.Forms) | Microsoft Docs[^]
There you'll find CloneWithValues method, which takes DataGridViewRow as input parameter and returns cloned row with set of original values. You can adapt this method to your requirements.
 
Share this answer
 
I have investigated further. The problem is that Environment.NewLine returns the string "\r\n", so you cannot parse that to a single character. Try this:
C#
string[] lines = clipboardvalue.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
 
Share this answer
 
Comments
Maciej Los 5-Feb-19 12:26pm    
5ed!
[no name] 5-Feb-19 12:28pm    
Thanks Maciej (and for all the other upvotes). It's amazing what you can discover with a little bit of debugging.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900