2016-03-09 310 views
0

我有這個程序,讓用戶/管理員通過DataGrid搜索某個學生。要搜索學生,他們需要通過ComboBox(分別具有年份級別,ID號碼,姓氏和程序名稱)進行選擇,並在文本框中鍵入相應的搜索關鍵字。 DataGrid將根據ComboBox中的選定項和關鍵字進行過濾,並且只有少數記錄會顯示在數據網格中。在DataGrid中搜索(過濾器)VB.Net

下面是搜索代碼:

If cmbSearch.SelectedItem = "Year Level" Then 
      Dim records = From STUDENT In StudentDB2DataSet.STUDENT Where STUDENT.YEARLEVEL Like txtKeyword.Text & "*" Select STUDENT 
      STUDENTBindingSource.DataSource = records.AsDataView 
     ElseIf cmbSearch.SelectedItem = "ID Number" Then 
      Dim records = From STUDENT In StudentDB2DataSet.STUDENT Where STUDENT.IDNUMBER Like txtKeyword.Text & "*" Select STUDENT 
      STUDENTBindingSource.DataSource = records.AsDataView 
     ElseIf cmbSearch.SelectedItem = "Last Name" Then 
      Dim records = From STUDENT In StudentDB2DataSet.STUDENT Where STUDENT.LNAME Like txtKeyword.Text & "*" Select STUDENT 
      STUDENTBindingSource.DataSource = records.AsDataView 
     ElseIf cmbSearch.SelectedItem = "Program Code" Then 
      Dim records = From STUDENT In StudentDB2DataSet.STUDENT Where STUDENT.PROGCODE Like txtKeyword.Text & "*" Select STUDENT 
      STUDENTBindingSource.DataSource = records.AsDataView 
     End If 

雖然聲明的BindingSources等等是正確的,將DataGrid並根據組合框,並在關鍵字文本值設置的條件不過濾框。在添加有關以其他形式查看這些數據的其他代碼塊之前,它曾經工作過。當我刪除我添加的代碼塊時,它仍然不起作用。有誰知道什麼可能是錯的?我想我需要添加一些東西,但我實際上並不那麼流利地使用VB.Net。提前致謝!

編輯:這是我對frmSearch_Load事件的代碼。這將使用MS Access數據庫文件中的精確值和記錄刷新DataGrid。

Dim myConString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\StudentDirectory2\StudentDB2.accdb" 
    con.ConnectionString = myConString 
    con.Open() 
    da = New OleDbDataAdapter("select * from STUDENT", con) 
    ds = New DataSet 
    da.Fill(ds, "STUDENT") 
    DataGridView1.DataSource = ds.Tables("STUDENT") 
    DataGridView1.DataSource = dt 
    dtTableGrd = dt 

    con.Close() 

    DataGridView1.DataSource = ds.Tables("STUDENT") 
    con.Close() 

這裏是填充的DataGridView代碼:

Imports System.Data.OleDb 
Imports System.Data 
Imports System.Collections 
Imports System.IO 

Public Class frmSearch 

    Dim con1 As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\StudentDirectory2\StudentDB2.accdb") 
    Dim con As New OleDbConnection 
    Dim ds As New DataSet 
    Dim ds2 As New DataSet 
    Dim dt As New DataTable 
    Dim da As New OleDbDataAdapter 
    Dim da2 As New OleDbDataAdapter 

    Dim provider As String 
    Dim dataFile As String 
    Dim connString As String 
    Public myConnection As OleDbConnection = New OleDbConnection 
    Public dr As OleDbDataReader 
    Public dr2 As OleDbDataReader 

