Public Class Department
Dim myConnection As SqlCeConnection
Dim dt As New DataTable
Dim Adapter As SqlCeDataAdapter
Dim ds As DataSet = New DataSet
Dim ActionFlag As String = ""
Dim IsFind As Boolean = False
Dim Conn As SqlCeConnection = New SqlCeConnection()
Private Sub Department_Load_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'ITDatabaseDataSet.Department' table. You can move, or remove it, as needed.
Me.DepartmentTableAdapter.Fill(Me.ITDatabaseDataSet.Department)
'BindDataGrid()
End Sub
Private Sub BindDataGrid()
'myConnection = New SqlCeConnection("Data Source =" + (System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "\Database1.sdf;"))
myConnection = New SqlCeConnection("Data Source=C:\Project\Project\ITDatabase.sdf;")
myConnection.Open()
Dim myCommand As SqlCeCommand = myConnection.CreateCommand()
myCommand.CommandText = "SELECT [Dep_Id], [Dep_Code], [Dep_Name], [Budget] FROM [Department]WHERE (Dep_Code LIKE '%" & txt_input.Text & "%') ORDER BY Dep_Id ASC"
myCommand.CommandType = CommandType.Text
Adapter = New SqlCeDataAdapter(myCommand)
Adapter.Fill(ds, "Department")
myConnection.Close()
dt = ds.Tables("Department")
Me.dgname.DataSource = dt
Me.dgname.Columns.Clear()
Dim column As DataGridViewTextBoxColumn
column = New DataGridViewTextBoxColumn()
column.DataPropertyName = "Dep_Id"
column.HeaderText = "Dep_Id"
column.Width = 50
Me.dgname.Columns.Add(column)
column = New DataGridViewTextBoxColumn()
column.DataPropertyName = "Dep_Code"
column.HeaderText = "Dep_Code"
column.Width = 100
Me.dgname.Columns.Add(column)
column = New DataGridViewTextBoxColumn()
column.DataPropertyName = "Dep_Name"
column.HeaderText = "Dep_Name"
column.Width = 150
Me.dgname.Columns.Add(column)
column = New DataGridViewTextBoxColumn()
column.DataPropertyName = "Budget"
column.HeaderText = "Budget"
column.Width = 150
Me.dgname.Columns.Add(column)
dt = Nothing
End Sub
Private Sub Btn_Select_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btn_Select.Click
BindDataGrid()
txt_input.Focus()
txt_input.SelectAll()
End Sub
If ds.Table("Department") <> Nothing Then '<-- เพิ่มบรรทัดนี้
ds.Table("Department").Rows.Clear(); '<-- เพิ่มบรรทัดนี้
End If '<-- เพิ่มบรรทัดนี้
Adapter = New SqlCeDataAdapter(myCommand)
Adapter.Fill(ds, "Department")
myConnection.Close()
dt = ds.Tables("Department")