Imports System.Data.SqlClient
Public Class Form1
Private dataAdapter As New SqlDataAdapter()
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Try
Me.DataGridView1.DataSource = Me.BindingSource1
' Specify a connection string. Replace the given value with a
' valid connection string for a Northwind SQL Server sample
' database accessible to your system.
Dim connectionString As String = "Data Source=MATHEW-PC\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True"
' Create a new data adapter based on the specified query.
Me.dataAdapter = New SqlDataAdapter("select URL, Name from rssfeeds", connectionString)
' 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(Me.dataAdapter)
' Populate a new data table and bind it to the BindingSource.
Dim table As New DataTable()
table.Locale = System.Globalization.CultureInfo.InvariantCulture
Me.dataAdapter.Fill(table)
Me.BindingSource1.DataSource = table
' Resize the DataGridView columns to fit the newly loaded content.
Me.DataGridView1.AutoResizeColumns( _
DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader)
Catch ex As SqlException
MessageBox.Show("To run this example, replace the value of the " + _
"connectionString variable with a connection string that is " + _
"valid for your system.")
End Try
End Sub
Private Sub addNewRowButton_Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles btnAdd.Click
Me.DataGridView1.Rows.Add()
End Sub
Private Sub deleteRowButton_Click(ByVal sender As Object, _
ByVal e As EventArgs) Handles btnDelete.Click
If Me.DataGridView1.SelectedRows.Count > 0 AndAlso _
Not Me.DataGridView1.SelectedRows(0).Index = _
Me.DataGridView1.Rows.Count - 1 Then
Me.DataGridView1.Rows.RemoveAt( _
Me.DataGridView1.SelectedRows(0).Index)
End If
End Sub
End Class