Improve company productivity with a Business Account.Sign Up


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
  • 4
  • 3
  • 2
LVL 71

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.
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

LVL 71

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 71

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

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Native ability to set a user account password via AD GPO was removed because the passwords can be easily decrypted by any authenticated user in the domain. Microsoft recommends LAPS as a replacement and I have written an article that does something …
From store locators to asset tracking and route optimization, learn how leading companies are using Google Maps APIs throughout the customer journey to increase checkout conversions, boost user engagement, and optimize order fulfillment. Powered …
In the video, one can understand the process of resizing images in single or bulk. Kernel Bulk Image Resizer is an easy to use tool for resizing large number of images. One can add and resize multiple images with this tool in single go. The video sh…

601 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