Reputation: 16246
I have a list of pocos like this:
List<A> alist;
public class A {
public string M { get; set; }
public string N { get; set; }
public Bs List<B> { get; set; }
}
public class B {
public string X { get; set; }
public string Y { get; set; }
}
I can bind this to a dataGridView dgv
no problem.
Now I want to clone a row, along with a copy of A
in that row.
var row = dgv.SelectedCells[0].OwningRow;
var alist= (List<A>)((BindingSource)dgv.DataSource).DataSource;
var a = (A)row.DataBoundItem;
var clone = (DataGridViewRow)row.Clone(); //only copied a blank row, no content
dgv.Rows.Add(clone);
var copya = (A)clone.DataBoundItem;
as.Add(copya); // This 'copya' is NULL, because dataBoudItem is not copied.
How do I make that happen so that clone row has clone data?
I prefer a simpe work around, not going into NotifyPropertyChanged, BindingList and all that if possible?
Upvotes: 0
Views: 139
Reputation: 5743
Add the new "row" in the data instead for binded DataGridView.
var currentCell = dgv.CurrentCell;
var bs = (BindingSource)dgv.DataSource;
var data = (List<A>)bs.DataSource;
var currentSelection = (A)row.DataBoundItem;
data.Add(currentSelection);
bs.ResetBindings(false); // This should refresh the grid
dgv.CurrentCell = currentCell; // Put back the current cell
Upvotes: 1