我有这个程序,允许用户/管理员通过DataGrid搜索某个学生。要搜索学生,他们需要通过ComboBox(分别具有Year Level,ID号,Last Name和Program Name)进行选择,然后在文本框中键入相应的搜索关键字。将根据ComboBox中的选定项目和关键字对DataGrid进行过滤,然后只有少量记录将显示在datagrid中。
这是搜索代码:
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不会根据ComboBox设置的条件和关键字文本框中的值进行过滤。在添加其他有关以另一种形式查看这些数据的代码块之前,它曾经可以正常工作。当我删除添加的代码块时,它仍然不起作用。有谁知道可能出什么问题了?我想我需要添加一些内容,但实际上我不太熟练VB.Net。提前致谢!
编辑:这是我为frmSearch_Load事件的代码。这将使用准确的值刷新数据网格,并从MS Access数据库文件中记录数据。
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
最佳答案
多一点的代码会有所帮助,特别是如果您有数据绑定。
无论如何,这可能有效:
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