将数据集的多个表绑定到datagridview [英] To bind multiple tables of dataset to datagridview
问题描述
我已经创建了Windows Application(c#3.5和sql server 2005).在这里,我在数据集中有多个表,现在我想将其绑定到单个DataGridView.有人可以帮助我吗?
I have create windows Application(c#3.5 and sql server 2005).In this , I have multiple tables in dataset now i want to bind that to a single DataGridView.Can anybody help me?
推荐答案
我现在在数据集中有多个表,我想将其绑定到单个DataGridView
您不能将多个表绑定到单个数据网格.一次只能一次.
I have multiple tables in dataset now i want to bind that to a single DataGridView
You cannot bind multiple tables to a single datagrid. Only one at a time.
// method to fill the dataset
public void FillMyDataSet(MyDataSet ds)
{
string sql = "SELECT a.*, b.* FROM MyTable a JOIN MyOtherTable b ON a.key = b.key";
using (SqlDataAdapter da = new SqlDataAdapter(sql, conn))
{
da.Fill(ds, "MyTable");
}
}
// call it like this
MyDataSet dsMine = new MyDataSet();
this.FillMyDataSet(dsMine);
MyGridView.DataSource = dsMine;
或者您也可以尝试使用此代码....表映射非常棒..
SqlDataAdapter适配器=新的SqlDataAdapter(
选择*来自客户;选择*来自订单",连接);
adapter.TableMappings.Add("Table","Customer");
adapter.TableMappings.Add("Table1","Order");
adapter.Fill(ds);
Or you can try this code.... table mapping is awesome one..
SqlDataAdapter adapter = new SqlDataAdapter(
"SELECT * FROM Customers; SELECT * FROM Orders", connection);
adapter.TableMappings.Add("Table", "Customer");
adapter.TableMappings.Add("Table1", "Order");
adapter.Fill(ds);
这篇关于将数据集的多个表绑定到datagridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!