Dear sir,
I'm trying to add a Row DataGridView with Event Button in VB.NET
But I want to add a row after the 3 columns are filled, I try the code below there is an error `Object reference not set to an instance of an object.` in button1 event
Please guide me
Thanks
Public Class Form1
Private BindingSource1 As BindingSource = Nothing
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Hide the last blank line.
DataGridView1.AllowUserToAddRows = False
'Clear Columns.
DataGridView1.Columns.Clear()
'Add Columns.
Dim customerId As DataGridViewColumn = New DataGridViewTextBoxColumn()
customerId.Name = "CustomerId"
customerId.HeaderText = "CustomerId Id"
customerId.DataPropertyName = "CustomerId"
customerId.Width = 100
DataGridView1.Columns.Insert(0, customerId)
Dim name As DataGridViewColumn = New DataGridViewTextBoxColumn()
name.HeaderText = "Name"
name.Name = "Name"
name.DataPropertyName = "Name"
name.Width = 100
DataGridView1.Columns.Insert(1, name)
Dim country As DataGridViewColumn = New DataGridViewTextBoxColumn()
country.Name = "Country"
country.HeaderText = "Country"
country.DataPropertyName = "Country"
country.Width = 100
DataGridView1.Columns.Insert(2, country)
'Bind the DataGridView.
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn(2) {New DataColumn("CustomerId"), New DataColumn("Name"), New DataColumn("Country")})
BindingSource1 = New BindingSource With {.DataSource = dt}
DataGridView1.DataSource = BindingSource1
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
`error below line code
If Not String.IsNullOrEmpty(DataGridView1.CurrentRow.Cells("CustomerId").Value?.ToString()) And Not String.IsNullOrEmpty(DataGridView1.CurrentRow.Cells("Name").Value?.ToString()) And Not String.IsNullOrEmpty(DataGridView1.CurrentRow.Cells("Country").Value?.ToString()) Then
BindingSource1.AddNew()
End If
End Sub
End Class
at the time of form load there is no row blank at all because I use
DataGridView1.AllowUserToAddRows = False
How did I add a row blank first and Prevent insert more blank row if there blank row already? (when button1 is click) and then if the cells of the 3 columns are filled then I can add row blank.