Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleDisplay read-only data from an Access database using ADO.NET
KeywordsVB.NET, NET, DataGrid, ADO.NET, Access, read-only data
CategoriesDatabase, VB.NET
 
Use an OleDbDataAdapter object to load the data into a DataSet. Make a DataView based on the data you want to display. In this example, the data is in the Contacts table.

Set the DataView's AllowDelete, AllowEdit, and AllowNew properties to False. Then bind the DataView to the DataGrid control for display. The DataGrid will automatically prevent the user from deleting, editing, or adding values.

 
Private Const SELECT_STRING As String = _
    "SELECT * FROM Books ORDER BY Title"

' The DataSet that holds the data.
Private m_DataSet As DataSet

' A DataView to allow read-only access the data.
Private m_DataView As DataView

' Load the data.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal _
    e As System.EventArgs) Handles MyBase.Load
    Dim database_name As String
    Dim connect_string As String
    Dim data_adapter As OleDbDataAdapter

    ' Compose the database file name.
    ' Modify this if the database is somewhere else.
    database_name = Application.StartupPath()
    database_name = database_name.Substring(0, _
        database_name.LastIndexOf("\"))
    database_name = database_name & "\Books.mdb"

    ' Compose the connect string. 
    connect_string = _
        "Provider=Microsoft.Jet.OLEDB.4.0;" & _
        "Data Source=" & database_name

    ' Create the OleDbDataAdapter.
    data_adapter = New OleDbDataAdapter(SELECT_STRING, _
        connect_string)

    ' Map Table to Contacts.
    data_adapter.TableMappings.Add("Table", "Books")

    ' Fill the DataSet.
    m_DataSet = New DataSet()
    data_adapter.Fill(m_DataSet)

    ' Make a DataView for the data.
    m_DataView = New DataView(m_DataSet.Tables("Books"))
    m_DataView.AllowDelete = False
    m_DataView.AllowEdit = False
    m_DataView.AllowNew = False

    ' Bind the DataGrid control to the DataView.
    dgContacts.DataSource = m_DataView
End Sub
 
For more information on using databases in Visual Basic .NET, see my book Visual Basic .NET Database Programming.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated