1

So, what I am trying to achieve is copy this from Excel and paste it into a NOT-blank DataGridView view. also I want to be readable for different characters. I am trying not to loose that row(First Image) after pasting. Thank you beforehand.

First Image: http://postimg.org/image/u656ou22v/

Error Image: http://postimg.org/image/63o1evn77/

This is my code in Add Button:

DataObject o = (DataObject)Clipboard.GetDataObject();
        if (o.GetDataPresent(DataFormats.Text))
        {
            if (dataGridView1.RowCount > 0)
                dataGridView1.Rows.Clear();

            if (dataGridView1.ColumnCount > 0)
                dataGridView1.Columns.Clear();

            bool columnsAdded = false;
            string[] pastedRows = Regex.Split(o.GetData(DataFormats.Text).ToString().TrimEnd("\r\n".ToCharArray()), "\r\n");
            int j = 0;
            foreach (string pastedRow in pastedRows)
            {
                string[] pastedRowCells = pastedRow.Split(new char[] { '\t' });

                if (!columnsAdded)
                {
                    for (int i = 0; i < pastedRowCells.Length; i++)
                        dataGridView1.Columns.Add("col" + i, pastedRowCells[i]);

                    columnsAdded = true;
                    continue;
                }

                dataGridView1.Rows.Add();
                int myRowIndex = dataGridView1.Rows.Count - 1;

                using (DataGridViewRow myDataGridViewRow = dataGridView1.Rows[j])
                {
                    for (int i = 0; i < pastedRowCells.Length; i++)
                        myDataGridViewRow.Cells[i].Value = pastedRowCells[i];
                }
                j++;
            }
        }
david1928
  • 23
  • 5

1 Answers1

1

You have to not remove the columns of your DataGridView if you want to keep the headers.

This simplifies your code this way :

DataObject o = (DataObject)Clipboard.GetDataObject();
if (o.GetDataPresent(DataFormats.Text))
{
    if (dataGridView1.RowCount > 0)
        dataGridView1.Rows.Clear();

    string[] pastedRows = Regex.Split(o.GetData(DataFormats.Text).ToString().TrimEnd("\r\n".ToCharArray()), "\r\n");
    int j = 0;
    foreach (string pastedRow in pastedRows)
    {
        string[] pastedRowCells = pastedRow.Split(new char[] { '\t' });

        dataGridView1.Rows.Add();
        int myRowIndex = dataGridView1.Rows.Count - 1;

        using (DataGridViewRow myDataGridViewRow = dataGridView1.Rows[j])
        {
            for (int i = 0; i < pastedRowCells.Length; i++)
                myDataGridViewRow.Cells[i].Value = pastedRowCells[i];
        }
        j++;
    }
}

I would even suggest you to not remove the rows as your button is called 'Add' and not 'Replace' :

DataObject o = (DataObject)Clipboard.GetDataObject();
if (o.GetDataPresent(DataFormats.Text))
{
    string[] pastedRows = Regex.Split(o.GetData(DataFormats.Text).ToString().TrimEnd("\r\n".ToCharArray()), "\r\n");
    int j = 0;
    foreach (string pastedRow in pastedRows)
    {
        string[] pastedRowCells = pastedRow.Split(new char[] { '\t' });

        dataGridView1.Rows.Add();
        int myRowIndex = dataGridView1.Rows.Count - 1;

        using (DataGridViewRow myDataGridViewRow = dataGridView1.Rows[j])
        {
            for (int i = 0; i < pastedRowCells.Length; i++)
                myDataGridViewRow.Cells[i].Value = pastedRowCells[i];
        }
        j++;
    }
}

My last suggestion is that you could add some pasted data checks, like if (pastedRows.length == dataGridView1.RowCount).

Bioukh
  • 1,888
  • 1
  • 16
  • 27
  • thank you it works great, but what about characters? – david1928 Feb 03 '16 at 09:34
  • I haven't enough information for that. Maybe the font doesn't support these characters. See this [one](http://stackoverflow.com/questions/420659/unicode-characters-not-showing-in-system-windows-forms-textbox). And try debug to see if the characters are correct when getting them from clipboard. – Bioukh Feb 03 '16 at 11:15