Building a composite control with a DataGridView

  • Thread starter Thread starter Jon F
  • Start date Start date
J

Jon F

I'd like to create a UserControl that consists of a DataGridView and a
couple of other things such as e.g. a button for resetting a filter.

The desired result would be to be able to use the UserControl just as a
DataGridView, i.e. without the need for the UserControl to publish the grid
by means of a property, but I can't seem to find a way to do that. If I'd
have to rather inherit from DataGridView I wouldn't know how to then add
other controls to it.

I thus created a public property "GridControl" that allows setting the
grid's properties when the UserControl is being used on a form. However,
when I attempt to access the columns collection I get a "Object reference
not set to an instance of an object." exception. While I can access the
columns collection by code, I'd prefer to be able to set them at design
time.

What would be the best approach for this?

Regards,
J.
 
Is the type of GridControl a DataGridView? If so, I don't think you will
get to work in the designer (although it may be a handy shortcut for
managing the DataGridView from code). The columns property should be a
Public Property of your user control, and should map directly through to the
columns property of the DataGridView in the control. Eg:

Public Property Columns() As DataGridViewColumnCollection
Get
Return DataGridView1.Columns
End Get
Set(ByVal value As DataGridViewColumnCollection)
DataGridView1.Columns.Clear()
For Each C As DataGridViewColumn In value
DataGridView1.Columns.Add(C)
Next
End Set
End Property

That's the theory. You will, however, immediately run into the problem
described here:
http://social.msdn.microsoft.com/Fo...r/thread/c0903eb4-53fa-4881-82ed-1a07fb88dc83

It appears that the solution may be here:
http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework.windowsforms/topic57961.aspx

but I don't know if it's still available.
 
James said:
Is the type of GridControl a DataGridView?
yes.

That's the theory. You will, however, immediately run into the problem
described here:
http://social.msdn.microsoft.com/Fo...r/thread/c0903eb4-53fa-4881-82ed-1a07fb88dc83

It appears that the solution may be here:
http://www.devnewsgroups.net/group/microsoft.public.dotnet.framework.windowsforms/topic57961.aspx

but I don't know if it's still available.

wow, pretty much work for something I had expected to be available out of
the box! But anyway, it's good to know that there IS a working way of
dealing with this, so, thank you sir!

Best,
J.
 
Back
Top