简体   繁体   中英

How to get Name instead of ID in datagridview

i have created a table

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

insert data on the table

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

and another table is `

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

and insert data product table

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

and finally i have load product table in datagrid using the command

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

and show data in GridView

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

it's work well but i need show SUP_NAME instead of SUP_ID such as

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

please see the pic here

Sup_ID is in your product table, and Sup_Name is in your suppliers1 table.

But in your code you only select from the product table. So you need to do a join between the two tables in the sql in your code.

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

This will generate a result which looks like this;

表格图片

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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