Khaneddy2013
Khaneddy2013

Reputation: 1321

How To use Datasource from Multiple Table to GridView

excuse me, I want make layout like this: layout

And the database stucture is like this: db 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...

Upvotes: 0

Views: 3584

Answers (2)

NMK
NMK

Reputation: 1020

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

Upvotes: 2

mybirthname
mybirthname

Reputation: 18137

Write query which Joins 3 tables, put the results in dataSet after that set this dataSet for DataSource of the grid.

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

Upvotes: 2

Related Questions