Dim strSql As String = "SELECT EmpCode,EmpID,EmpName FROM dbo.Employee"
Dim dtb As New DataTable
Using cnn As New SqlConnection(connectionString)
cnn.Open()
Using dad As New SqlDataAdapter(strSql, cnn)
dad.Fill(dtb)
End Using
cnn.Close()
End Using
Private Sub SetDGV1()
Dim sqlDcode As String
Dim dtDcode As New DataTable
Dim sb As New StringBuilder
Dim Comm As New SqlCommand
If TextBox4.Text.Length <> 0 Then
sb.Remove(0, sb.Length)
sb.Append("SELECT * FROM tbl_solution_w ")
'
sb.Append("where id_W=@ID ORDER BY date DESC ")
With Comm
.Parameters.Clear()
.Parameters.AddWithValue("@ID", SqlDbType.NVarChar).Value = TextBox4.Text
End With
sqlDcode = sb.ToString()
dtDcode = ZSQLDataReader("Drugstore", sqlDcode, Comm)
DataGridView2.DataSource = dtDcode
If dtDcode.Rows.Count <> 0 Then
DataGridView1.Rows.Add()
Dim i As Integer
i = DataGridView1.Rows.Count - 1
Label25.Text = dtDcode.Rows(0)(1).ToString
Label26.Text = dtDcode.Rows(0)(2).ToString
TextBox4.Text = dtDcode.Rows(0)(3).ToString
cmb_dept.Text = dtDcode.Rows(0)(7).ToString
DateTimePicker1.Value = dtDcode.Rows(0)(8).ToString
Label20.Text = dtDcode.Rows(0)(15).ToString
Label22.Text = dtDcode.Rows(0)(16).ToString
cmb_psell.Text = dtDcode.Rows(0)(17).ToString
DataGridView1.Item(0, i).Value = dtDcode.Rows(0)(4).ToString
DataGridView1.Item(1, i).Value = dtDcode.Rows(0)(5).ToString
DataGridView1.Item(2, i).Value = dtDcode.Rows(0)(6).ToString
DataGridView1.Item(3, i).Value = dtDcode.Rows(0)(9).ToString
DataGridView1.Item(4, i).Value = dtDcode.Rows(0)(10).ToString
DataGridView1.Item(5, i).Value = dtDcode.Rows(0)(11).ToString
DataGridView1.Item(6, i).Value = dtDcode.Rows(0)(12).ToString
DataGridView1.Item(7, i).Value = dtDcode.Rows(0)(13).ToString
DataGridView1.Item(8, i).Value = dtDcode.Rows(0)(14).ToString
End If
End If
searchdeptID()
End Sub
บ้าน ๆ ครับ แอด row เข้าไปแล้ว แอดข้อมูลลงไปตามคอร์รั่ม ใช้ While ไล่อ่านข้อมูลที่มีทั้งหมดในฐานข้อมูล
Code (VB.NET)
cmd = New MySqlCommand("SELECT user_id,user_name,user_address,user_tel,user_email,user_usernamelogin,user_leval FROM users", ConnectionDB)
Call open_connection()
Dr = cmd.ExecuteReader
While Dr.Read()
Dim r As Integer
With Me.DataGridview1
r = .RowCount
.Rows.Add()
.Rows(r).Cells(0).Value = r + 1
.Rows(r).Cells(1).Value = Dr.Item("user_id")
.Rows(r).Cells(2).Value = Dr.Item("user_name")
.Rows(r).Cells(3).Value = Dr.Item("user_address")
.Rows(r).Cells(4).Value = Dr.Item("user_tel")
.Rows(r).Cells(5).Value = Dr.Item("user_email")
.Rows(r).Cells(6).Value = Dr.Item("user_usernamelogin")
End With
End While
Private Sub SetDGV1()
If DataGridView1.Rows.Count <> 0 Then
DataGridView1.Rows.Clear()
End If
Dim sqlDcode As String
Dim con As New SqlConnection
con = ZConnectionOnly("drugstore")
Dim dtDcode As New DataTable
Dim sb As New StringBuilder
Dim Comm As New SqlCommand
If TextBox4.Text.Length <> 0 Then
sb.Remove(0, sb.Length)
sb.Append("SELECT * FROM tbl_solution_w ")
sb.Append("where id_W=@ID ORDER BY date DESC ")
sqlDcode = sb.ToString()
With Comm
.Parameters.Clear()
.CommandText = sqlDcode
.Parameters.AddWithValue("@ID", SqlDbType.NVarChar).Value = TextBox4.Text
.Connection = con
End With
dr = Comm.ExecuteReader
While (dr.Read())
Dim r As Integer
With (Me.DataGridView1)
r = .RowCount
.Rows.Add()
.Rows(r).Cells(0).Value = dr.Item("drug_code_homec")
.Rows(r).Cells(1).Value = dr.Item("drug_code_invc")
.Rows(r).Cells(2).Value = dr.Item("drug_name")
.Rows(r).Cells(3).Value = dr.Item("sell")
.Rows(r).Cells(4).Value = dr.Item("sumsell")
.Rows(r).Cells(5).Value = dr.Item("unit")
.Rows(r).Cells(6).Value = dr.Item("price")
.Rows(r).Cells(7).Value = dr.Item("sumprice")
.Rows(r).Cells(8).Value = dr.Item("status")
Label25.Text = dr.Item("id").ToString
Label26.Text = dr.Item("hid").ToString
cmb_dept.Text = dr.Item("dept_name").ToString
DateTimePicker1.Value = dr.Item("date").ToString
' Label20.Text = dtDcode.Rows(0)(15).ToString
' Label22.Text = dtDcode.Rows(0)(16).ToString
cmb_psell.Text = dr.Item("psell").ToString
End With
End While
End If
searchdeptID()
End Sub