DataGridView control not loading

bh0526

Member
Joined
May 11, 2015
Messages
15
Programming Experience
10+
It's been 19 years since I developed a windows form using VB. I've only done web development since. So I created a windows form and dragged a DataGridView control to my form. I then added 2 columns. I have a class library where I have a function that returns a datatable. This works as I use it on a web form. But when I run my windows form, I get the DataGridView with no data. Here is my code:

Imports System.Configuration
Imports OrdersClasses
Public Class Form1
Dim connStringNW As String = ConfigurationManager.ConnectionStrings("MRInw").
ConnectionString Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load
dgvOrders.DataSource = GetDataTable()
End Sub

Private Function GetDataTable() As DataTable
Dim dt As New DataTable(connStringNW)
dt = objOrder.GetOrderByStatusCode("I")
Return dt
End Function
End Class


This method works perfectly on my web form: objOrder.GetOrderByStatusCode("I") So what am I doing wrong? Why is my DataGridView not displaying my data?
 
I had a copy / paste error I just noticed on my original post. I had the connection string as a parameter when i was building my datatable which would bomb. I fixed the code below:

Imports System.Configuration
Imports OrdersClasses
Public Class Form1
Dim connStringNW As String = ConfigurationManager.ConnectionStrings("MRInw").
ConnectionString Private Sub Form1_Load(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles MyBase.Load
dgvOrders.DataSource = GetDataTable()
End Sub

Private Function GetDataTable() As DataTable
Dim oOrder As New Order(connStringNW)
Dim dt As New DataTable()
dt = oOrder.GetOrderByStatusCode("I")
Return dt
End Function
End Class
 
Last edited:
I'm not sure whether you're saying that you still have an issue that you didn't express correctly in your first post or that you have actually corrected the issue you reported in your first post.
 
I'm not sure whether you're saying that you still have an issue that you didn't express correctly in your first post or that you have actually corrected the issue you reported in your first post.

Sorry for the confusion. No, it's not working. I made that last post since I had a copy / paste error in my original post. But I still cannot get my datagridview control populated from a method that works perfectly for my web application.
 
One thing to be aware of is that exceptions thrown in the Load event handler will be swallowed and the application continue. I'd suggest that you wrap the contents of your Load event handler in a Try...Catch block and, if there is an exception, examine it to determine what the issue is.
 
One thing to be aware of is that exceptions thrown in the Load event handler will be swallowed and the application continue. I'd suggest that you wrap the contents of your Load event handler in a Try...Catch block and, if there is an exception, examine it to determine what the issue is.

I got it working by putting all my ADO.Net code directly in my form page load event. Not sure why my class file method does not work since it works fine for my web applications.
 
Back
Top