【问题标题】:Reload a Datagrid View returns Nothing重新加载 Datagrid 视图返回 Nothing
【发布时间】:2017-04-04 12:12:50
【问题描述】:

我有一个函数可以将数据从 Microsoft Access DB 加载到 DGV 中,如下面的代码所示:

Public Sub LoaddgvCombined()  
    Try
        '//Clear DataGrid
        ds.Clear()
        dgvSales.DataSource = Nothing
        'dgvSales.Refresh()
        dgvSales.DataSource = ds

        '//Establish Connection to DB
        con.ConnectionString = dbProvider & dbSource
        tables = ds.Tables
        con.Open()

        sql = "SELECT * FROM [tblINV_SalesRecord] WHERE CDate(adddate &  ' ' &addtime) < CDate('" & selectfrm & "')) ORDER BY temID ASC"

        da = New OleDb.OleDbDataAdapter(sql, con)
        da.Fill(ds, "tblINV_SalesRecord")
        con.Close()

        Dim view As New DataView(tables(0))
        source1.DataSource = view
        dgvSales.DataSource = view
        dgvSales.AllowUserToAddRows = False

        If dgvSales.RowCount < 1 Then
            MessageBox.Show("No Sales Recorded Found From " & selectfrm)
        End If

    Catch ex As Exception
        MessageBox.Show(ex.Message & " - " & ex.Source)
        MessageBox.Show("An Error Occured While Loading Sales Record ")
    End Try 
End Sub

第一次运行良好,但当我第二次调用该函数时,它清空了 DGV,但这次它没有加载任何数据。

请任何人指出我做错了什么。

【问题讨论】:

    标签: database vb.net datagrid


    【解决方案1】:

    我在这里注意到的是你正在传递给tables之前你得到了你的查询结果。

    在填写 ds 后尝试传递 tables 的值。

    像这样:

    da = New OleDb.OleDbDataAdapter(sql, con)
    da.Fill(ds, "tblINV_SalesRecord")
    con.Close()
    tables = ds.Tables '<=== transfer it here
    

    它显示为空,因为在您传递给tables 时,您的ds 变量尚未从您的查询中填充。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-29
      • 2019-03-18
      • 2023-04-07
      相关资源
      最近更新 更多