Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


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

Posted on 2014-02-19
Medium Priority
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.
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

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 2000 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


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
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:…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…

722 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