开发者

Unable to bind LINQ to gridview

开发者 https://www.devze.com 2023-03-06 22:22 出处:网络
I just using a simple LINQ query having group by clause and trying to bind its result set to GridView.

I just using a simple LINQ query having group by clause and trying to bind its result set to GridView. My LINQ query looks like

var expData = from c in WebDB.TransTable
              group c by c.enterdate into g
              select g;

Grid view on ASP.NET page

<asp:GridView ID="GridVi开发者_JAVA技巧ew1" AutoGenerateColumns="true" runat="server" DataKeyField="Key" />

But getting the error:

A field or property with the name 'Key' was not found on the selected data source.

Anyone can help me please?

None of the answers were helpful


expData is a string as you've put the LINQ query inside quotation marks.

var expData = "from c in WebDB.TransTable
            group c by c.enterdate into g
              select g;"

System.String does not have a property called Key, hence the error.

If you remove the quotation marks it should all work fine.


EDIT This ignores the "" in your linq statement

Look at the attribute on your asp.net GridView(aspx code): DataKeyField="" is pointing to a column name that does not exist in your linq query

<asp:GridView AutoGenerateColumns="true" DataKeyField="Key"/>


I had your problem either , I wrote like this . I hope this work for you :

var expData = from c in WebDB.TransTable
              group c by c.enterdate into g
              select new {EnterDate = g.Key};
0

精彩评论

暂无评论...
验证码 换一张
取 消