• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 473
  • Last Modified:

unlock data grid view properties in a user control

Hey there

I`m working on this user control using vb.net 2005 that is a form with a data grid view in it

but when I use this user control and try to modify the grid view properties the grid view is locked

and can`t change any of the grid view properties

is there any way that enables the properties of the grid view

thanks in advance
0
mohammadalsayeh
Asked:
mohammadalsayeh
  • 3
1 Solution
 
jpaulinoCommented:
Aren't you in run mode ?
0
 
SanclerCommented:
When you put a control (such as a datagridview) into a usercontrol the only properties, methods and events of the "internal" control (the datagridview) that are exposed by the "external" control (the usercontrol) are those that you explicitly code for.  That is, all that you now have access to are the properties, methods and events of the user control itself and unless you give THAT a method which will, for instance, change the background color of the (Internal) datagridview you will not be able to alter that property of the datagridview.

What you could do is give your user control a (probably read-only) property to return the datagridview.  And then you can use that to get direct hold of the (Internal) datagridview and so gain direct access to its properties and methods.  Something like this in the usercontrol (VB rather than C#, but easily translateable I imagine)

    Public ReadOnly Property DGV()
        Get
            Return myDataGridView
        End Get
    End Property

and then in the form on which the user control is located

        Dim dgv As DataGridView = myUserControl.DGV
        dgv.BackgroundColor = Color.Blue

Roger
0
 
mohammadalsayehAuthor Commented:
is that the only possible solution for this??

can`t I change the propeties of the grid when I use the user control??
0
 
SanclerCommented:
Not so far as I know.  

One feature of OO programming is encapsulation.  A class is only accessible to objects outside the class through the properties, methods and events that the class design expressly sets as Public.  When you put a DataGridView into a UserControl the class is the UserControl.  The DataGridView becomes "encapsulated" in the UserControl.  That means that the UserControl can change the properties of the DataGridView, but none of the properties, methods or events of the DataGridView is accessible to anything outside the UserControl.  

As the outside world is dealing with the UserControl, THAT can be designed to accept "instructions" that it will "pass on" to the encapsulated DataGridView.  I gave you a general example - of actually getting hold of the datagridview - in my last post.  An example of the use of a property of the user control that would achive the same thing would be code like this

    Public Property dgvBackgroundColor() As System.Drawing.Color
        Get
            Return myDataGridView.BackgroundColor
        End Get
        Set(ByVal value As System.Drawing.Color)
            myDataGridView.BackgroundColor = value
        End Set
    End Property

in the UserControl and like this

        myUserControl.dgvBackgroundColor = Color.Blue

in the form.

But I don't think you can both encapsulate the DataGridView in a UserControl and then also expect to change its properties as you would if it were a free-standing control.

Roger
0
 
SanclerCommented:
Perhaps I should have added one point.  If you want to make the property from the example above visible in the Properties Pane for the UserControl you need to add Browsable to the declaration.  Like this

    <System.ComponentModel.Browsable(True)> _
Public Property dgvBackgroundColor() As System.Drawing.Color

Roger
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now