Winform clone DataGridViewRow along with DataBoundItem

本文关键字:with DataBoundItem along DataGridViewRow clone Winform | 更新日期: 2023-09-27 18:01:26

我有一个pocos列表,像这样:

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

我可以将其绑定到dataGridView dgv,没有问题。现在我想克隆一行,以及该行中A的副本。

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.

我如何做到这一点,使克隆行具有克隆数据?

我更喜欢一个简单的工作,而不是进入NotifyPropertyChanged, BindingList和所有的,如果可能的话?

Winform clone DataGridViewRow along with DataBoundItem

为绑定的DataGridView添加新的"row"。

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