使用click事件填充datagridview

时间:2014-05-07 05:27:01

标签: sql vb.net visual-studio-2012 datagridview ms-access-2007

我正在尝试在点击事件(如搜索按钮)上加载数据网格视图。在此过程中我没有收到任何错误,但我的datagridview没有填充。我知道打开我的数据库连接的方法是有效的,并且我的SQL语句也可以正常工作,因为我已经在Access中对它进行了测试,我相信如果它是SQL语句,它会爆炸。我认为这是在事件声明中,但我对vb很新。我的代码如下:

Private Sub btnQuickSearch_Click(sender As Object, e As EventArgs) Handles btnQuickSearch.Click
    dgvQuickSearchEmployee.Visible = True
    qSearchEmployee()

End Sub

Private Sub qSearchEmployee()
    conOpen()
    Dim qSearchString As String
    qSearchString = tbQuickSearch.Text
    qSearchString = qSearchString.ToUpper()



    sqlStr = "SELECT employeeID, firstName, lastName, department FROM [employee] WHERE (employeeID LIKE '*" & qSearchString & "*' OR firstName LIKE '*" & qSearchString & "*' OR lastName LIKE '*" & qSearchString & "*' OR department LIKE '*" & qSearchString & "*') AND active = TRUE"


    Dim ds As DataSet = New DataSet
    Dim dt As DataTable = New DataTable
    ds.Tables.Add(dt)
    Dim daEmployee As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(sqlStr, con)
    daEmployee.Fill(dt)
    dgvQuickSearchEmployee.DataSource = dt.DefaultView
    dgvQuickSearchEmployee.Refresh()

End Sub

非常感谢任何帮助。

**更新! 这是我的代码现在看起来的方式(没有太大的不同)。它现在正在填充datagridview,但无论我对搜索做什么,它都不会返回任何结果。

Private Sub btnQuickSearch_Click(sender As Object, e As EventArgs) Handles btnQuickSearch.Click
    dgvQuickSearchEmployee.Visible = True
    qSearchEmployee()

End Sub

Private Sub qSearchEmployee()
    conOpen()
    Dim qSearchString As String
    qSearchString = tbQuickSearch.Text
    qSearchString = qSearchString.ToUpper()

    sqlStr = "SELECT employeeID, firstName, lastName, department FROM [employee] WHERE (employeeID LIKE '*" & qSearchString & "*' OR firstName LIKE '*" & qSearchString & "*' OR lastName LIKE '*" & qSearchString & "*' OR department LIKE '*" & qSearchString & "*') AND active = TRUE"

    Dim ds As DataSet = New DataSet
    Dim dt As DataTable = New DataTable
    ds.Tables.Add(dt)
    Dim da As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(sqlStr, con)
    da.Fill(dt)
    dgvQuickSearchEmployee.DataSource = dt.DefaultView
    dgvQuickSearchEmployee.Refresh()

End Sub

2 个答案:

答案 0 :(得分:0)

试试此代码

'Dim ds As DataSet = New DataSet    'Not required or Not inused
Dim dt As DataTable = New DataTable
'ds.Tables.Add(dt)          'Not required or Not inused
Dim daEmployee As OleDb.OleDbDataAdapter = New OleDb.OleDbDataAdapter(sqlStr, con)
daEmployee.Fill(dt)
dgvQuickSearchEmployee.DataSource = dt
dgvQuickSearchEmployee.Refresh()

答案 1 :(得分:0)

而不是dgvQuickSearchEmployee.Refresh()尝试dgvQuickSearchEmployee.Databind()