gpt4 book ai didi

mysql - 在 DataGrid VB.Net 中搜索(过滤器)

转载 作者:行者123 更新时间:2023-11-29 21:16:32 26 4
gpt4 key购买 nike

我有这个程序,可以让用户/管理员通过数据网格搜索某个学生。要搜索学生,他们需要通过组合框(分别包含年级、ID 号、姓氏和项目名称)进行选择,然后在文本框中输入相应的搜索关键字。 DataGrid 将根据 ComboBox 中的所选项目和关键字进行过滤,然后仅在 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 数据库文件中的准确值和记录刷新 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

最佳答案

多一点代码会很有帮助,特别是如果你有数据绑定(bind)的话。无论如何,这可能有效:

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

关于mysql - 在 DataGrid VB.Net 中搜索(过滤器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35891965/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com