linq绑定到GridView显示不了全部数据
用的默认的数据库是NorthWind aspx页面就放了个GridView
<asp:GridView ID="GridView1" runat="server" >
</asp:GridView>
绑定的代码
protected void Page_Load(object sender, EventArgs e)
{
NorthwindDataContext db = new NorthwindDataContext();
var q =
from e1 in db.Employees
join o in db.Orders on e1.EmployeeID equals o.EmployeeID into ords
from o in ords.DefaultIfEmpty()
select new
{
e1.FirstName,
e1.LastName,
Order = o
};
GridView1.DataSource = q;
GridView1.DataBind();
}
最后只会显示FirstName 和 LastName这两列 ,这两列的数据也都是正确的,但是我断点调试了一下,把生成的sql语句放到sql server 中去运行了一下,查询出来的符合条件的记录有17列,为什么在GridView中显示出来的记录只有两列?是不是那里要设置一下?
[最优解释]
你的q是有17列,但是你查询的后面的select是只有两列的
e1.FirstName,e1.LastName 所以最后绑定的时候是只有两列的。
把其他需要显示的列也加上去。
[其他解释]
你跟综一下q的值,不要改动SQL语句,
GridView1.DataSource = q;
GridView1.DataBind();
就是这个Q的值
[其他解释]
select new
{
e1.FirstName,
e1.LastName,
Order = o
};
LZ,会不会是你最后在筛选的时候是查了FirstName和LastName
把这个改成select e1看下看呢
[其他解释]
select new
{
e1.FirstName,
e1.LastName,
Order = o
};
你这个只找了 e1.FirstName,
e1.LastName两列。。。。
[其他解释]
因为你这个linq只查出了两列,要想显示十七列,就一个一个写吧
id=o.id
name=o.name
……
[其他解释]
有SQL 不用非的用 linq ,第一如果对象绑定一定要有个共有属性和 GridView 的DataboundFile 对应。要不不会显示的。
[其他解释]
你断点一下q,查看q的值是多少个
------其他解决方案--------------------
我看过了,我还把值放到sqlserver中,改成了sql语句的形式运行了,显示的结果有十七列,可是在GridView中只会显示FirstName和LastName这两列,不知道为什么会出现这种情况
[其他解释]
我跟踪的q的值就是这个
{SELECT [t0].[FirstName], [t0].[LastName], [t2].[test], [t2].[OrderID], [t2].[CustomerID], [t2].[EmployeeID], [t2].[OrderDate], [t2].[RequiredDate], [t2].[ShippedDate], [t2].[ShipVia], [t2].[Freight], [t2].[ShipName], [t2].[ShipAddress], [t2].[ShipCity], [t2].[ShipRegion], [t2].[ShipPostalCode], [t2].[ShipCountry]
FROM [dbo].[Employees] AS [t0]
LEFT OUTER JOIN (
SELECT 1 AS [test], [t1].[OrderID], [t1].[CustomerID], [t1].[EmployeeID], [t1].[OrderDate], [t1].[RequiredDate], [t1].[ShippedDate], [t1].[ShipVia], [t1].[Freight], [t1].[ShipName], [t1].[ShipAddress], [t1].[ShipCity], [t1].[ShipRegion], [t1].[ShipPostalCode], [t1].[ShipCountry]
FROM [dbo].[Orders] AS [t1]
) AS [t2] ON ([t0].[EmployeeID]) = [t2].[EmployeeID]
}
我去掉开头和末尾的那两个大括号,然后 在sql server 中运行,查出来的符合条件的记录有十七列,而在GridView中显示的就只有FirstName 和LastName这两列
[其他解释]
掉出首页了,顶上去!
[其他解释]
我调试的时候看了下q的值 var q = from e1 in db.Employees join o in db.Orders on e1.EmployeeID equals o.EmployeeID into ords from o in ords.DefaultIfEmpty() select new { e1.FirstName, e1.LastName, Order = o };
然后把相应的sql语句放到sql server 中去运行,查询到的列有十七列 ,为什么在GridView中只显示两列?
[其他解释]
可是q的值不是这个吗?
{SELECT [t0].[FirstName], [t0].[LastName], [t2].[test], [t2].[OrderID], [t2].[CustomerID], [t2].[EmployeeID], [t2].[OrderDate], [t2].[RequiredDate], [t2].[ShippedDate], [t2].[ShipVia], [t2].[Freight], [t2].[ShipName], [t2].[ShipAddress], [t2].[ShipCity], [t2].[ShipRegion], [t2].[ShipPostalCode], [t2].[ShipCountry]
FROM [dbo].[Employees] AS [t0]
LEFT OUTER JOIN (
SELECT 1 AS [test], [t1].[OrderID], [t1].[CustomerID], [t1].[EmployeeID], [t1].[OrderDate], [t1].[RequiredDate], [t1].[ShippedDate], [t1].[ShipVia], [t1].[Freight], [t1].[ShipName], [t1].[ShipAddress], [t1].[ShipCity], [t1].[ShipRegion], [t1].[ShipPostalCode], [t1].[ShipCountry]
FROM [dbo].[Orders] AS [t1]
) AS [t2] ON ([t0].[EmployeeID]) = [t2].[EmployeeID]
}
去掉一前一后的大括号,然后在sql server 中运行都能查询出十七列来
而q又等于这个
var q = from e1 in db.Employees join o in db.Orders on e1.EmployeeID equals o.EmployeeID into ords from o in ords.DefaultIfEmpty() select new { e1.FirstName, e1.LastName, Order = o };
为什么结果不一样?只是形式变了而已呀