如何在C#中的datagridview中显示关系? [英] How do I display a relationship in a datagridview in C#?
问题描述
我有两个表路由和RouteInvoices。
路由有列RouteId和RouteName。
RouteInvoices有RouteId和其他一些路线发票栏。
这两个表在RouteInvoices中使用RouteID作为外键相关。
我尝试过:
我使用datagridview在我的应用程序中显示RouteInvoices
SQLiteDataAdapter adapter = new SQLiteDataAdapter(SELECT * FROM [route_invoices];,conn);
DataTable table = new DataTable();
adapter.Fill(table);
dataGridView1.DataSource = table;
这显示了routeinvoices表中的routeID,但我想显示路由名称。
我该怎么办?
你需要的只是使用正确的查询,例如:
SELECT R. *,RI。*
FROM routes AS R INNER JOIN [route_invoices] AS RI ON R.RouteID = RI.RouteID
以上查询将返回routes
表中的数据以及来自<$的相应数据c $ c> route_invoices table。
换句话说,你必须提高你对SQL [ ^ ]。
几种类型的连接。如果您想要发现它们,我建议您阅读:视觉表现形式SQL连接 [ ^
I have two tables Routes and RouteInvoices.
Routes has columns RouteId and RouteName.
RouteInvoices has RouteId and some other route invoice columns.
These two tables are related using RouteID as a foreign key in RouteInvoices.
What I have tried:
I use a datagridview to display RouteInvoices in my application
SQLiteDataAdapter adapter = new SQLiteDataAdapter("SELECT * FROM [route_invoices];", conn); DataTable table = new DataTable(); adapter.Fill(table); dataGridView1.DataSource = table;
This shows routeID in the routeinvoices table but I want to display the route name.
How do I do this?
All what you need is to use proper query, such as:
SELECT R.*, RI.* FROM routes AS R INNER JOIN [route_invoices] AS RI ON R.RouteID = RI.RouteID
Above query will return data fromroutes
table and corresponding data fromroute_invoices
table.
In other words, you have to improve your knowledge about SQL[^].
There's few types of joins. In case you may want to discover them, i'd suggest to read this: Visual Representation of SQL Joins[^]
这篇关于如何在C#中的datagridview中显示关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!