将多个数据库绑定到单个DataGridView [英] Bind multiple databases to a single DataGridView
问题描述
我有一个c#winform项目,该项目具有对SQL数据库的多个绑定.
其中包括2个DataGridViews,它们完全可以满足我的需求.两个Datagrids从2个不同的数据库返回相同的查询结果.我要完成的工作是将2个DataGridViews合并为一个,以便单个DataGridViews显示所有返回的结果.
换句话说,我可以将多个数据库绑定到单个DataGridView吗?
我在下面列出了我当前的代码,其中只有一个SQLCommand,但是有2个连接(未显示代码,因为它工作正常),两个DataSet和2个DataAdapter都用于2个DataGridViews.拥有2个DataGridViews效率不高,而且我们还将很快推出更多的Dababase服务器,因此我试图找出一种将所有结果提交到一个网格的方法.
Hi I have a c# winform project which has multiple bindings to a SQL Database.
Included are 2 DataGridViews which works perfectly for my needs. Both Datagrids return the same query results from 2 different databases. What I''m trying to accomplish is to combine the 2 DataGridViews into one so that the single DataGridViews shows all returned results.
In other words can I bind multiple databases to a single DataGridView?
I''ve listed my current code below where I have a single SQLCommand but 2 connections (Code not shown because this is working fine) 2 DataSets and 2 DataAdapters all for 2 DataGridViews. It''s somewhat innefficient to have 2 DataGridViews and we''re also bringing on more Dababase servers soon so I''m trying to figure out a way to have all the results submitted to one of the grids.
SqlCommand cmd = new SqlCommand
("select 'this is my SQL statement'");
cmd.Connection = connectionDB1;
daAccountsDB1.SelectCommand = cmd;
daAccountsDB1.Fill(dsDB1, "tblAccount");
dgvAccountSearchDB1.DataSource = dsDB1.Tables["tblAccount"];
cmd.Connection = connectionDB2;
daAccountsDB2.SelectCommand = cmd;
daAccountsDB2.Fill(dsDB2, "tblAccount");
dgvAccountSearchDB2.DataSource = dsDB2.Tables["tblAccount"];
我想这一定是要先解决的,所以任何人都有理想吗?
我当时想将所有内容都整合到一个集合中,但是我感觉还有其他更好的方法.
有理想吗?
非常感谢,
〜Chris
I''m thinking that this must have come up before so does anyone have any ideals?
I was thinking of incorporation everything into a collection but I have a feeling there are other, better, ways.
Any ideals?
Thanks very much,
~ Chris
推荐答案
尝试在数据网格的数据源上使用合并
try using merge on the datagrid''s datasource
检查此
http://msdn.microsoft.com/en-us/library/803bh6bc.aspx [ ^ ]
http://aspalliance.com/148_Merging_two_Datasets_into_a_single_Datagrid [ ^ ]
check this
http://msdn.microsoft.com/en-us/library/803bh6bc.aspx[^]
http://aspalliance.com/148_Merging_two_Datasets_into_a_single_Datagrid[^]
是的.
实际上,它也可以使我从其他数据库中提取数据.
非常感谢您为我指明了正确的方向,
〜克里斯
Yes this worked.
In fact it came along where I was able to pull data from other db''s too.
Thanks very much for pointing me in the right direction,
~ Chris
这篇关于将多个数据库绑定到单个DataGridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!