Guest
Private paste!

Untitled

By: a guest | Apr 29th, 2010 | Syntax: VB.NET | Size: 2.38 KB | Hits: 122 | Expires: Never
Copy text to clipboard
  1. Imports System.Data.SqlClient
  2.  
  3. Public Class Form1
  4.  
  5.     Private dataAdapter As New SqlDataAdapter()
  6.  
  7.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  8.  
  9.         Try
  10.             Me.DataGridView1.DataSource = Me.BindingSource1
  11.             ' Specify a connection string. Replace the given value with a
  12.             ' valid connection string for a Northwind SQL Server sample
  13.             ' database accessible to your system.
  14.             Dim connectionString As String = "Data Source=MATHEW-PC\SQLEXPRESS;Initial Catalog=Test;Integrated Security=True"
  15.  
  16.             ' Create a new data adapter based on the specified query.
  17.             Me.dataAdapter = New SqlDataAdapter("select URL, Name from rssfeeds", connectionString)
  18.  
  19.             ' Create a command builder to generate SQL update, insert, and
  20.             ' delete commands based on selectCommand. These are used to
  21.             ' update the database.
  22.             Dim commandBuilder As New SqlCommandBuilder(Me.dataAdapter)
  23.  
  24.             ' Populate a new data table and bind it to the BindingSource.
  25.             Dim table As New DataTable()
  26.             table.Locale = System.Globalization.CultureInfo.InvariantCulture
  27.             Me.dataAdapter.Fill(table)
  28.             Me.BindingSource1.DataSource = table
  29.  
  30.             ' Resize the DataGridView columns to fit the newly loaded content.
  31.             Me.DataGridView1.AutoResizeColumns( _
  32.                 DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader)
  33.         Catch ex As SqlException
  34.             MessageBox.Show("To run this example, replace the value of the " + _
  35.                 "connectionString variable with a connection string that is " + _
  36.                 "valid for your system.")
  37.         End Try
  38.  
  39.     End Sub
  40.  
  41.     Private Sub addNewRowButton_Click(ByVal sender As Object, _
  42.         ByVal e As EventArgs) Handles btnAdd.Click
  43.  
  44.         Me.DataGridView1.Rows.Add()
  45.  
  46.     End Sub
  47.  
  48.     Private Sub deleteRowButton_Click(ByVal sender As Object, _
  49.         ByVal e As EventArgs) Handles btnDelete.Click
  50.  
  51.         If Me.DataGridView1.SelectedRows.Count > 0 AndAlso _
  52.             Not Me.DataGridView1.SelectedRows(0).Index = _
  53.             Me.DataGridView1.Rows.Count - 1 Then
  54.  
  55.             Me.DataGridView1.Rows.RemoveAt( _
  56.                 Me.DataGridView1.SelectedRows(0).Index)
  57.  
  58.         End If
  59.  
  60.     End Sub
  61.  
  62.  
  63. End Class