By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,373 Members | 1,792 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,373 IT Pros & Developers. It's quick & easy.

How do I filter datagridview exported from excel in VB.NET

P: 2
I have a datagridview which i import an excel excel columns are name,id,sex,grade,seat no .what i want is to filter all the datagridview via a textbox. i.e. when i type a single word in the text box i want it to filter the rows of the DGV at the same time.

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

Expand|Select|Wrap|Line Numbers
  1. Private Sub OpenFileDialog1_FileOk(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
  2.         Dim filePath As String = OpenFileDialog1.FileName
  3.         Dim extension As String =
  4.         Path.GetExtension(filePath)
  5.         Dim header As String = If(rbHeaderYes.Checked, "YES", "NO")
  6.         Dim conStr As String, sheetName As String
  7.         conStr = String.Empty
  8.         Select Case extension
  9.             Case ".xls"
  10.                 'Excel 97-03
  11.                 conStr = String.Format(Excel03ConString, filePath, header)
  12.                 Exit Select
  13.             Case ".xlsx"
  14.                 'Excel 07
  15.                 conStr = String.Format(Excel07ConString, filePath, header)
  16.                 Exit Select
  17.         End Select
  18.         'Get the name of the First Sheet.
  19.         Using con As New OleDbConnection(conStr)
  20.             Using cmd As New OleDbCommand()
  21.                 cmd.Connection = con
  22.                 con.Open()
  23.                 Dim dtExcelSchema As DataTable = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, Nothing)
  24.                 sheetName = dtExcelSchema.Rows(0)("TABLE_NAME").ToString()
  25.                 con.Close()
  26.             End Using
  27.         End Using
  28.         'Read Data from the First Sheet.
  29.         Using con As New OleDbConnection(conStr)
  30.             Using cmd As New OleDbCommand()
  31.                 Using oda As New OleDbDataAdapter()
  32.                     Dim dt As New DataTable()
  33.                     cmd.CommandText = (Convert.ToString("SELECT * From [") & sheetName) + "]"
  34.                     cmd.Connection = con
  35.                     con.Open()
  36.                     oda.SelectCommand = cmd
  37.                     oda.Fill(dt)
  38.                     con.Close()
  39.                     'Populate DataGridView.
  40.                     DataGridView1.DataSource = dt
  41.                 End Using
  42.             End Using
  43.         End Using
  44.     End Sub
Aug 13 '16 #1
Share this Question
Share on Google+
2 Replies

P: 293
As per my understanding, you can filter the data of the DGV by identifying the Indicator that identifies the row . Which column from the imported data identifies the row. You need to add extra column in the grid for that.

I have suggestions on how you can code this but prior to that could you please put your code here which you tried to filter the data.
Aug 13 '16 #2

P: 2
I haven't done any coding yet. i need you to suggest the code for me.
Aug 15 '16 #3

Post your reply

Sign in to post your reply or Sign up for a free account.