微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

gridview行绑定到linq查询

如何解决gridview行绑定到linq查询

| 我需要将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>
    

解决方法

我相信您将需要从GridView RowDataBound事件中调用辅助查询,然后手动填充辅助值字段: http://msdn.microsoft.com/zh-CN/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx 但是,您似乎可以通过SQL连接或子选择获得所需的内容。如果您使用的是SQL数据源,那么我将研究SQL,看看是否可以在单个Select语句中获取数据,然后返回并查看是否可以将SQL查询转换为适当的LINQ-否则我可能会调用它通过存储过程。但是,如果您致力于LINQ,或者如果联接或子选择将不起作用,那么RowDataBound是您的最佳选择。     ,我不确定,网格视图会为您提供所需的功能,因为数据集将绑定到整个网格。您可以在运行时创建HTM1表结构,并在创建的每一行上对其进行控制以绑定任何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. 
    

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。