ผมเขียนโค้ดแบบนี้ครับเวลาตอนค้นหาไม่แสดงข้อมูลที่ต้องการ โดยหลังจากทำการโหลดข้อมูลมาแสดงในตาราง หลังจากการกดปุ่มค้นหาข้อมูลในตารางก็จะหายไปหมดครับ ตอนนี้ติดตรงนี้มานานแล้วเป็นมือใหม่ไม่ได้จบทางด้านคอมมาแต่สนใจงานด้านนี้ครับ วานผู้รู้ช่วยหน่อยครับ ขอบคุณครับ
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Public connstring As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Chainarong.k\Desktop\Database app\PrimerDatabase2015New.mdb;persist security info = false"
Public con As New OleDbConnection
Dim com As OleDbCommand
Dim oledbda As OleDbDataAdapter
Dim ds As DataSet
Dim dt As DataTable
Dim str As String
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Try
str = "select * from tbl_primerbank where Agent ='" & TextBox1.Text & "'"
com = New OleDbCommand(str, con)
oledbda = New OleDbDataAdapter(com)
ds = New DataSet
oledbda.Fill(ds, "tbl_primerbank")
con.Close()
DataGridView1.DataSource = ds
DataGridView1.DataMember = "tbl_primerbank"
Catch ex As Exception
MsgBox(ex.Message)
End Try
TextBox1.Clear()
DataGridView1.Visible = True
End Sub
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
Private Sub showdata()
Dim oledbc As New OleDbCommand
Dim oledbdr As OleDbDataReader
Dim c As Integer
c = 0
With oledbc
.Connection = con
.CommandText = "select*from tbl_primerbank"
End With
oledbdr = oledbc.ExecuteReader
DataGridView1.Rows.Clear()
If oledbdr.HasRows Then
While oledbdr.Read
DataGridView1.Rows.Add()
DataGridView1.Item(0, c).Value = oledbdr.Item(0)
DataGridView1.Item(1, c).Value = oledbdr.Item(1)
DataGridView1.Item(2, c).Value = oledbdr.Item(2)
DataGridView1.Item(3, c).Value = oledbdr.Item(3)
DataGridView1.Item(4, c).Value = oledbdr.Item(4)
DataGridView1.Item(5, c).Value = oledbdr.Item(5)
DataGridView1.Item(6, c).Value = oledbdr.Item(6)
DataGridView1.Item(7, c).Value = oledbdr.Item(7)
DataGridView1.Item(8, c).Value = oledbdr.Item(8)
DataGridView1.Item(9, c).Value = oledbdr.Item(9)
DataGridView1.Item(10, c).Value = oledbdr.Item(10)
DataGridView1.Item(11, c).Value = oledbdr.Item(11)
DataGridView1.Item(12, c).Value = oledbdr.Item(12)
DataGridView1.Item(13, c).Value = oledbdr.Item(13)
DataGridView1.Item(14, c).Value = oledbdr.Item(14)
DataGridView1.Item(15, c).Value = oledbdr.Item(15)
DataGridView1.Item(16, c).Value = oledbdr.Item(16)
DataGridView1.Item(17, c).Value = oledbdr.Item(17)
DataGridView1.Item(18, c).Value = oledbdr.Item(18)
DataGridView1.Item(19, c).Value = oledbdr.Item(19)
DataGridView1.Item(20, c).Value = oledbdr.Item(20)
DataGridView1.Item(21, c).Value = oledbdr.Item(21)
DataGridView1.Item(22, c).Value = oledbdr.Item(22)
DataGridView1.Item(23, c).Value = oledbdr.Item(23)
DataGridView1.Item(24, c).Value = oledbdr.Item(24)
DataGridView1.Item(25, c).Value = oledbdr.Item(25)
DataGridView1.Item(26, c).Value = oledbdr.Item(26)
DataGridView1.Item(27, c).Value = oledbdr.Item(27)
c = c + 1
End While
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If con.State = ConnectionState.Closed Then
con.ConnectionString = connstring
con.Open()
End If
Call showdata()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call showdata()
End Sub
End Class
มีปัญา vb2010 กับการทำปุ่ม search button ให้แสดงใน datagridview ให้แสดงตอนค้นหาครับวานผู้รู้ช่วยแนะนำด้วยครับ
Imports System.Data
Imports System.Data.OleDb
Public Class Form1
Public connstring As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\Chainarong.k\Desktop\Database app\PrimerDatabase2015New.mdb;persist security info = false"
Public con As New OleDbConnection
Dim com As OleDbCommand
Dim oledbda As OleDbDataAdapter
Dim ds As DataSet
Dim dt As DataTable
Dim str As String
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
Try
str = "select * from tbl_primerbank where Agent ='" & TextBox1.Text & "'"
com = New OleDbCommand(str, con)
oledbda = New OleDbDataAdapter(com)
ds = New DataSet
oledbda.Fill(ds, "tbl_primerbank")
con.Close()
DataGridView1.DataSource = ds
DataGridView1.DataMember = "tbl_primerbank"
Catch ex As Exception
MsgBox(ex.Message)
End Try
TextBox1.Clear()
DataGridView1.Visible = True
End Sub
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
Private Sub showdata()
Dim oledbc As New OleDbCommand
Dim oledbdr As OleDbDataReader
Dim c As Integer
c = 0
With oledbc
.Connection = con
.CommandText = "select*from tbl_primerbank"
End With
oledbdr = oledbc.ExecuteReader
DataGridView1.Rows.Clear()
If oledbdr.HasRows Then
While oledbdr.Read
DataGridView1.Rows.Add()
DataGridView1.Item(0, c).Value = oledbdr.Item(0)
DataGridView1.Item(1, c).Value = oledbdr.Item(1)
DataGridView1.Item(2, c).Value = oledbdr.Item(2)
DataGridView1.Item(3, c).Value = oledbdr.Item(3)
DataGridView1.Item(4, c).Value = oledbdr.Item(4)
DataGridView1.Item(5, c).Value = oledbdr.Item(5)
DataGridView1.Item(6, c).Value = oledbdr.Item(6)
DataGridView1.Item(7, c).Value = oledbdr.Item(7)
DataGridView1.Item(8, c).Value = oledbdr.Item(8)
DataGridView1.Item(9, c).Value = oledbdr.Item(9)
DataGridView1.Item(10, c).Value = oledbdr.Item(10)
DataGridView1.Item(11, c).Value = oledbdr.Item(11)
DataGridView1.Item(12, c).Value = oledbdr.Item(12)
DataGridView1.Item(13, c).Value = oledbdr.Item(13)
DataGridView1.Item(14, c).Value = oledbdr.Item(14)
DataGridView1.Item(15, c).Value = oledbdr.Item(15)
DataGridView1.Item(16, c).Value = oledbdr.Item(16)
DataGridView1.Item(17, c).Value = oledbdr.Item(17)
DataGridView1.Item(18, c).Value = oledbdr.Item(18)
DataGridView1.Item(19, c).Value = oledbdr.Item(19)
DataGridView1.Item(20, c).Value = oledbdr.Item(20)
DataGridView1.Item(21, c).Value = oledbdr.Item(21)
DataGridView1.Item(22, c).Value = oledbdr.Item(22)
DataGridView1.Item(23, c).Value = oledbdr.Item(23)
DataGridView1.Item(24, c).Value = oledbdr.Item(24)
DataGridView1.Item(25, c).Value = oledbdr.Item(25)
DataGridView1.Item(26, c).Value = oledbdr.Item(26)
DataGridView1.Item(27, c).Value = oledbdr.Item(27)
c = c + 1
End While
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If con.State = ConnectionState.Closed Then
con.ConnectionString = connstring
con.Open()
End If
Call showdata()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Call showdata()
End Sub
End Class