C# GridView未从LINQ语句填充

C# GridView未从LINQ语句填充,c#,asp.net,linq-to-sql,web-applications,C#,Asp.net,Linq To Sql,Web Applications,我有一个GridView,它没有绑定到任何数据源。在我的页面加载事件中,我正在编写以下代码: protected void Page_Load(object sender, EventArgs e) { string getEntity = Request.QueryString["EntityID"]; int getIntEntity = Int16.Parse(getEntity); OISLinq2SqlVs1DataConte

我有一个GridView,它没有绑定到任何数据源。在我的页面加载事件中,我正在编写以下代码:

 protected void Page_Load(object sender, EventArgs e)
    {
        string getEntity = Request.QueryString["EntityID"];
        int getIntEntity = Int16.Parse(getEntity);

        OISLinq2SqlVs1DataContext dt = new OISLinq2SqlVs1DataContext();
        var tr = from r in dt.Users
                 join s in dt.Entities on r.Entity_ID equals s.ID
                 where s.ID == getIntEntity
                 select new
                 {
                     s.Name,
                     r.FirstName,
                     r.LastName,
                     s.Email,
                     //r.Email,
                     r.UserID,
                     r.Pwd,
                     s.Company,
                     s.Description,
                     s.Phone,
                     s.Fax,
                     s.WebSite

                 };

        GridView1.DataSource = tr;
        GridView1.DataBind();

    }
标记:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
     Height="348px" Width="656px">
</asp:GridView>

现在我可以看到值来自上一页,但是我的GridView没有填充任何内容。我做错了什么?请帮忙!谢谢。


<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
     Height="348px" Width="656px">
</asp:GridView>

您应该将
AutoGenerateColumns
属性设置为
true
,这是数据源定义显示哪些列所必需的


或者,您必须手动指定要显示的列。

您可以发布GridView的标记吗?这样做了。请检查我的答案。当我用itTHanks Alex更新你的问题时,你可以删除这个答案,这很有效。我希望此GridView可编辑。我如何才能做到这一点?@klm9971,MSDN上有一个很好的例子: