【问题标题】:DataGridView and values from multiple tables?DataGridView 和来自多个表的值?
【发布时间】:2016-06-21 21:27:03
【问题描述】:

在我的一个 WinForms 上,我必须使用一个 DataGridView 控件来显示来自 DB 表的数据。在该表中,外键很少,所以我的 DGV 如下所示:

Column1 | Column2 | ForeignKey1 | ForeignKey2 | ...
================================================...
Text    | Text    | 1           | 1           | ...

有没有办法以某种方式显示外键引用的值?

【问题讨论】:

  • 您到底想要实现什么,“引用”是什么意思,您想在哪里显示这些值?
  • 这种需求有多种解决方案。网格的数据源是什么?
  • 您可以通过修改获取数据的查询来做到这一点。添加您的查询以便我可以修改它
  • 一个可能的解决方案是Master-Detail
  • 当然是。只要可以转换为文本格式,您就可以在 gridview 的每个特定单元格中显示任何数据。

标签: c# winforms datagridview


【解决方案1】:

您可以使用以下方法:

让你的tbl1 有栏目:

stu_id (primary key),
stu_name

让你的tbl2 有栏目:

course_id (primary key),
course_name

那么你的tbl3 有列:

teacher, 
stu_id (foreign key),
course_id (foreign key)

现在您可以打印来自tbl3 的数据,外键为:

select teacher,
(select stu_name from tbl1 where stu_id=tbl3.stu_id) stu_name ,
(select course_name from tbl2 where course_id=tbl3.course_id) course_name
 from tbl3;

只需在Datatable 中从上述方法中获取数据并提供GridView 数据源。

除此之外,还有很多其他方法可以做到。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 2015-06-12
    • 1970-01-01
    • 1970-01-01
    • 2015-04-25
    • 1970-01-01
    相关资源
    最近更新 更多