简体   繁体   中英

Filtering a datagridview after importing from excel via a text box in vb.net

I have a datagridview which i import an excel file in to it. My excel columns are name,id,sex,grade,seat no,permanent address,temporary address and phone number.What i want is to filter all the columns at the same time (multi column filter) in the datagridview via a single textbox. ie when i type a single word in the text box i want it to filter the columns at the same time.

Here is the code i got to import the excel to the datagridview....

Dim cn As New OleDbConnection
Dim cm As New OleDbCommand
Dim da As OleDbDataAdapter
Dim dt As New DataTable
Private Excel03ConString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Extended Properties='Excel 8.0;HDR={1}'"
Private Excel07ConString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR={1}'"
Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click
OpenFileDialog1.Title = "Open Internship Excel Files"

OpenFileDialog1.ShowDialog()
End Sub
Private Sub OpenFileDialog1_FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
        Dim filePath As String = OpenFileDialog1.FileName
        Dim extension As String =
        Path.GetExtension(filePath)
        Dim header As String = If(rbHeaderYes.Checked, "YES", "NO")
        Dim conStr As String, sheetName As String
        conStr = String.Empty
        Select Case extension
            Case ".xls"
                'Excel 97-03
                conStr = String.Format(Excel03ConString, filePath, header)
                Exit Select
            Case ".xlsx"
                'Excel 07
                conStr = String.Format(Excel07ConString, filePath, header)
                Exit Select
        End Select
        'Get the name of the First Sheet.
        Using con As New OleDbConnection(conStr)
            Using cmd As New OleDbCommand()
                cmd.Connection = con
                con.Open()
                Dim dtExcelSchema As DataTable = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
                sheetName = dtExcelSchema.Rows(0)("TABLE_NAME").ToString()
                con.Close()
            End Using
        End Using
        'Read Data from the First Sheet.
        Using con As New OleDbConnection(conStr)
            Using cmd As New OleDbCommand()
                Using oda As New OleDbDataAdapter()
                    Dim dt As New DataTable()
                    cmd.CommandText = (Convert.ToString("SELECT * From [") & sheetName) + "]"
                    cmd.Connection = con
                    con.Open()
                    oda.SelectCommand = cmd
                    oda.Fill(dt)
                    con.Close()
                    'Populate DataGridView.
                    DataGridView1.DataSource = dt
                End Using
            End Using
        End Using
    End Sub

You can apply a row filter like this: (From Filtering DataGridView without changing datasource )

CType(DataGridView1.DataSource,DataTable).DefaultView.RowFilter = MyQueryCriteria

For your criteria you'll want something like:

Dim MyQueryCriteria = String.Format("ID like '%{0}%' OR NAME like '%{0}%' OR ...", MyTextBox.Text)

You may run into issues with types there but the documentation has information on conversions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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