我从朋友那里修改了一些代码
但它只显示第一条记录,我将如何操纵循环遍历所有记录
任何能够在代码中给出正确答案的人都将获得赏金
注意:DisplayOfficeEquipmentList()
是一个子数据库,它将数据库中的数据显示到文本框和组合框
Public Sub DisplayOfficeEquipmentList()
Dim sqlconn As New SqlClient.SqlConnection
sqlconn.ConnectionString = "server = SKPI-APPS1;" & _
"Database = EOEMS;integrated security=true"
Dim dt As New DataTable
sqlconn.Open()
Dim da As New SqlDataAdapter("SELECT * FROM tblOfficeEquipmentProfile", sqlconn)
da.Fill(dt)
cmbCategory.Text = dt.Rows(0)("OE_Category").ToString()
cmbSubCategory.Text = dt.Rows(0)("OE_SubCategory").ToString()
txtOEID.Text = dt.Rows(0)("OE_ID").ToString()
txtName.Text = dt.Rows(0)("OE_Name").ToString()
txtUser.Text = dt.Rows(0)("OE_User").ToString()
cmbBrand.Text = dt.Rows(0)("OE_Brand").ToString()
cmbModel.Text = dt.Rows(0)("OE_Model").ToString()
txtSpecs.Text = dt.Rows(0)("OE_Specs").ToString()
txtSerialNo.Text = dt.Rows(0)("OE_SerialNo").ToString()
txtPropertyNo.Text = dt.Rows(0)("OE_PropertyNo").ToString()
txtMacAddress.Text = dt.Rows(0)("OE_MacAddress").ToString()
txtStaticIP.Text = dt.Rows(0)("OE_Static_IP").ToString()
txtVendor.Text = dt.Rows(0)("OE_Vendor").ToString()
dtpPurchaseDate.Text = dt.Rows(0)("OE_PurchaseDate").ToString()
txtWarrantyStatus.Text = dt.Rows(0)("OE_WarrantyStatus").ToString()
txtWarrantyInclusiveYear.Text = dt.Rows(0)("OE_WarrantyInclusiveYear").ToString()
txtStatus.Text = dt.Rows(0)("OE_Status").ToString()
cmbDeptCode.Text = dt.Rows(0)("OE_Dept_Code").ToString()
cmbLocationCode.Text = dt.Rows(0)("OE_Location_Code").ToString()
txtRemarks.Text = dt.Rows(0)("OE_Remarks").ToString()
sqlconn.Close()
End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
End Sub
答案 0 :(得分:2)
你必须循环这些行,你在索引0(只有一行)获得行的值。
使用foreach:
foreach (System.Data.DataRow row in dt.Rows)
{
//Get values of row
}
编辑:在vb.net中,它会是这样的:
For Each filarow As DataRow In dt.Rows
Dim OE_ID As String = filarow("OE_ID").ToString
Dim txtName As String = filarow("OE_NAME").ToString
Next
顺便说一下,您似乎正在填充文本框,因此值将在下一个循环中更改。也许你应该使用像ListBox
这样的另一个控件答案 1 :(得分:1)
您需要将检索数据的逻辑与显示该数据的逻辑分开。 首先添加一个加载数据表的方法
Private Function LoadData() as DataTable
Using sqlconn = New SqlClient.SqlConnection("server = SKPI-APPS1;" & _
"Database = EOEMS;integrated security=true")
Dim dt As New DataTable
sqlconn.Open()
Dim da As New SqlDataAdapter("SELECT * FROM tblOfficeEquipmentProfile", sqlconn)
da.Fill(dt)
return dt
End Using
End Function
然后在按钮中单击传递数据表和rownumber以显示到DisplayOfficeEquipmentList
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
if currentRow + 1 >= dt.Rows.Count Then
Return
End if
currentRow = currentRow + 1
DisplayOfficeEquipmentList(dt, currentRow)
End Sub
Private Sub btnPrevious_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
if currentRow - 1 < 0 Then
Return
End if
currentRow = currentRow - 1
DisplayOfficeEquipmentList(dt, currentRow)
End Sub
并在DisplayOfficeEquipmentList中引用按钮传递的行单击
Public Sub DisplayOfficeEquipmentList(ByRef dt as DataTable, ByVal rowNum as INteger)
Dim row as DataRow
row = dt.Rows(rowNum)
cmbCategory.Text = row("OE_Category").ToString()
cmbSubCategory.Text = row("OE_SubCategory").ToString()
....
End Sub
要使其正常工作,您需要在显示表单时调用LoadData(加载事件?),并将currentRow设置为表单全局级变量