我有这个程序让用户/管理员通过DataGrid搜索某个学生。要搜索学生,他们需要通过ComboBox(分别具有年级,ID号,姓氏和程序名)进行选择,并在文本框中键入相应的搜索关键字。 DataGrid将根据ComboBox中的选定项和关键字进行过滤,然后只会在数据网格中显示几条记录。
以下是搜索的代码:
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数据库文件中的准确值和记录刷新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 :(得分:0)
更多代码会有所帮助,特别是如果你有数据绑定.. 无论如何,这可能有用:
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