i want to ask something about datagridview in visual basic
i have 1 form name 'form1' and 1 class name 'test' in my project, within the class there is 1 method 'addColumn()'.
i want to add column to datagridview programmatically, when i add directly to form1 from method form1_load, it's success. the code like this
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
DataGridView1.Columns.Add("01", 1)
But when i add the column via method from class test,
Dim obj As test
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
obj.addColumns(DataGridView1)
End Sub
i got error this
"Object reference not set to an instance of an object."
code in class test like this
Public Function addColumns(ByVal dgv As DataGridView)
dgv.Columns.Add("01", 1)
End Function
What wrong with my code?, there are any way to solve this error. Please help me...
You are not declaring a New test object, hence the error.
Dim obj As New test
Or make the function a Shared Function
Public Shared Function addColumns(ByVal dgv As DataGridView)
dgv.Columns.Add("01", 1)
End Function
Then no need to declare any obj object. Use the function directly.
test.addColumns(DataGridView1)