Windows Develop Bookmark and Share   
 index > Windows Forms General > Usercontrol inherited from DataGridView
 

Usercontrol inherited from DataGridView

Hi All,

currently i am designing a usercontrol which inherits from a datagridview, in order to increase itsfunctionality. Basically, I would like to have some defined methods that I continuously use in projects when using the datagridview control.

the problems that till now I am encountering are 2:

1. in the user control designer, this error is being popped out:

The type 'System.Windows.Forms.DataGridView' has no property named 'AutoScaleMode'.

although I have defined this property, as shown below:

AutoScaleMode autoScaleMode;

public AutoScaleMode AutoScaleMode

{

get

{

return autoScaleMode;

}

set

{

autoScaleMode = value;

}

}

2. In my usercontrol, I have defined: this.AutoGenerateColumns = true, but when I am placing the user control in a form, the InitializeComponent changes it back to false. How could I override the default property so that it will always be set to true when the usercontrol is placed in a form?

Thanks

tigrot  Monday, April 07, 2008 9:01 AM

1. The error suggests that you are attempting to set the AutoScaleMode of the DataGridView inside your control, rather than of your control itself, which is the object on which you have defined that property. You haven't added AutoScaleMode to System.Windows.Forms.DataGridView, you've created a new type (something like MyProject.MyCustomControlClass) that inherits from DataGridView, and then added a property to THAT type.

2. If initialize component is setting the value to false, set the value to true after InilializeComponent() in the constructor.

JayStation3  Monday, April 07, 2008 11:41 AM

Hi JayStation3,

thanks for your reply.

Regarding 1, if I am inheriting from DataGridView, then its base class must have declared the AutoScaleMode as can be found from its properties. So why it keeps showing me that the property is not defined?

Regarding 2, I did know this solution, but i wanted to make it in a professional way instead of manually declaring it to true everytime I use the control.....Maybe in future I forget that in order for my control to work, I need to manually define the property as true. Hence I need something which automatically always defines it to true. Do you understand what I mean since my english is not very well Sad

Thanks for your suggestions

tigrot  Monday, April 07, 2008 12:07 PM

1) I don't think that the base DataGridView class has an AutoScaleMode property. You have defined it in your own class, right? So, you shouldn't have a DataGridView on your form, you have your own custom control, which does have an AutoScaleMode property. Creating a property in the inherited class does not add that property to the base class.

2) You know, I'm not 100 percent certain of the best way to do this -- I might try setting the value in the designer file. You're right, that you should not set the value every time in the form's code.

JayStation3  Monday, April 07, 2008 12:33 PM

With respect to theAutoScaleProperty, yheee...you are right, the datagridview does not implement the AutoScaleMode property. So how can I eliminate this error?

Thanks for your suggestion regarding the automatic set of a property, but as I said before, I would like it to be implemented automatically. Does anyone else have any idea how I can manage to always set a property to a default value as explained before?

thanks everyone

tigrot  Monday, April 07, 2008 1:54 PM

You need to set the property value in the custom control. Regular .NET controls have default values for their properties, but that still has to be explicitly set in the code somewhere. If you set it properly in the the definition of the control, then every time you add the control to your form, you will get that value "automatically."

Let's say your class iscalled myControl, and the property is myProperty, a boolean.

You should have a Private variable that holds the myProperty value -- let's call it myPropertyValue.

Now, when you Get() myProperty, you return myPropertyValue.

When you Set() myProperty, you set myPropertyValue == value

If you have never Set() myProperty, then when you Get() it (which includes when the control is initialized), it is going to return False.

So, when you first declare that class variable, myPropertyValue, you can assign it a value of True.

Now, unless you change it explicitly by Set(), myProperty will return True by default every time.

Build it, drag it from the ToolBox onto your form, and myProperty will be True.

--------

If you are creating a custom AutoScaleMode property for your control (not DataGridView, but your control that derives from it), then when you set the AutoScaleMode value, you have to apply it to an instance of your control, not an instance of DataGridView.

JayStation3  Monday, April 07, 2008 3:06 PM

yhee....that what I normally do and it always worked. I don't know why I was having problems but don't bother anymore since I removed the property....thanks for all your help.

but I still have got the problem of AutoScaleMode in the designer.

This is sthe error shown in the designer:

The type 'System.Windows.Forms.DataGridView' has no property named 'AutoScaleMode'.
HideEdit


at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.Error(IDesignerSerializationManager manager, String exceptionText, String helpLink)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializePropertyAssignStatement(IDesignerSerializationManager manager, CodeAssignStatement statement, CodePropertyReferenceExpression propertyReferenceEx, Boolean reportError)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeAssignStatement(IDesignerSerializationManager manager, CodeAssignStatement statement)
at System.ComponentModel.Design.Serialization.CodeDomSerializerBase.DeserializeStatement(IDesignerSerializationManager manager, CodeStatement statement
)

I don't have any idea why this is happening since I have done the following code:

public AutoScaleMode AutoScaleMode

{

get

{

return autoScaleMode;

}

set

{

autoScaleMode = value;

}

}

Anyone have got a solution please?

thanks

tigrot  Wednesday, April 09, 2008 7:01 AM

You can use google to search for other answers

Custom Search

More Threads

• Implementing help in touchscreen UI
• CPU and Memory Usage
• Multiple instances of Windows Media Player ActiveX control
• DataGridView: selection displayed on the whole row
• setting form size
• validate form
• Form1 does not display when I run the application I am making
• Tab Control is making my events unhook...Why?
• How to set the button image to a shell icon?
• Simple forms question...