2017-08-09 53 views
0

獲得的,而不是ID名稱我創建了一個表如何在datagridview的

create table suppliers1(
sup_id NUMBER(20) PRIMARY KEY, 
sup_name VARCHAR2(40), 
sup_address varchar2(50), 
sup_phone NUMBER(15)); 

插入數據的表

INSERT INTO suppliers1 (SUP_ID,SUP_NAME,SUP_ADDRESS,SUP_PHONE) 
VALUES (100,'PPS','Bds',99545414); 
INSERT INTO suppliers1 (SUP_ID,SUP_NAME,SUP_ADDRESS,SUP_PHONE) 
VALUES (200,'Abcd','Dhaka',0295469); 
INSERT INTO suppliers1 (SUP_ID,SUP_NAME,SUP_ADDRESS,SUP_PHONE) 
VALUES (300,'Xyz','Ctg',0896547556); 

,另一臺是 `

CREATE TABLE Product (
item_id NUMBER(20) PRIMARY KEY, 
item_name VARCHAR2(50) not null, 
sup_id NUMBER(20), 
unit_id NUMBER(10) , 
CONSTRAINT fk_id FOREIGN KEY (sup_id) REFERENCES 
suppliers1(sup_id) 
); 

和插入數據產品表

INSERT INTO Product(item_id,item_name,sup_id) values(03,'Product1',100); 
INSERT INTO Product(item_id,item_name,sup_id) values(02,'Product2',200); 
INSERT INTO Product(item_id,item_name,sup_id) values(01,'Product3',100); 

,最後我已經在DataGrid中裝載的產品表,可以使用命令的GridView

OleDbDataAdapter adp = new OleDbDataAdapter("SELECT * FROM product", con); 
      DataTable dt = new DataTable(); 
      dataGridView1.DataSource = dt; 

和顯示數據

ITEM_ID ITEM_NAME SUP_ID 
1  Product1  100 
2  Product2  200 
3  Product3  100 

它的工作很好,但我需要顯示SUP_NAME而不是SUP_ID

>  ITEM_ID ITEM_NAME SUP_NAME 
>  1  Product1  PPS 
>  2  Product2  Abcd 
>  3  Product3  PPS 

please see the pic here

回答

0

SUP_ID是你的產品表,Sup_Name是你suppliers1表。

但是在您的代碼中,您只能從產品表中進行選擇。 因此,您需要在代碼中的sql中的兩個表之間進行連接。

dataGridView1.AutoGenerateColumns = true; 
SQLiteConnection dbConnection = new SQLiteConnection("Data Source=C:\\Data\\SODB.db;Version=3;"); 
dbConnection.Open(); 

dataGridView1.DataSource = null; 
dataGridView1.Columns.Clear(); 
dataGridView1.Rows.Clear(); 
SQLiteDataAdapter adp = new SQLiteDataAdapter("SELECT item_id, item_name, sup_name FROM product, suppliers1 where product.sup_id = suppliers1.sup_id", dbConnection); 
DataTable dt = new DataTable();    
adp.Fill(dt); 
dataGridView1.DataSource = dt; 

dbConnection.Close(); 

,這將產生一個結果,它看起來像這樣;

Form Picture

+0

你說得對,但我需要管理代碼有問題。 – khan

+0

這不是你問你的問題。請澄清這個問題,並確切地告訴我們你想要什麼。並顯示代碼如何你今天編程生成這個 –

+0

謝謝,我已經插入數據產品表使用一些文本字段使用C#和需要顯示數據SUP_NAME而不是SUP_ID在數據網格上, – khan