简体   繁体   中英

How to display name instead of id in DataGridView?

I have two tables:

CREATE TABLE "clients" (
    "id"    INTEGER NOT NULL UNIQUE,
    "name"  TEXT,
    "address"   TEXT,
    "telephone" TEXT,
    "passport"  TEXT
);

CREATE TABLE "requests" (
    "id"    INTEGER NOT NULL UNIQUE,
    "date"  INTEGER,
    "client_id" INTEGER,
);

I have loaded them using SQLiteDataAdapter into DataTable objects. With this code, I am displaying a table called "requests" in the DataGridView:

_requestsDataGridView.DataSource = requestsDataTable;

The column titled "client_id" displays the id for which the request was created. How can I connect the second DataTable to the DataGridView and make the name of the client who created the request appear instead of the client_id?

I suggest doing this in the query. Using an INNER JOIN should give you the results you are looking for. Something like…

SELECT date, name From clients
INNER JOIN requests on requests.client_id = clients.id

This should return one table with two columns the client name and request date.

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