VB Auto从datatable填充datagridview

时间:2013-11-13 00:42:31

标签: vb.net

是否可以直接从数据中填充datagrieview?以下是我在表单加载中尝试的内容。基本上我希望它显示此查询自动返回的所有列

Dim con As New OleDbConnection
    con = New OleDbConnection(connStr)
    If con.State = ConnectionState.Closed Then
        con.Open()
    End If
    Dim titleSQLStr As String = "SELECT * FROM Titles ORDER BY YearPublished DESC"
    daYears = New OleDbDataAdapter(titleSQLStr, connStr)
    daYears.Fill(dtYears)
    cboYearsFillBy.DataSource = dtYears
    cboYearsFillBy.DisplayMember = "YearPublished"
    cboYearsFillBy.ValueMember = "YearPublished"
    DataGridView1.AutoGenerateColumns = True
    DataGridView1.DataSource = daYears
    con.Close()

1 个答案:

答案 0 :(得分:1)

将其更改为:

DataGridView1.DataSource = dtYears