I'm working on a Windows Form project and am having issues with one of the DataGridView objects in one of the forms. This is what I have so far:
Private Sub CustomerUsageForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Tbl_usageTableAdapter1.Fill(Me.ClarityWaterDBMAINDataSet1.tbl_usage)
Dim con As New OleDb.OleDbConnection
Dim dbString As String
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
dbString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\ClarityWaterDBMAIN.accdb"
con.ConnectionString = dbString
con.Open()
sql = "SELECT * FROM tbl_usage"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "tbl_usage")
con.Close()
Then when the form is activated this code is run:
Private Sub CustomerUsageForm_Activated(sender As Object, e As EventArgs) Handles MyBase.Activated
Dim GridInitializedBoolean As Boolean = False
'Initialize the grid's binding
If Not GridInitializedBoolean Then
'bind and format the grid
DataGridView1.DataSource = BindingSource1
SetUpGridColumns()
GridInitializedBoolean = True
End If
End Sub
Private Sub SetUpGridColumns()
'sets up the columns for the data grid view
Try
With DataGridView1
.Columns!usage_id.Visible = False
.Columns!reading_date.HeaderText = "Reading Date"
.Columns!reading_cf.HeaderText = "Amount Used"
.Columns!account_id.HeaderText = "Account ID"
End With
Catch ex As Exception
MessageBox.Show("Error Setting up the grid." & ex.Message)
End Try
End Sub
When I load the form there is a NullReferenceException thrown from the SetUpGridColumns() method. I'm not sure why this is happening, I have this form set up exactly like another form that works perfectly:
Private Sub InventoryForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'ClarityWaterDBMAINDataSet.tbl_customers' table. You can move, or remove it, as needed.
Me.Tbl_customersTableAdapter.Fill(Me.ClarityWaterDBMAINDataSet.tbl_customers)
Dim con As New OleDb.OleDbConnection
Dim dbString As String
Dim ds As New DataSet
Dim da As OleDb.OleDbDataAdapter
Dim sql As String
dbString = "Provider=Microsoft.ACE.OLEDB.12.0;DataSource=|DataDirectory|\ClarityWaterDBMAIN.accdb"
con.ConnectionString = dbString
con.Open()
sql = "SELECT * FROM tbl_customers"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "tbl_customers")
con.Close()
AccountNumberTextBox.Focus()
CustomerUsageForm.CustomerUsageForm_Load(sender, e)
CustomerUsageForm.Hide()
End Sub
Then this is the event on the button click event
Private Sub SearchButton_Click(sender As Object, e As EventArgs) Handles SearchButton.Click
'retrieve the customer search information
Dim GridInitializedBoolean As Boolean = False
'initialize the grids binding
If Not GridInitializedBoolean Then
'bind and format the grid
CompanyIDLookupDataGridView.DataSource = CompanyLookupBindingSource
SetUpGridColumns()
GridInitializedBoolean = True
End If
Try
CompanyLookupBindingSource.Filter = BuildSearchString()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
I sorry for such a long post. I'm honestly not sure what else to try. Can anyone help explain why I'm having these issues?
EDIT: What I mean by set up exactly the same way is that both forms have the same types of objects, the Load events are coded the same, and the events that initialize the grid are the same.
If I comment out the first .Columns! line from the SetUpGridColumns() method, the error is thrown on the next line.