Gridview没有显示记录?

时间:2012-07-16 10:51:57

标签: c# asp.net .net linq

我刚将linq数据附加到gridview,它没有显示行?

DataDataContext db = new DataDataContext();
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
        BindData();
}

private void BindData()
{ 
    var source = from n in db.Names
                 select n;

    gridSample.DataSource = source.ToList<Name>();
    gridSample.DataBind();'
}

<asp:UpdatePanel ID="panelGrid" runat="server">
    <ContentTemplate>
        <asp:GridView runat="server" ID="gridSample" AutoGenerateColumns="false">
            <Columns>
                <asp:TemplateField HeaderText="First Name">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtFirstName" runat="server" Text='<%Bind("FirstName") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label runat="server" ID="lblFirstName" Text='<%Eval("FirstName") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Last Name">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtLastName" runat="server" Text='<%Bind("LastName") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label runat="server" ID="lblLastName" Text='<%Eval("LastName") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Date of Birth">
                    <EditItemTemplate>
                        <asp:TextBox ID="txtDOB" runat="server" Text='<%Bind("DOB") %>' />
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label runat="server" ID="lblDOB" Text='<%Eval("DOB") %>' />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
   </ContentTemplate>
</asp:UpdatePanel>

4 个答案:

答案 0 :(得分:0)

试试这个

List<Name> source = (from n in db.Names select n).ToList();

        gridSample.DataSource = source;
        gridSample.DataBind();'


<asp:TextBox ID="txtFirstName" runat="server" Text='<%# Bind("FirstName") %>' />

答案 1 :(得分:0)

使用其中之一,

<asp:Label runat="server" ID="lblDOB" Text='<%# Eval("DOB") %>' />

<asp:Label runat="server" ID="lblDOB" Text='<%# Bind("DOB") %>' />

答案 2 :(得分:0)

试试这个:

 private void BindData()
    {

        var source = from n in db.Names
                     select n;

        gridSample.DataSource = source;
        gridSample.DataBind();'
    }

此致

答案 3 :(得分:0)

设置属性

AutoPostBack = True

用于GridView。因为它在UpdatePanel内。