YourPaste - For your paste! Archive - Tools - Login

Dataset Example

Posted by earlbob on Fri 3 Jul 2009 1:19 243 views - Syntax: VB.NET - Expires: never - Report - IMG - Download -

  1. ' Import Data and SqlClient namespaces...
  2. Imports System.Data
  3. Imports System.Data.SqlClient
  4.  
  5. Public Class Form1
  6.     Dim objConnection As New SqlConnection _
  7.         ("server=myserver;database=pubs;user id=sa;password=mypassword")
  8.  
  9.     Dim objDataAdapter As New SqlDataAdapter()
  10.     Dim objDataSet As New DataSet()
  11.  
  12.     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
  13.         Handles Me.Load
  14.         ' Set the SelectCommand properties...
  15.         objDataAdapter.SelectCommand = New SqlCommand()
  16.         objDataAdapter.SelectCommand.Connection = objConnection
  17.         objDataAdapter.SelectCommand.CommandText = _
  18.             "SELECT au_lname, au_fname, title, price " & _
  19.             "FROM authors " & _
  20.             "JOIN titleauthor ON authors.au_id = titleauthor.au_id " & _
  21.             "JOIN titles ON titleauthor.title_id = titles.title_id " & _
  22.             "ORDER BY au_lname, au_fname"
  23.         objDataAdapter.SelectCommand.CommandType = CommandType.Text
  24.  
  25.         ' Open the database connection...
  26.         objConnection.Open()
  27.  
  28.         ' Fill the DataSet object with data...
  29.         objDataAdapter.Fill(objDataSet, "authors")
  30.  
  31.         ' Close the database connection...
  32.         objConnection.Close()
  33.  
  34.         ' Set the DataGridView properties to bind it to our data...
  35.         grdAuthorTitles.AutoGenerateColumns = True
  36.         grdAuthorTitles.DataSource = objDataSet
  37.         grdAuthorTitles.DataMember = "authors"
  38.         ' Declare and set the currency header alignment property...
  39.         Dim objAlignRightCellStyle As New DataGridViewCellStyle
  40.         objAlignRightCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
  41.  
  42.         ' Declare and set the alternating rows style...
  43.         Dim objAlternatingCellStyle As New DataGridViewCellStyle()
  44.         objAlternatingCellStyle.BackColor = Color.WhiteSmoke
  45.         grdAuthorTitles.AlternatingRowsDefaultCellStyle = objAlternatingCellStyle
  46.  
  47.         ' Declare and set the style for currency cells ...
  48.         Dim objCurrencyCellStyle As New DataGridViewCellStyle()
  49.         objCurrencyCellStyle.Format = "c"
  50.         objCurrencyCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
  51.  
  52.         ' Change column names and styles using the column index
  53.         grdAuthorTitles.Columns(0).HeaderText = "Last Name"
  54.         grdAuthorTitles.Columns(1).HeaderText = "First Name"
  55.         grdAuthorTitles.Columns(2).HeaderText = "Book Title"
  56.         grdAuthorTitles.Columns(2).Width = 225
  57.  
  58.         ' Change column names and styles using the column name
  59.         grdAuthorTitles.Columns("price").HeaderCell.Value = "Retail Price"
  60.         grdAuthorTitles.Columns("price").HeaderCell.Style = objAlignRightCellStyle
  61.         grdAuthorTitles.Columns("price").DefaultCellStyle = objCurrencyCellStyle
  62.  
  63.         ' Clean up
  64.         objDataAdapter = Nothing
  65.         objConnection = Nothing
  66.         objCurrencyCellStyle = Nothing
  67.         objAlternatingCellStyle = Nothing
  68.         objAlignRightCellStyle = Nothing
  69.  
  70.     End Sub
  71. End Class
  72.  

Comments


Name:
Comment:

© 2010 YourPaste.net - Disclaimer