如何将 LINQ 查询绑定到 gridview 的行?

发布于 2024-11-04 02:26:21 字数 1025 浏览 0 评论 0原文

我需要将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

在你怀里撒娇 2024-11-11 02:26:21

我相信您需要从 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.

回忆追雨的时光 2024-11-11 02:26:21

我不确定网格视图是否会为您提供所需的功能,因为数据集将绑定到整个网格。您可以在运行时创建 HTMl 表结构,并且在您创建的每一行上,您都可以控制它来绑定您拥有的任何 linq 数据。希望有帮助:)

placeHolder.Controls.Clear(); //asp:placeholder holds the table structure.
Table table = new Table();
table.ID = "table";
placeHolder.Controls.Add(table); //adding to place holder

TableRow row = new TableRow();
row.ID ="rowID";
table.Rows.Add(row); //creating first row for first linq dataset

var nonstop0query = from x in obj select new {x.ID, x.Name, x.Age}; //first linq dataset.

//Creating cells for the data returned by the nonstop0query
TableCell cell = new TableCell();
cell.ID = "cell1";
row.Cells.Add(cell);
cell.Text = nonstop0Query[0];

cell = new TableCell();
cell.ID = "cell2";
row.Cells.Add(cell);
cell.Text = nonstop0Query[1];

cell.ID = "cell3";
row.Cells.Add(cell);
cell.Text = nonstop0Query[2];

//Same way can be done for more dataset to bind to row. 

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 :)

placeHolder.Controls.Clear(); //asp:placeholder holds the table structure.
Table table = new Table();
table.ID = "table";
placeHolder.Controls.Add(table); //adding to place holder

TableRow row = new TableRow();
row.ID ="rowID";
table.Rows.Add(row); //creating first row for first linq dataset

var nonstop0query = from x in obj select new {x.ID, x.Name, x.Age}; //first linq dataset.

//Creating cells for the data returned by the nonstop0query
TableCell cell = new TableCell();
cell.ID = "cell1";
row.Cells.Add(cell);
cell.Text = nonstop0Query[0];

cell = new TableCell();
cell.ID = "cell2";
row.Cells.Add(cell);
cell.Text = nonstop0Query[1];

cell.ID = "cell3";
row.Cells.Add(cell);
cell.Text = nonstop0Query[2];

//Same way can be done for more dataset to bind to row. 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文