使用来自两个表的数据填充datagridview控件 [英] Populate datagridview control with data from two tables

查看:70
本文介绍了使用来自两个表的数据填充datagridview控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用datagridview来显示数据库中两个表的信息。 

I am trying to get a datagridview to display information from two tables in the database. 

表格首先是"Campaign_Types"。具有属性"campaignTypeID"(主键)和"标题"和第二个"营销活动"具有属性"campaignID"(主键),"campaignTypeID"(对于广告系列类型为foriegn
键),"title","description","startDate","endDate"。 

The tables are first "Campaign_Types" with attributes "campaignTypeID"(Primary key) and "title" and second "Marketing Campaigns" with attributes "campaignID"(Primary Key), "campaignTypeID"(foriegn key to Campaign Types), "title", "description", "startDate", "endDate". 

所以我希望datagridview显示市场营销活动的所有属性,但用相应的"title"替换campaignTypeID外键。来自"Campaign_Types"的字段。 

So I want the datagridview to display all the attributes from marketing campaigns but replace the campaignTypeID foreign key with the corresponding "title" field from "Campaign_Types". 

我尝试使用执行此操作的查询编辑bindingsource但它会产生错误"新查询返回的架构与基础不同查询" ;.所以在那之后我尝试通过添加一个非数据绑定组合框列,将显示
样式设置为"无"(因此它只显示为纯文本),然后将其绑定到广告系列类型,但我认为我所遵循的指示已过时,因为我无法使其工作。 

I tried to edit the bindingsource with a query that does this but it produces an error "the schema returned by the new query differs from the base query". So after that I tried to do it by adding a non-databound combobox column, setting the display style to "nothing"(so it just appears as plain text) and then binding it to campaign types, but I think the instructions I followed were out of date as I couldn't get this to work. 

一种可能的解决方案是添加一个普通的非数据绑定列并将一些逻辑编码到当campaignTypeID字段显示特定数字时,它会显示相应的广告系列类型名称。在psuedocode中"当'campaignTypeID'列是"1"时
在"类型"中显示"电子邮件"。列,当它显示'2'时显示'电视'等"但是我无法弄清楚这是否可行。 

One possible solution would be to add a plain non-data bound column and code some logic into it that displays the corresponding campaign type name when the campaignTypeID field shows a certain number. In psuedocode "When 'campaignTypeID' column is "1" display 'Email' in the "Type" column, when it displays '2" display 'TV', etc" but I can't figure out if that's even possible or not. 

推荐答案

如果这仅用于显示,您可以使用JOIN使用单个SELECT语句并返回Campaign_Types.Title而不是CampaingTYpeID。

If this is just for display you can do it with a single SELECT statement using a JOIN and return the Campaign_Types.Title instead of the CampaingTYpeID.

如果这是用于显示和编辑,那么您可以使用一个ComboBox列绑定到第二个绑定源,该第二个绑定源填充了所有的Campaign_Types。

If this is for display and edit then you can use a ComboBox column bound to a second bindingsource which is filled with all of the Campaign_Types.

Karen Payne已经拥有数以万计的数据示例,所以我会给她一些时间来发布一个相关示例(或链接到现有样本)。 在此期间,您可以随意搜索这些论坛,因为类似的问题已经多次解决。

Karen Payne already has a zillion data examples so I'll give some time for her to post a relevant example (or link to and existing sample).  In the meantime feel free to search these forums as similar issues have already been resolved many times.


这篇关于使用来自两个表的数据填充datagridview控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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