实例124DataGridView显示DataTable数据

1.添加数据源:northwind.mdb

2.设计一个窗体,添加一个DataGridView

实例124DataGridView显示DataTable数据_第1张图片

3.写代码

Imports System.Data.OleDb

Public Class Form1
    Public Function GetConncetion() As OleDbConnection
        Return New OleDbConnection(My.Settings.northwindConnectionString)
    End Function
    Private myConn As OleDbConnection

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        DataGridView1.DataSource = Nothing

        myConn = GetConncetion()
        myConn.Open()

        Dim strSql As String = "Select 公司名称,联系人姓名,联系人职务,城市,电话 From 4客户 " & _
                               "Order By 城市"
        Dim myComm As OleDbCommand = New OleDbCommand(strSql, myConn)
        Dim myDataReader As OleDbDataReader = myComm.ExecuteReader
        Dim myTable As New DataTable
        myTable.Load(myDataReader)

        DataGridView1.DataSource = myTable

        myConn.Close()
    End Sub
End Class

 

你可能感兴趣的:(#,VB2010编程技巧与实例)