Results 1 to 4 of 4

Thread: [RESOLVED] DataGridView: getting "Object reference not set to an instance of an object"

  1. #1

    Thread Starter
    Member
    Join Date
    Sep 2017
    Location
    UK, France, Belgium, Ireland
    Posts
    34

    Resolved [RESOLVED] DataGridView: getting "Object reference not set to an instance of an object"

    I want to dynamically change the source of data for a DataGridView. So I have been trying to use this Microsoft example as a basis.

    I've now spent about six hours spread spread over two and a half days trying to get this to work and I'm now exhausted and have to get other things done. So I hope someone else will share my pain, or maybe even solve the problem.

    So far I have reduced the above MS example to a bare bones example that still works. But unfortunately it dynamically adds a DataGridView control, and I don't want to do that. I want to place a DataGridView myself at design time and simply populate it at runtime -- all that actually changes is the WHERE clause of the underlying SQL statement. (At design time the DataGridView is based on a SQL Server view).

    What I've now tried is adding a DataGridView at design time and, in the code, commented out where a DataGridView is declared and added to a FlowLayoutPanel. However, on running it I get an error as per the attached screendump.
    Name:  DataGridView.jpg
Views: 3074
Size:  27.7 KB

    There is no problem running it where the DataGridView is added dynamically as per the full code below. I don't understand why it's happy to use the BindingSource in one case but not the other. Any ideas?

    Code:
    Imports System
    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Windows.Forms
    
    Public Class Form1
        Inherits System.Windows.Forms.Form
    
        Private dgParentChildren As New DataGridView()
        Private bsParentChildren As New BindingSource()
        Private daParentChildren As New SqlDataAdapter()
        Public m_intID As Integer
    
    
        ' Initialize the form.
        Public Sub New()
    
            Dim panel As New FlowLayoutPanel()
            panel.Dock = DockStyle.Top
            panel.AutoSize = True
    
            Controls.AddRange(New Control() {dgParentChildren, panel})
    
    
        End Sub
    
        Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            m_intID = 140
            ' Bind the DataGridView to the BindingSource and load the data from the database.
            dgParentChildren.DataSource = bsParentChildren
            Dim selectStatement As String
            selectStatement = ""
            selectStatement = selectStatement & " SELECT tblAssociations.colChildID, tblItems.colName AS ItemName, tblItemTypes.colName AS ItemTypeName"
            selectStatement = selectStatement & " FROM tblItems INNER JOIN"
            selectStatement = selectStatement & " tblItemTypes ON tblItems.colItemTypeID = tblItemTypes.colID INNER JOIN"
            selectStatement = selectStatement & " tblAssociations ON tblItems.colID = tblAssociations.colChildID"
            selectStatement = selectStatement & " WHERE tblAssociations.colParentID = " & m_intID
    
            GetData(selectStatement)
    
        End Sub
    
    
        Private Sub GetData(ByVal selectCommand As String)
    
            Try
    
                ' Create a new data adapter based on the specified query.
                daParentChildren = New SqlDataAdapter(selectCommand, g_cn)
    
                ' Create a command builder to generate SQL update, insert, and
                ' delete commands based on selectCommand. These are used to
                ' update the database.
                Dim commandBuilder As New SqlCommandBuilder(daParentChildren)
    
                ' Populate a new data table and bind it to the BindingSource.
                Dim dtParentChildren As New DataTable()
                dtParentChildren.Locale = System.Globalization.CultureInfo.InvariantCulture
                daParentChildren.Fill(dtParentChildren)
                bsParentChildren.DataSource = dtParentChildren
    
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
    
        End Sub
    
    End Class
    EDIT: Or of course if there's a simpler way to change the WHERE clause instead of the elaborate Microsoft way, then do please advise. (I wasn't able to find one after some random experimentation.)
    Last edited by ktrammen; Nov 4th, 2017 at 09:06 AM.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: DataGridView: getting "Object reference not set to an instance of an object"

    That error message seems to indicate that you have a property named dgParentChildren. If you had simply added a control with that name in the designer then you'd have a field rather than a property.

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,927

    Re: DataGridView: getting "Object reference not set to an instance of an object"

    In simpler terms, remove this line:
    Quote Originally Posted by ktrammen View Post
    Private dgParentChildren As New DataGridView()
    ...and make sure that either your control is called dgParentChildren, or you change all instances of dgParentChildren in the code to the name of your control.


    Quote Originally Posted by ktrammen View Post
    EDIT: Or of course if there's a simpler way to change the WHERE clause instead of the elaborate Microsoft way, then do please advise. (I wasn't able to find one after some random experimentation.)
    You can do the same as changing the Where clause by using the .Filter property of the BindingSource, the documentation has an example: https://docs.microsoft.com/en-us/dot...ramework-4.7.1
    (note that most of the code in the example is to get some data in the first place, to alter which rows are displayed you only need to change .Filter)

  4. #4

    Thread Starter
    Member
    Join Date
    Sep 2017
    Location
    UK, France, Belgium, Ireland
    Posts
    34

    Re: DataGridView: getting "Object reference not set to an instance of an object"

    Quote Originally Posted by si_the_geek View Post
    In simpler terms, remove this line:
    ...and make sure that either your control is called dgParentChildren, or you change all instances of dgParentChildren in the code to the name of your control.
    This was one of the first things I did (and I thought I'd indicated this in my OP), and it was precisely this that gave rise to the error, which gave rise to my OP...

    Quote Originally Posted by si_the_geek View Post
    You can do the same as changing the Where clause by using the .Filter property of the BindingSource, the documentation has an example: https://docs.microsoft.com/en-us/dot...ramework-4.7.1
    Yes, this works. A whole lot simpler! I had tried ploughing through the various properties and methods of the various object types, but I didn't come across that (though I must admit my ploughing was a bit random). Many thanks.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width