我面臨的一個問題是,我從數據庫中選擇行以顯示在網格中:結果只有一行顯示在網格上,其餘沒有顯示。如何顯示數據庫中的每行和列數據到Datagridview
這裏是我的代碼:
conn()
Dim qry As String = "select SN,Product_ID,Product_Description,Quantity,Supplier_Name from materialreq where Req_No=" & TextBox1.Text & ""
cmd = New SqlCommand(qry, cn)
dr = cmd.ExecuteReader()
Dim i As Integer = 0
While dr.Read() And i = DataGridView1.Rows.Count - 1
DataGridView1.Rows(i).Cells("Column1").Value = dr("SN")
DataGridView1.Rows(i).Cells("Column2").Value = dr("Product_ID")
DataGridView1.Rows(i).Cells("Column3").Value = dr("Product_Description")
DataGridView1.Rows(i).Cells("Column4").Value = dr("Quantity")
DataGridView1.Rows(i).Cells("Column5").Value = dr("Supplier_Name")
'DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells("Column3").Value = dr("Product_Description").ToString()
'DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells("Column4").Value = dr("Quantity").ToString()
'DataGridView1.Rows(DataGridView1.Rows.Count - 1).Cells("Column5").Value = dr("Supplier_Name").ToString()
i = i + 1
End While
cn.Close()
嘗試'讀取所有數據雖然dr.Read()和我<= DataGridView1.Rows.Count - 1' – DeanOC