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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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.
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

In my previous article ( we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

623 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