不好意思,我想要像这样制作布局:
数据库结构如下: 我想使用select查询从2个表加载数据并将数据显示到gridview .. 到目前为止,我在互联网上找到了教程,但使用单表.. 请给我建议......
答案 0 :(得分:2)
你应该尝试这样的事情。
string query = @"SELECT * FROM Sales_so
Inner join Com_customer on COM_Customer.Com_customer_id = Sales_SO.Com_customer_Id";
Connection con = new Connection();
SqlDataAdapter dataAdapter = new SqlDataAdapter(query, con);
SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
DataSet ds = new DataSet();
dataAdapter.Fill(ds);
dataGridView1.ReadOnly = true;
dataGridView.DataSource = ds.tables[0];
答案 1 :(得分:2)
写入连接3个表的查询,将结果放入dataSet之后,为网格的DataSource设置此数据集。
SqlCommand cmd = new SqlCommand();
cmd.ConnectionString = "your connection";
cmd.CommandText = @"
SELECT
so.* -- WRITE Here columns which you need from the tables.
FROM
SALES_SO so
INNER JOIN
SALES_SO_LITEM soItem ON soItem.SALES_SO_ID = so.SALES_SO_ID
INNER JOIN
COM_CUSTOMER cus ON cus.COM_CUSTOMER_ID = so.COM_CUSTOMER_ID
";
DataSet resultDst = new DataSet();
using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))
{
adapter.Fill(resultDst, defaultTable);
}
grid.DataSource = resultDst.Tables[0];
grid.DataBind();
你需要这样的事情