两个解决方案可能很有趣:
- 将数据库结果直接链接到 DGV 非常便于更新,但如果需要移动行则较少
- 将查询中的数据作为行插入,您首先需要定义 DGV DataGridView2.Columns.Add()的列
1/
Using con As New SqlClient.SqlConnection("YOUR SQL CONNECTION STRING")
Dim sqlString As String = "SELECT * FROM Table WHERE intApprovedOvertime = 0 AND fkEmployee = pkEmployee"
con.Open()
Dim da As New SqlClient.SqlDataAdapter(sqlString, con)
Dim ds As New DataSet
da.Fill(ds, "Table")
DataGridView1.DataSource = ds.Tables("Table")
ds.Dispose()
da.Dispose()
SqlConnection.ClearPool(con)
End Using
2/
Using con As New SqlClient.SqlConnection("YOUR SQL CONNECTION STRING")
Dim command As New SqlCommand("SELECT * FROM Table WHERE intApprovedOvertime = 0 AND fkEmployee = pkEmployee", con)
Dim reader As SqlDataReader = command.ExecuteReader()
While reader.Read()
DataGridView1.Columns.Rows.Add(reader(0), reader(1))
End While
End Using
在第 2 点中,您需要记住使reader(x)适应您需要添加到行的值的数量。您还可以通过以下方式获取特定列:
"SELECT Table.Name, Table.Position FROM Table WHERE intApprovedOvertime = 0 AND fkEmployee = pkEmployee"
可能有一些小的语法错误我没有带 IDE 哈哈。