Searching of Data from datagrid.

Imports is used to import namespace in your current module.

Imports System.Data.OleDb

Public Class Form1

con is the object of oledbconnection. It is used to connect with the backend.

Dim con As New OleDbConnection

str is ov=bject of StringBuilder class used to create the statement using multiple strings

Dim str As New System.Text.StringBuilder

cmd is object of oledbCommand used to create the command that will be exceuted by the adapter or reader.

Dim cmd As New OleDbCommand

da is object of oledbDataAdapter used to create the exceute the statement. and return datatable. DataAdapter use disconnected connection

Dim da As OleDbDataAdapter

dt is object of contains multiple rows and columns. It is used to store the data in frontend.

Dim dt As New DataTable

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

.append() is the method of StringBuilder class that will append strings one after another in the main string

str.Append("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=")
str.Append(";Persist Security Info=False")

ToString() is the method of StringBuilder class that will concatenate all the strings and create the statement.

con.ConnectionString = str.ToString

cmd.CommandType = CommandType.Text
cmd.CommandText = "select * from studentDetails "
cmd.Connection = con

da will execute the command

da = New OleDbDataAdapter(cmd)

using fill method of dataadapter data will be filled in the datatable.


datasource is the property of grid that will bind the datatable with grid.

DataGridView1.DataSource = dt

End Sub

whenever user change the data in the textbox TextChanged event will be called

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

Dim s As New System.Text.StringBuilder
Dim tabl As New DataTable

cm is the object of oledbcommand that will accept 2 argument first is the sql query and second will be the connection

"select * from studentDetails where sname like '" & Trim(TextBox1.Text) & "%'"

In the above sql statement like is keyword that is used with the string manipulation.

Trim is the method used by .net that will remove all the spaces from the string.
% indicates many character.

eg:if i type p then it will return p% means string starts with p and have one or many characters.

Dim cm As New OleDbCommand("select * from studentDetails where sname like '" & Trim(TextBox1.Text) & "%'", con)

Dim d As New OleDbDataAdapter(cm)
DataGridView1.DataSource = tabl

End Sub

End Class


  • Searching data from datagrid (30965-2818-SearchDatagrid.rar)
  • Comments

    No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: