Vb.net 如何将数据库中的每一行和列数据显示到Datagridview

Vb.net 如何将数据库中的每一行和列数据显示到Datagridview,vb.net,sqlcommand,Vb.net,Sqlcommand,我面临一个问题,我从数据库中选择要在网格中显示的行:结果,网格上只显示一行,其余的不显示 这是我的密码: 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.Exe

我面临一个问题,我从数据库中选择要在网格中显示的行:结果,网格上只显示一行,其余的不显示

这是我的密码:

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()

问题似乎出在您的SQL查询中

where Req_No=" & TextBox1.Text & "
上述查询的这一部分将把返回的行限制为仅一行

我建议将上述问题替换为:

where Req_No LIKE %" & TextBox1.Text & "
这将返回与输入文本相似的带有“Req_No”的行,而不是与输入文本相同的带有“Req_No”的行


如果您没有实现搜索功能。完全删除Where子句

确保selectquery返回所需的输出(多行),并且您可以使用下面提到的代码读取
SqlDataReader中的所有数据

 If dr.HasRows Then
     While dr.Read
          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")
    End While
 End If
在Read()博士和我