为DataGridView CellFormatting声明一个DataSet

时间:2013-04-02 11:06:30

标签: vb.net winforms datagridview dataset

我试图在datagridview中更改行颜色,但过去常用于我的代码需要一个数据集。这是我用来填充datagridview的代码

    Dim sql As String
    sql = "SELECT * FROM [product info]"
    Dim adapter As New OleDbDataAdapter(sql, strcon)
    Dim dt As New DataTable("[product info]")
    adapter.Fill(dt)
    DataGridView1.DataSource = dt

    Dim sql1 As String
    sql1 = "SELECT * FROM [product info]"
    Dim adapter1 As New OleDbDataAdapter(sql1, strcon)
    Dim cmd As New OleDbCommand(sql1, strcon)
    strcon.Open()
    Dim myreader As OleDbDataReader = cmd.ExecuteReader
    myreader.Read()

    strcon.Close()

如何在这行代码中声明数据集?

2 个答案:

答案 0 :(得分:0)

    Dim sql As String
    sql = "SELECT * FROM [product info]"
    Dim adapter As New OleDbDataAdapter(sql, strcon)
    Dim ds As New DataSet("[product info]")
    adapter.Fill(ds)
    DataGridView1.DataSource = ds

    Dim sql1 As String
    sql1 = "SELECT * FROM [product info]"
    Dim adapter1 As New OleDbDataAdapter(sql1, strcon)
    Dim cmd As New OleDbCommand(sql1, strcon)
    strcon.Open()
    Dim myreader As OleDbDataReader = cmd.ExecuteReader
    myreader.Read()

strcon.Close()

答案 1 :(得分:0)

Dim ds as New Dataset("product_info")
ds.Tables.Add(dt)

在将数据填充到datatable dt

后添加这些行