vb/net add datarow to datagridview code example

Example 1: vb.net add row to datagridview programmatically

Private Sub SurroundingSub()
    Dim rowId As Integer = dataGridView1.Rows.Add()
    Dim row As DataGridViewRow = dataGridView1.Rows(rowId)
    row.Cells("Column1").Value = "Value1"
    row.Cells("Column2").Value = "Value2"
End Sub

Example 2: add rows to datagridview vb.net

'Add rows
Me.DataGridView1.Rows.Add(TextBox1.Text) 'TextBox1 = data being added to grid

'Remove rows

'making sure the user has chosen a row
If DataGridView1.SelectedRows.Count > 0 Then
	For i As Integer = DataGridView1.SelectedRows.Count - 1 To 0 Step -1
		DataGridView1.Rows.RemoveAt(DataGridView1.SelectedRows(i).Index)
	Next
Else
	MsgBox("No row(s) have been selected")
End If

Tags:

Vb Example