Private Sub frmSearch_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 
     'TODO: This line of code loads data into the 'StudentDB2DataSet.STUDENT' table. You can move, or remove it, as needed. 
     Me.STUDENTTableAdapter.Fill(Me.StudentDB2DataSet.STUDENT) 
     'TODO: This line of code loads data into the 'StudentDB2DataSet.STUDENT' table. You can move, or remove it, as needed. 
     Me.STUDENTTableAdapter.Fill(Me.StudentDB2DataSet.STUDENT) 
     'TODO: This line of code loads data into the 'StudentDB2DataSet.STUDENT' table. You can move, or remove it, as needed. 
     Me.STUDENTTableAdapter.Fill(Me.StudentDB2DataSet.STUDENT) 
     'TODO: This line of code loads data into the 'StudentDB2DataSet.STUDENT' table. You can move, or remove it, as needed. 
     Me.STUDENTTableAdapter.Fill(Me.StudentDB2DataSet.STUDENT) 
     'TODO: This line of code loads data into the 'UserDBDataSet.tblUser' table. You can move, or remove it, as needed. 




     Dim myConString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\StudentDirectory2\StudentDB2.accdb" 
     con.ConnectionString = myConString 
     con.Open() 
     da = New OleDbDataAdapter("select * from STUDENT", con) 
     ds = New DataSet 
     da.Fill(ds, "STUDENT") 
     DataGridView1.DataSource = ds.Tables("STUDENT") 
     DataGridView1.DataSource = dt 


     con.Close() 

     DataGridView1.DataSource = ds.Tables("STUDENT") 
     con.Close() 
     'DataGridView.ColumnCount = 6 
     'DataGridView.Columns(0).Name = "IDNumber" 
     'DataGridView.Columns(1).Name = "LastName" 
     'DataGridView.Columns(2).Name = "FirstName" 
     'DataGridView.Columns(3).Name = "MiddleInitial" 
     'DataGridView.Columns(4).Name = "YearLevel" 
     'DataGridView.Columns(5).Name = "Program" 

     'STUDENTBindingSource1.Sort = "IDNUMBER" 
     'TODO: This line of code loads data into the 'StudDirDBDataSet.tblStudent' table. You can move, or remove it, as needed. 

     Me.STUDENTTableAdapter.Fill(Me.StudentDB2DataSet.STUDENT) 
     DataGridView1.Refresh() 

     Dim user As String 

     user = frmLogin.cmbUsername.SelectedItem 

     If user = "User" Then 
      btnUpdate.Enabled = False 
      btnDelete.Enabled = False 
     End If 

     'Me.STUDENTTableAdapter1.Fill(Me.StudentDB2DataSet1.STUDENT) 
     'TODO: This line of code loads data into the 'StudDirDBDataSet.tblStudent' table. You can move, or remove it, as needed. 
     ' Me.STUDENTTableAdapter1.Fill(Me.StudentDB2DataSet1.STUDENT) 
     'TODO: This line of code loads data into the 'StudDirDBDataSet.tblStudent' table. You can move, or remove it, as needed. 
     ' Me.STUDENTTableAdapter1.Fill(Me.StudentDB2DataSet1.STUDENT) 
     'TODO: This line of code loads data into the 'StudDirDBDataSet.tblStudent' table. You can move, or remove it, as needed. 
     ' Me.STUDENTTableAdapter1.Fill(Me.StudentDB2DataSet1.STUDENT) 

End Sub 

回答

0

多一點的代碼將是有益的,特別是如果u有數據綁定.. 反正這可能工作:

Private dtTable As New DataTable 
Private dtTableGrd As DataTable 

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
DataGridView1.DataSource = dtTable 
    dtTableGrd = dtTable 
End Sub 

Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged 
    dtTableGrd.DefaultView.RowFilter = "ID Number Like '%" & TextBox1.Text & "%'" 
End Sub 
+0

謝謝!數據來自Access文件,我使用SQL連接到程序。我試過代碼,並且出現一個錯誤,提示「即使Access文件和數據網格中的列名與條件相同,也找不到(在此處插入列名稱)。」 –

+0

您還沒有回答?要知道我是否應該走得更遠:) – noidea

+0

還沒有。我試過這段代碼,但是錯誤提示找不到表名:( –