将多个表中的列放入 vb.net 中的 1 个 datagridview
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13007599/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
getting columns from multiple tables into 1 datagridview in vb.net
提问by Alex Luthor
I'm asking once more, since I'm really new in vb, I am using visual studio 2010 and mysql for my database, I need help getting different columns from different tables but same database and load it into one datagridview. Any kind of help or tips would be much appreciated . Please and Thank you.
我再问一次,因为我真的是 vb 新手,我正在为我的数据库使用 Visual Studio 2010 和 mysql,我需要帮助从不同的表但相同的数据库中获取不同的列并将其加载到一个 datagridview 中。任何形式的帮助或提示将不胜感激。谢谢,麻烦您了。
回答by Tim Schmelter
One way would be to use a DataTablewith all joined columns as datasource which you can fill with a DataAdapter:
一种方法是将 aDataTable与所有连接的列一起用作数据源,您可以使用 a 填充DataAdapter:
Private Function GetDataSource() As DataTable
Const sqlSelect As String = "SELECT a.Col1 AS aCol1,a.Col2 AS aCol2,b.Col1 AS bCol1,b.Col2 AS bCol2 " & _
"FROM dbo.TableA AS a INNER JOIN dbo.TableB AS b ON a.IdCol=b.aIdCol " & _
"ORDER BY aCol1 ASC,bCol1 ASC"
Try
Dim table = New DataTable()
Using con = New MySqlConnection(My.Settings.MySqlConnectionString)
con.Open()
Using da = New MySqlDataAdapter(sqlSelect, con)
da.Fill(table)
Return table
End Using
End Using
Catch ex As Exception
' log message instead '
Throw ' don't use throw new Exception or throw ex '
End Try
End Function
Now you can use this DataTableas DataSource for the DataGridView:
现在您可以将其DataTable用作以下数据源DataGridView:
me.dataGridView1.DataSource = GetDataSource()

