Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Addind twice a cloned row to the same DataGridView throws an error

$
0
0

Hi All,

I'm trying to Clone a row in DataGridView with the following code:

public DataGridViewRow CloneWithValues(DataGridViewRow row)
        {
            DataGridViewRow clonedRow = (DataGridViewRow)row.Clone();
            for (Int32 index = 0; index < row.Cells.Count; index++)
            {
                clonedRow.Cells[index].Value = row.Cells[index].Value;
            }
            return clonedRow;
        }

Afterwards for example I want to copy this row 2 times with the following command piece of code:

dataGridView1.Rows.Insert(dataGridView1.CurrentRow.Index, _cloneRows);

When executing this line of code the 2nd time I get the following error:

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Windows.Forms.dll
Additional information: Row provided already belongs to a DataGridView control.

What is wrong here that it is not possible to add several rows with the same content?
Thanks.


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>