EditingControlDataGridView property is null - c#

With relation to my previous question mentioned here:
Custom DataGridViewColumn value disappears after loosing focus
I tried to change my EditControl class. I added an override to the 'OnTextChanged' method. When I run and debug my application, I came to know that the 'EditingControlDataGridView' property for the custom control is not being set. Even though in the designer code I am adding the column into the MetroGrid.
What can the issue be?

Related

Binding error in design and runtime on usercontrol

In great need help with the strange binding problem with regards to Winform UserControls.
Its like this.
Have a form with a UserControl
UserControl has a System.Windows.Forms.BindingSource control
BindingSource has a DataSource set to an entity POCO type at design time.
UserControl has controls that are bound to properties on the POCO class. An example: CreateDate
No problem in designer of the UserControl itself.
But entering the designer og the Form containing the UserControl, I get a designer error window with the message: "Cannot bind to the property or column CreateDate on the DataSource. Parameter name: dataMember"
In runtime I get the same error, but first when Show is called on the form. Here I can see on the stack trace that it is thrown after a CheckBinding call.
The error occurs no matter if the DataSource on the bindingSource is set or not during the creation (in constructor) or Load event on the form.
I can see that the designer sets my data source like this in the designer file: this.bindingSourceRecipe.DataSource = typeof(Data.Entities.Recipe);
I have tried so many things to solve this problem. It seems very strange as this is a dead simple form/control setup and should be a trivial use of Winforms usercontrols.
If I set the bindingSourceRecipe.DataSource at runtime to a temp. instance just after the InitializeComponent() in the form then no error occure;
InitializeComponent();
ucRecipeBaseControl.Recipe = new Recipe() { Id = 0, CreateDate = DateTime.Now, Name = "" };
So it seems that the Initialization cycle of the form/control somehow clears the binding source knowledge about its DataSource type.
This does of course not happen if a place all the user controls and BindingSource Directly on the form. Then everythings works, and it does not matter if the DataSource ever gets set to an instance of a Data entity.
Seems like a complete mystery to me, should be dead simple, and I'm kind of lost on what to do.
Any help or suggestions are highly appreciated.
BR Peter Meldgaard
Ahhh, got it nailed at last. It was caused by a mix of Winform designer behavior and my code. I was setting the binding controls datasource in a public property setter, so I could save a copy of the entity, to be able to compare the changed and the org. The thing is that when you put a control with a public property on a form, then the designer initializes this property to null inside the designer generated code. I did not know that. So everytime the usercontrol was instantiated, the DataSource got set to null, clearing the typeof(dataentity), albeit loosing the binding information.
Adding a null check in the setter, and only update datasource when value is not null fixed the issue.

Which events are triggered by editing a DataGridViewComboBoxCell?

I'm trying to make a manually-editable DataGridView, not bound to any DataSource, where the DataGridView itself and most of the cell types are subclassed (custom cell types are provided as templates to columns in the grid subclass) so that certain events can be handled differently for different cell types.
One of the custom cell types is based on a ComboBoxCell, where the Items are manually added from an enum. At the moment, this is the only part of the application which isn't working properly, so I assume I got most of the subclassing syntax okay (I'm new to C#).
Whenever I run the application and try to change the dropdown list to anything other than the blank default, a message box appears which includes the line:
System.ArgumentException: DataGridViewComboBoxCell value is not valid.
As I understand, this happens because the cell's Value is set as a string instead of an object. I've tried fixing this by setting the Value to the string's value in the OnEndEdit event, which caused the error window to only appear once and the dropdown to be selected properly only after the window was dismissed.
I've also tried overriding the event that throws DataError and managed to cause the error window not to appear, but this way is considered a hack and won't be accepted as a solution - I need to stop whatever error is being thrown from appearing entirely.
Any suggestions as to what other events I should override, or how else to stop this error?
In the DataGridViewComboBoxColumn class, there is a ValueType property that allows you to define the type of the data you add in the Items collection.
You could use it this way :
myComboBoxColumn.ValueType = typeof(myEnum);

Control's default properties

When I add a control to a form through the designer, not all properties of the control appear in the designer code. For example, when I add a ListBox the UseWaitCursor property does not appear in the designer code unless it is set to True. When I change it to False it disappears from the designer code, which makes me think that the properties somehow have defaults and don't appear in the designer code if left at default.
Can someone please help me understand how the designer works and where all this is tracked. The reason I ask is I am currently writing a class that extends a third party ActiveX control which I plan to initialize dynamically at run time. I was going through the designer code (when the third party control is added through the designer) and a lot of its properties do not appear there.
This is done with the [DefaultValue] attribute. The Control.UseWaitCursor property looks similar to this:
[DefaultValue(false)]
public bool UseWaitCursor
{
// etc..
}
So if you leave the value at False in the Properties window then the designer knows that it should not display the value in Bold and that it is not necessary to put the property assignment in the InitializeComponent() method since the default is already good. An ActiveX control will certainly have a lot of properties set at its default value as well.

How to save control property from designer auto-filling? [duplicate]

This must be a FAQ, but I can’t find a duplicate question!
There are lot of different attributes that control what the WinForm Designer does with properties on a custom control, I am never clear on the one I should use in this case.
I am looking for:
Designer does not show property in grid
Designer does not read value of property
Designer does not set property to default value
E.g. Designer behaves as if the property was not there.
Designer does not complain if it has already done one of the above before the attributes were added (hard!)
Background.
The code that is giving me the problem is:
this.eventListControl.FilterSets =
((SystList<FilterSet>)(resources.GetObject("eventListControl.FilterSets")));
The FilterSets property should never have been touched by the winforms designer; it is now not Serializable and MsDev falls over every time a form that used the eventListControl is changed!
I think you can use [Browsable (false)] and [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
The second attribute prevents the property from appearing in InitializeComponent

Why DataGridView.AutoGenerateColumns has a Browsable(false) attribute applied?

I am just curious why the Windows DataGridView control is hiding its AutoGenerateColumns property from the designer?
Really can't come up with some nice answer to this question.
Anyone?
It looks like it's hidden because it's controlled by a helper class. According to the accepted answer to this question, the Initialize() method of the DataGridViewDesigner (an undocumented and pretty well-hidden helper class) sets the DGV's AutoGenerateColumns property based on whether there is a DataSource specified, which overrides anything you might specify in your own code.
The logic would be that a DataSource is required in order to auto-generate columns; so, it would be invalid to let you try to set this property to True without having specified one. Conversely, if you're setting the DataSource in the designer, you probably want the DGV to set up the columns for you unless you've also set up your own using the Columns property.
You can expose this property by overriding it in a derived class and specifying a [Browseable(true)] attribute. However, as the question above shows, anything you set it to in a constructor or InitializeComponent() method will be overwritten by the DGVDesigner anyway.

Categories