如何在C#中的datagridview中显示关系? [英] How do I display a relationship in a datagridview in C#?

查看:64
本文介绍了如何在C#中的datagridview中显示关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表路由和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 from routes table and corresponding data from route_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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