0

am using EF Code first, and have a windows form with a bindingNavigator bound to a datasource. The data entry textboxes are also bound to the datasource

I want it to work so that
When the user opens the form if there are no records in the data source then the form behaves as if the user had clicked the add button.

I have tried

private void PersonForm_Load(object sender, EventArgs e)
    {
        if (bindingSource1.Count == 0)
        {
            bindingNavigator1.BindingSource.AddNew();
        }
    }
private void bindingSource1_AddingNew(object sender, System.ComponentModel.AddingNewEventArgs e)
    {
        e.NewObject = CreatePerson();
    }

 private Person CreatePerson()
    {
        var obj = new Person();
        obj.RowId = Guid.NewGuid();
        return obj;
    }

however the record selector stays disabled and when I click save in the navigator bar the new record does not save.

Kirsten
  • 15,730
  • 41
  • 179
  • 318

1 Answers1

0

I found if I added the entity before setting the data source, the navigator behaved how I wanted

DbSet<Person> dset = Db.People;
bindingList = dset.Local.ToBindingList();
if (bindingList.Count == 0)
{
   var obj = CreatePerson();
   dset.Local.Add(obj);
}
 binding.DataSource = bindingList;
Kirsten
  • 15,730
  • 41
  • 179
  • 318