erictrigo
erictrigo

Reputation: 1017

Adding rows to XtraGrid using interfaces

I am using a BindingList for my grid's DataSouce, like this:

private BindingList<IPeriodicReportGroup> gridDataList = 
    new BindingList<IPeriodicReportGroup>();

...

gridControl.DataSource = gridDataList;

I set up the main view's OptionsBehaviour properties AllowDeleteRows and AllowAddRows to true, and the NewItemRowPosition to Bottom. When I click on the empty row to add data, I get an exception since there's no constructor in my interface -which makes sense-. Is there an easy way around this? I figure it'll probably have to do with handling the event InitNewRow, but I'm not quite sure on how to go from there.

Thanks.

Upvotes: 2

Views: 2395

Answers (1)

DmitryG
DmitryG

Reputation: 17848

You can accomplish your task at the BindingList level using the following approach (via the BindingList.AddingNew event):

    gridView1.OptionsBehavior.AllowAddRows = DevExpress.Utils.DefaultBoolean.True;
    gridView1.OptionsBehavior.AllowDeleteRows = DevExpress.Utils.DefaultBoolean.True;
    gridView1.OptionsView.NewItemRowPosition = NewItemRowPosition.Bottom;
    //...
    var bindingList = new BindingList<IPerson>(){
        new Person(){ Name="John", Age=23 },
        new Person(){ Name="Mary", Age=21 },
    };
    bindingList.AddingNew += bindingList_AddingNew; //  <<--
    bindingList.AllowNew = true;
    gridControl1.DataSource = bindingList;
}

void bindingList_AddingNew(object sender, AddingNewEventArgs e) {
    e.NewObject = new Person(); //   <<-- these lines did the trick
}
//...
public interface IPerson {
    string Name { get; set; }
    int Age { get; set; }
}
class Person : IPerson {
    public string Name { get; set; }
    public int Age { get; set; }
}

Upvotes: 3

Related Questions