在Gridview上绑定2表的问题 [英] problem to bind 2 table on the Gridview

查看:69
本文介绍了在Gridview上绑定2表的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的网站上有网格视图.我想从具有内部联接的2表中选择数据并在gridview中显示此数据,但是sql数据源无法将2表中的数据绑定到显示.请帮我.
我要选择产品图片,产品名称,产品公司.我的查询是:

  SELECT  Product.ProPic,Product.ProName,Product.ProCompany,SUM(Sales.SaleProCount) AS  Expr1
 FROM 销售 INNER   JOIN 
                      产品打开 Sales.SaleProId = Product.ProID
位置(Sales.SaleReg =  1 )
  BY  Product.ProName,Product.ProCompany,Product.ProPic
订单  BY  Expr1  DESC  

正如您已经说过的,将网格绑定到两个表的方法是将两个表的数据一起获取.网格不能同时绑定到两个数据源.

看起来您已经具有可用于内部联接的公用密钥.使用它,用您需要的所有数据构成查询,然后使用此查询从数据库中获取数据并将检索到的数据表绑定到网格.因此,单个表到一个网格-完成.

尝试!


如果您希望在应用程序级别上使用它,请使用LINQ

  var 结果= 来自 rs  db.ratesSchedule
              加入 ud 中db.userdetails on
                   {rs.sid,rs.tabletype}等于
                   {ud.sid,表类型= " } 



为此SQL

 选择 * 来自 ratesSchedule   rs
    内部 加入用户详细说明 as  ud 上rs.sid = ud.sid
     rs.tabletype = '  d' 


I have a grid view in my website. I want select data from 2 table with inner join and show this data in gridview but sql data source can not bind data from 2 table to show. plz help me.
I want select product picture ,product name ,product company. My query is :

SELECT     Product.ProPic, Product.ProName, Product.ProCompany, SUM(Sales.SaleProCount) AS Expr1
FROM         Sales INNER JOIN
                      Product ON Sales.SaleProId = Product.ProID
WHERE     (Sales.SaleReg = 1)
GROUP BY Product.ProName, Product.ProCompany, Product.ProPic
ORDER BY Expr1 DESC

解决方案

As you already said, the way to bind a grid to two tables is to fetch data for two tables together. Grid cannot bind to two datasources at same time.

Looks like you already have a common key that can be used for Inner join. Using it, form the query with all data you need and then use this query to fetch data from DB and bind the retrieved datatable to the grid. Thus, single table to a grid - done.

Try!


if you wish to do it application level, use LINQ

var results = from rs in db.ratesSchedule
              join ud in db.userdetails on 
                  new { rs.sid, rs.tabletype } equals 
                  new { ud.sid, tabletype = "d" }



for this SQL

select * from ratesSchedule as rs
    inner join userdetails as ud on rs.sid = ud.sid
    and rs.tabletype = 'd'


这篇关于在Gridview上绑定2表的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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