简体   繁体   English

在DataGrid VB.Net中搜索(筛选器)

[英]Search (Filter) in DataGrid VB.Net

I have this program that lets the user/admin search a certain student through a DataGrid. 我有这个程序,允许用户/管理员通过DataGrid搜索某个学生。 To search a student, they need to select through a ComboBox (which has Year Level, ID Number, Last Name, and Program Name respectively) and type in a corresponding search keyword on the text box. 要搜索学生,他们需要通过组合框(分别具有年级,ID号,姓氏和程序名)进行选择,然后在文本框中键入相应的搜索关键字。 The DataGrid will be filtered according to the selected item in the ComboBox and the keyword, and only a few records will then be shown in the datagrid. 将根据ComboBox中的选定项目和关键字对DataGrid进行过滤,然后只有少量记录将显示在datagrid中。

Here is the code for the search: 这是搜索代码:

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

Although the declarations for BindingSources and the like are correct, the DataGrid does not filter according to the condition set by the ComboBox and the value in the keyword text box. 尽管BindingSources等的声明是正确的,但DataGrid不会根据ComboBox设置的条件和关键字文本框中的值进行过滤。 It used to work before I added other blocks of codes regarding viewing of these data in another form. 在添加其他有关以另一种形式查看这些数据的代码块之前,它曾经可以正常工作。 When I removed the block of code I added in, it still doesn't work. 当我删除添加的代码块时,它仍然不起作用。 Does anyone know what might be wrong? 有谁知道可能出什么问题了? I think I need to add something but I'm actually not that fluent in VB.Net. 我想我需要添加一些内容,但实际上我不太熟练VB.Net。 Thanks in advance! 提前致谢!

EDIT: This is the code I have for the frmSearch_Load event. 编辑:这是我为frmSearch_Load事件的代码。 This refreshes the DataGrid with the accurate values and records from the MS Access database file. 这将使用准确的值刷新数据网格,并从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()

Here is the code to populate the datagridview: 这是填充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

A bit more code would be helpful, specially if u have data bindings.. Anyway this might work: 多一点的代码会有所帮助,特别是如果您有数据绑定。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM