如何将 LINQ 查询绑定到 gridview 的行?
我需要将 LINQ 查询绑定到 gridview 的行。我正在尝试创建一个像 expedia 中的矩阵一样的表,其中不同行上有直达、1 个站点和 2 个站点。我不太确定如何将查询绑定到 gridview 行。我很感激你的帮助。
var imgquery = from f in XElement.Load(MapPath("flightdata3.xml")).Elements("flight")
orderby Convert.ToInt32(f.Element("price").Value)
select new
{
ImagePath = (string)f.Element("airlineimageurl").Value
};
//query for gvMatrix where numberofstops=0
var numstops0query = from f in XElement.Load(MapPath("flightdata3.xml")).Elements("flight")
where Convert.ToInt32(f.Element("numberofstops").Value) == 0
orderby Convert.ToInt32(f.Element("price").Value)
select new
{
Price = "$" + (Int32)f.Element("price"),
ImagePath = (string)f.Element("airlineimageurl").Value
};
<asp:GridView ID="gvMatrix" runat="server">
</asp:GridView>
I need to bind a LINQ query to the row of a gridview. I'm trying to create a table like the matrix in expedia where there are nonstop, 1 stop and 2 stops on different rows. I'm not quite sure how to bind the queries to the gridview rows. I appreciate your help.
var imgquery = from f in XElement.Load(MapPath("flightdata3.xml")).Elements("flight")
orderby Convert.ToInt32(f.Element("price").Value)
select new
{
ImagePath = (string)f.Element("airlineimageurl").Value
};
//query for gvMatrix where numberofstops=0
var numstops0query = from f in XElement.Load(MapPath("flightdata3.xml")).Elements("flight")
where Convert.ToInt32(f.Element("numberofstops").Value) == 0
orderby Convert.ToInt32(f.Element("price").Value)
select new
{
Price = "$" + (Int32)f.Element("price"),
ImagePath = (string)f.Element("airlineimageurl").Value
};
<asp:GridView ID="gvMatrix" runat="server">
</asp:GridView>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信您需要从 GridView RowDataBound 事件调用辅助查询,然后手动填充辅助值字段:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx
但是,看来您也许能够在 SQL 连接或子选择中获得您正在寻找的内容。如果您使用的是 SQL 数据源,我会使用 SQL 来查看是否可以在单个 Select 语句中获取数据,然后返回并查看是否可以将 SQL 查询转换为适当的 LINQ - 或者我可能会调用它通过存储过程。但是,如果您致力于 LINQ,或者如果联接或子选择不起作用,那么 RowDataBound 是您的最佳选择。
I believe you will need to call your secondary query from the GridView RowDataBound event and then manually populate your secondary value fields:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx
However, it seems that you might be able to get what you are looking for in a SQL join or sub-select. If you are using a SQL data source, I would play around with SQL to see if I could get the data in a single Select statement, then go back and see if I can translate the SQL query into appropriate LINQ -- or I might call it via a stored procedure. However, if you are committed to LINQ, or if a join or sub-select will not do, then RowDataBound is your best bet.
我不确定网格视图是否会为您提供所需的功能,因为数据集将绑定到整个网格。您可以在运行时创建 HTMl 表结构,并且在您创建的每一行上,您都可以控制它来绑定您拥有的任何 linq 数据。希望有帮助:)
i am not sure that, Grid View will give you the desired functionality because the dataset will bind to whole grid. You can create HTMl table structure at runtime and on every row you create you have control over it to bind whatever linq data that you have. hope it helps :)