2013-10-31 135 views
0

有时我的数据库中的新记录与之前几乎相同,我想创建一个按钮“从上次创建”,从上次创建新记录(如果这不是第一个记录)来自当前记录的新记录

我想是这样的: object rec = BindingSource1.Current; BindingSource1.Add(cur);

,但我得到这个错误:

A first chance exception of type 'System.ArgumentException' occurred in System.Data.dll

Additional information: Cannot add external objects to this list.

+0

你可能想看看:http://stackoverflow.com/questions/78536/deep-cloning-objects-in-c- sharp – NoChance

+0

我需要一些帮助 – Theodoros

+0

请提供更多信息。关于你的情况 - Thx。 – NoChance

回答

0

终于让我找到一个答案!

我用我的BindingSource1的.AddingNew事件

private void BindingSource1_AddingNew(object sender, AddingNewEventArgs e) 
     { 
    if(need_insert_from_Current) 
       { 
       BindingSource bs =(BindingSource)sender; 
       DataRowView cur = (DataRowView)bs.Current; 
       DataView dv=(DataView)bs.List; 
       DataRowView drv=dv.AddNew(); 
       // Collect data from current rec (except from the 1st value (Id is Identity !) 
       for (int i = 1; i <= dv.Table.Columns.Count-1; i++) 
       { 
        drv.Row[i] = cur.Row[i]; 
       } 
       bs.Position = bs.Count - 1; 
       e.NewObject = drv; 
       need_insert = true; 
       need_insert_from_Current=false; 
       } 
     } 
相关问题