简体   繁体   English

如何使用数据源从多个表到GridView

[英]How To use Datasource from Multiple Table to GridView

excuse me, I want make layout like this: 对不起,我要这样布局: 布局

And the database stucture is like this: 数据库结构是这样的: D b I want use select query to load data from 2 tables and show the data to the gridview.. so far, I found tutorials at internet but using single table.. Please give me suggestion... 我想使用select查询从2个表中加载数据并将数据显示到gridview ..到目前为止,我在Internet上找到了使用单个表的教程。.请给我建议...

You should try something like this. 您应该尝试这样的事情。

 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];

Write query which Joins 3 tables, put the results in dataSet after that set this dataSet for DataSource of the grid. 编写联接3个表的查询,将结果放入dataSet中,然后为网格的DataSource设置此dataSet。

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();

You need somethig like this 你需要这样的东西

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM