Searching of Data from datagrid.
Imports is used to import namespace in your current module.
Imports System.Data.OleDb
Public Class Form1con is the object of oledbconnection. It is used to connect with the backend.
Dim con As New OleDbConnectionstr is ov=bject of StringBuilder class used to create the statement using multiple strings
Dim str As New System.Text.StringBuildercmd is object of oledbCommand used to create the command that will be exceuted by the adapter or reader.
Dim cmd As New OleDbCommandda is object of oledbDataAdapter used to create the exceute the statement. and return datatable. DataAdapter use disconnected connection
Dim da As OleDbDataAdapterdt is object of DataTable.it 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(AppDomain.CurrentDomain.BaseDirectory)
str.Append("yogita.mdb")
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 = conda will execute the command
da = New OleDbDataAdapter(cmd)using fill method of dataadapter data will be filled in the datatable.
da.Fill(dt)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 DataTablecm 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)
d.Fill(tabl)
DataGridView1.DataSource = tabl
End Sub
End Class