Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim DBconn As New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\NewData.mdb")
If DBconn.State = ConnectionState.Closed Then
DBconn.Open()
End If
Dim name As String = Replace(TextBox1.Text, " ", "")
Dim sql As String = "select * from Customer where cusname like '%" & TextBox1.Text & "%' "
Dim cmd As New OleDb.OleDbCommand(sql, DBconn)
Dim dr As OleDb.OleDbDataReader
dr = cmd.ExecuteReader
DataGridView1.Columns.Clear()
While dr.Read
DataGridView1.Rows.Add(dr.Item("CusID"), dr.Item("CusName"), dr.Item("CusAddress"), dr.Item("Custel"))
End While
End Sub
Private Sub PopulateData()
'*** BindData From Database **'
Dim objConn As OleDb.OleDbConnection
Dim dtAdapter As OleDb.OleDbDataAdapter
Dim dt As New DataTable
Dim strConnString As String
strConnString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\NewData.mdb"
objConn = New OleDb.OleDbConnection(strConnString)
objConn.Open()
Dim strSQL As String
strSQL = "SELECT * FROM Customer WHERE 1=1"
If strKeyWord <> "" Then
strSQL = strSQL & " AND Like '%" & TextBox1.Text & "%' "
End If
dtAdapter = New OleDb.OleDbDataAdapter(strSQL, objConn)
dtAdapter.Fill(dt)
dtAdapter = Nothing
objConn.Close()
objConn = Nothing
Me.DataGridView1.DataSource = dt
'******************************'
Me.DataGridView1.Columns.Clear()
With DataGridView1.ColumnHeadersDefaultCellStyle
.BackColor = Color.Navy
.ForeColor = Color.White
.Font = New Font(songsDataGridView.Font, FontStyle.Bold)
End With
Dim ColumnTextBox As DataGridViewTextBoxColumn
'*** Column (Select) ***'
Dim ColumnCheckBox As New DataGridViewCheckBoxColumn()
ColumnCheckBox.Width = 30
ColumnCheckBox.DataPropertyName = "Select"
Me.DataGridView1.Columns.Add(ColumnCheckBox)
'*** Checkbox in Header ***'
Dim ckBox As New CheckBox()
Dim rect As Rectangle = DataGridView1.GetCellDisplayRectangle(0, -1, True)
ckBox.Size = New Size(18, 18)
rect.X = rect.Location.X + (rect.Width / 4)
ckBox.Location = rect.Location
AddHandler ckBox.CheckedChanged, New EventHandler(AddressOf TextBox1_TextChanged)
Me.DataGridView1.Controls.Add(ckBox)
DataGridView1.Columns(0).Frozen = False
'*** Column (CusID) ***'
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "CusID"
ColumnTextBox.HeaderText = "CusID"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.DataGridView1.Columns.Add(ColumnTextBox)
'*** Column (CusName) ***'
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "CusName"
ColumnTextBox.HeaderText = "CusName"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.DataGridView1.Columns.Add(ColumnTextBox)
'*** Column (CusAddress) ***'
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "CusAddress"
ColumnTextBox.HeaderText = "CusAddress"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.DataGridView1.Columns.Add(ColumnTextBox)
'*** Column (custel) ***'
ColumnTextBox = New DataGridViewTextBoxColumn()
ColumnTextBox.DataPropertyName = "custel"
ColumnTextBox.HeaderText = "custel"
ColumnTextBox.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
ColumnTextBox.Width = 100
Me.DataGridView1.Columns.Add(ColumnTextBox)
End Sub
Private Sub ckBox_CheckedChanged()
Dim i As Integer = 0
For j As Integer = 0 To Me.DataGridView1.RowCount - 1
Me.DataGridView1(0, j).Value = True
Next
End Sub
<STAThreadAttribute()> _
Public Shared Sub Main()
Application.EnableVisualStyles()
Application.Run(New Form1())
End Sub