Handprint
Handprint

Reputation: 444

Copy by value a DataGridViewRow?

I have a command class that (hopefully) uses a DataGridViewRow as a member in order to execute and undo. When I attempt to assign the incoming dgvr to the member row in the constructor (e.g. _row = r;), it appears I'm getting a reference, and not a new copy, making the undo operation impossible (the original dgvr gets deleted by then and the member row becomes empty).

I've also tried to "copy" the row via msdn as below:

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

This, however, seems to be leaving the schema behind, as I can't access the fields by name (e.g. _row.Cells["critical"].Value).

How do I make a copy by value of a DataGridViewRow?

Upvotes: 0

Views: 821

Answers (1)

Davide Piras
Davide Piras

Reputation: 44605

I woul try a much simpler way, at the datatable level, call datatable.Rows.Add and pass your modified datarow.ItemArray.

This works if you still have a reference to your table which should be the grid datasource I think. I used this in my projects and usually worked fine.

Upvotes: 1

Related Questions