DatagridView in Custom User Control in VB.NET not loading data

Posted on 2014-02-19
Last Modified: 2014-02-26
I'm trying to build a custom user component which utilises a datagridview within it.
The problem is, no matter what I try, I cannot get the grid to load a datasource, it just ignores it.
It seems to be down to the fact the grid is in a Component class as calling out the supplied data to a grid on the form works everytime.

Has anyone come across this before and/or knows how to cure it?

(Attached is source example and screenshots of problem area). To test enter 'sm' in surname field, a grid should pop-up with matching records.
Question by:JonBrehaut
  • 4
  • 3
  • 2
LVL 70

Expert Comment

by:Éric Moreau
ID: 39879172
You never set your BindingSource! Try this (at least you will see data in your grid):

      Friend WriteOnly Property LookupData() As DataTable
            Set(value As DataTable)
            p_LookupData = value
            p_BindingSource.DataSource = value
            RaiseEvent ShowData(p_BindingSource)
            End Set
      End Property
LVL 28

Expert Comment

ID: 39881918
IMHO it's visible/invisible issue. AFAIK DataGridView calculates layout only when need (including .ColumnsCount, .RowsCount etc), so when dgv (or its container) is invisible .ColumnsCount = 0

Author Comment

ID: 39881995
Erik: The Bindsource is set, it may be unclear from the code but it has a data table, like I mentioned the 'external' grid on the form receives the bindingsource perfectly happily on from the Event call "ShowData".

Ark: Good idea however doesn't seem to work in practice: I've rearranged the code and have made the grid (and container) visible before setting the source and it still does the same thing.
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 70

Expert Comment

by:Éric Moreau
ID: 39882111
So why when I tried your code, the grid remained empty? With my modification, it was filled.

Author Comment

ID: 39883268
Eric: The problem is not with the datagrid in the form (which is just there for testing purposes): The issue is with the datagrid being created on the fly, I did try your code and that grid still remains blank (dark grey) or did it work for you (in which case it must point to some other issue?)
LVL 70

Expert Comment

by:Éric Moreau
ID: 39883289
have you tested my fix with the sample you attached to this question or with something else?

Author Comment

ID: 39885027
With the sample supplied
LVL 28

Accepted Solution

Ark earned 500 total points
ID: 39888118
Hm, seems I found it. Not sure why but it looks like dgv inside a controlhost doesn't assigned to the binding context. To fix just add:
Private Sub InitialiseFormEventCapture(thisControl As Control)
		If Not p_Form Is Nothing Then Return 'Already performed
		If thisControl Is Nothing Then Return

		Dim form As Form = thisControl.FindForm()
        If form Is Nothing Then Return
        p_DGV.BindingContext = form.BindingContext '<=== ADDED

Open in new window


Author Comment

ID: 39888388
That's it, now works perfectly. Brilliant find, many thanks I'd almost given up on it.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Upgrading my SSIS package in VS 2012 6 83 Configuration, settings file location? 8 41 convert long time to mm:ss 23 20 figure out error 3 17
This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question