What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleDisplay read-only data from an MSDE or SQL Server database using ADO.NET
KeywordsVB.NET, NET, DataGrid, ADO.NET, MSDE, SQL Server, read-only data
CategoriesDatabase, VB.NET
Use a SqlDataAdapter 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 Contacts ORDER BY LastName, FirstName"
Private Const CONNECT_STRING As String = _
    "Data Source=Bender\NETSDK;Initial " & _
        "Catalog=Contacts;User Id=sa"

' 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 data_adapter As SqlDataAdapter

    ' Create the SqlDataAdapter.
    data_adapter = New SqlDataAdapter(SELECT_STRING, _

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

    ' Fill the DataSet.
    m_DataSet = New DataSet()

    ' Make a DataView for the data.
    m_DataView = New DataView(m_DataSet.Tables("Contacts"))
    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.

Note that this example uses an MSDE database. Click here for information on MSDE including instructions for installing it for free.

You will also need build a database for the program to use and change the connect code shown here to match. This example uses an MSDE server named NETSDK on the computer Bender and takes data from the Contacts database.

Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.