Question

sometimes a new record in my DB is almost the same with the previous , i want to make a button "Create From Last" that makes a new record from last (if that is not the first rec)

I am trying something like : object rec = BindingSource1.Current; BindingSource1.Add(cur);

but i am getting this error:

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

Additional information: Cannot add external objects to this list.

Was it helpful?

Solution

Finally i found an answer !

i am using .AddingNew event of my BindingSource1

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;
                }
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top