0

I am trying to add a paste function to my Windows Forms DataGridView in C#. I already have some code that works just fine, but only as long as the DataGridView has "enough" columns and lines.

Let's say I have copied one Excel column with five lines and I want to paste it to a column in DataGridView with four lines, the last line gets cut off. My aim is that this line is supposed to be added, so that the pasted value can be added as well.

This is my code:

string s = Clipboard.GetText();
            string[] lines = s.Split('\n');
            int row = dataGridView1.CurrentCell.RowIndex;
            int col = dataGridView1.CurrentCell.ColumnIndex;
            foreach (string line in lines)
            {
                if (row < dataGridView1.RowCount && line.Length >0)
                {
                    string[] cells = line.Split('\t');
                    for (int i = 0; i < cells.GetLength(0); ++i)
                    {
                        if (col + i < this.dataGridView1.ColumnCount)
                        {
                            dataGridView1[col + i, row].Value = Convert.ChangeType(cells[i], dataGridView1[col + i, row].ValueType);
                        }
                        else
                        {
                            break;
                        }
                    }
                    row++;
                }
                else
                {
                    break;
                }
            }
  • 1
    Are you asking how to add a new [column](https://stackoverflow.com/questions/5524075/programmatically-add-new-column-to-datagridview) or [row](https://stackoverflow.com/questions/10063770/how-to-add-a-new-row-to-datagridview-programmatically) to datagridview? You need to make your question more specific. – default locale Apr 20 '18 at 06:40
  • I am asking how I can use this code to add a new column or row when the pasted cells' range does not fit into the datagridview's range –  Apr 20 '18 at 06:42
  • 2
    You can split this into subtasks: 1) detect that there are not enough rows; 2) add rows; 3) detect that there are not enough columns 4) add columns. Which one is problematic to you? You need to concentrate on one and ask a specific question, otherwise your question is too broad – default locale Apr 20 '18 at 06:45
  • My question refers to subtasks 1) and 3) –  Apr 20 '18 at 06:46
  • 2
    Good, you'll need to edit your question to make it clearer. Now, let's concentrate on 1: you're already using `dataGridView1.RowCount` and you're already keeping track of the current row number (`int row`). It should be trivial to compare those values, isn't it? Can you clarify what's the problem here? – default locale Apr 20 '18 at 06:49
  • 1
    Thanks, that took me on the right path for the solution –  Apr 20 '18 at 07:45

1 Answers1

0

This code solves my problem now:

 string s = Clipboard.GetText();
        string[] lines = s.Split('\n');
        int row = dataGridView1.CurrentCell.RowIndex;
        int col = dataGridView1.CurrentCell.ColumnIndex;
        while (lines.Length != (dataGridView1.RowCount -row))
        {
            dt.Rows.Add();
        }
        foreach (string line in lines)
        {
            if (row < dataGridView1.RowCount && line.Length >0)
            {

                string[] cells = line.Split('\t');
                while(cells.Length != (dataGridView1.ColumnCount - col))
                {
                    dt.Columns.Add();
                }
                for (int i = 0; i < cells.GetLength(0); ++i)
                {
                    if (col + i < this.dataGridView1.ColumnCount)
                    {
                        dataGridView1[col + i, row].Value = Convert.ChangeType(cells[i], dataGridView1[col + i, row].ValueType);
                    }
                    else
                    {
                        break;
                    }
                }
                row++;
            }
            else
            {
                break;
            }
        }