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
谢谢!数据来自Access文件,我使用SQL连接到程序。我试过代码,并且出现一个错误,提示“即使Access文件和数据网格中的列名与条件相同,也找不到(在此处插入列名称)。” –
您还没有回答?要知道我是否应该走得更远:) – noidea
还没有。我试过这段代码,但是错误提示找不到表名:( –