VB.NET How to Filter DataGridView Using TextBox and MySQL Database - BTS Au Maroc

vendredi 16 février 2018

VB.NET How to Filter DataGridView Using TextBox and MySQL Database





Source Code ModuleConnection.vb


Imports System.Data.Odbc
Module ModuleConnection
' for this tutorial, we must have a database MySQL before
' then create a connection using ODBC drivers
' i have a database MySQL, you must create it before.


Public Connections As OdbcConnection ' declarations our connection
Sub openConnection()
Try
Connections = New OdbcConnection("DSN=java_db;MultipleActiveResultSets=True")
If Connections.State = ConnectionState.Closed Then
' open connection
Connections.Open()
End If
Catch ex As Exception
MsgBox("Error at " & ex.ToString)
End Try
End Sub
End Module




Source Code Form Filter Data:


Imports System.Data.Odbc
Public Class Form1
Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles TextBox1.TextChanged
openConnection() ' open our connection before
Dim da As OdbcDataAdapter ' declaration data adapter
Dim dt As DataTable ' declaration data table
da = New OdbcDataAdapter("SELECT * FROM biodata where nama like '" & TextBox1.Text & "%'", Connections)
dt = New DataTable
da.Fill(dt)
DataGridView1.DataSource = dt
Connections.Close() ' close connections
da.Dispose() ' close data adapter
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
openConnection() ' open our connection
Dim da As OdbcDataAdapter
Dim ds As DataSet
da = New OdbcDataAdapter("SELECT * FROM biodata", Connections)
ds = New DataSet

da.Fill(ds, "biodata")
DataGridView1.DataSource = ds.Tables("biodata")
Connections.Close() 'close our connection
da.Dispose() ' close data adapter
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Me.Close()
End Sub
End Class







Video Tutorial  Filter DataGridView Using TextBox:








Aucun commentaire:

Enregistrer un commentaire

Pages