Solved

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

Posted on 2014-02-19
10
1,040 Views
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.
DatagridViewLoad.jpg
DatagridViewControl.jpg
AutocompleteDataGridViewIssue.zip
0
Comment
Question by:JonBrehaut
  • 4
  • 3
  • 2
10 Comments
 
LVL 69

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
0
 
LVL 27

Expert Comment

by:Ark
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
0
 

Author Comment

by:JonBrehaut
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.
0
 
LVL 69

Expert Comment

by:Éric Moreau
ID: 39882111
So why when I tried your code, the grid remained empty? With my modification, it was filled.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:JonBrehaut
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?)
0
 
LVL 69

Expert Comment

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

Author Comment

by:JonBrehaut
ID: 39885027
With the sample supplied
0
 
LVL 27

Accepted Solution

by:
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

0
 

Author Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
This video discusses moving either the default database or any database to a new volume.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

706 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now