Hey Mike,
I am creating a derived UltraDataSource that has my own custom columns. I have been using the ctor to create the columns (the setup proc is called by the ctor):
_colName = this.Band.Columns.Add("CalendarName", typeof(string));
_colDateCreated = this.Band.Columns.Add("DateCreated", typeof(DateTime));
_colOwner = this.Band.Columns.Add("CalendarOwner", typeof(string));
}
But when I put this on a form and bind it to a grid, I get this designercode:
ultraDataColumn4.DataType = typeof(System.DateTime);
ultraDataColumn1,
ultraDataColumn2,
ultraDataColumn3,
ultraDataColumn4,
ultraDataColumn5});
Which throws a key already exists. So the ctor is not the place to define the columns.
Where should I do this? Is there another way to get around not having to comment out this designer code (which I am now going to confession for...).
Thanks!
Bob
Hi Bob,
This makes sense because when you place your derived UltraDataSource on the form at design-time, it will fire it's constructor and create the columns. The columns are also serialized to the form. Then when you run the application, a new component is created and the constructor is called, which creates the columns. Then the deserialization code will get called and try to create them again.
So you need to set this up so it only creates the columns the first time the control is created. For a control, I would normally recommend that you override OnCreateControl.
There doesn't seem to be a similar method of Components, though. So the only thing I can think of is that you do not create the component at design-time, but instead create it at run-time. Perhaps in the Form_Load event. That way you aboid the serialziation/ deserialization of the columns.
Thanks for the reply Mike.
I would do it that way, but I also want the grid to know all of the columns at design time. To work around, I have been:
1) Using the ctor to create the columns I want.
2) Place the component on the form, set the grids datasource.
3) Comment out the ctor code and continue.
But this limits me and makes changes hard to manage. Maybe this is a new feature request. It would be really cool to have a method that I could override, one that I could create the columns, assign UltraDataColumn member vars that I can use in my code for indexes and exposing properties outside the class...
Thanks for the reply. Things are working for me now.
That does sound like correct way to do it, but I am under the gun with a deadline looming, So, I'll just stick with what I have.
I really appreciate the extra attention that you have pit into this issue. As always You Rock!
Thanks,
The only other thing that I can think of is that you could derive from the UltraDataSource component and then derive from it's Designer. I think the designer has a method like InitializeNewComponent that will be fired when you first place the component on the form and you could add the columns there.