david1928
david1928

Reputation: 23

Pasting excel data into a blank DataGridView - UTF8

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++;
            }
        }

Upvotes: 1

Views: 1288

Answers (1)

Bioukh
Bioukh

Reputation: 1968

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).

Upvotes: 1

Related Questions