使用通用列表绑定网格视图(错误:“未分配字段\属性”)

时间:2011-11-02 17:50:05

标签: asp.net gridview binding

我是.NET新手。我试图将通用List绑定到aspx页面中的GridView。我设置了AutoGenerateColumns="false",我在.aspx页面上定义了列,并对它们进行了绑定,但它仍然抛出了错误A field or property with the name 'Assigned' was not found on the selected data source.

我尝试了所有选项,但最终没有。 CL是我的命名空间的别名。

SITESTATUS CLASS

public class SiteStatus
{
    public string Opened;
    public string Assigned;
    public string LocationAddress;
    public string LocationId;

    public SiteStatus(string Assigned, string Opened, string LocationAddress, string  LocationId)
    {
        this.Assigned = Assigned;
        this.Opened = Opened;
        this.LocationAddress = LocationAddress;
        this.LocationId = LocationId;
    }    
}

Aspx文件

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="SiteSurveyStatus.aspx.cs"  MasterPageFile="~/Site.Master" Inherits="Website.WebForms.SiteSurveyStatus" %>    
<asp:Content ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
    <form id="form1" runat="server">
        <asp:Label ID="SiteSurvey" runat="server" Text="Site Survey Status" Font-Bold="True"></asp:Label>
        <asp:GridView ID="GridView1" runat="server" PageSize="15" CellPadding="0" Width="100%" AutoGenerateColumns="false" EnableViewState="True">
            <Columns>
                <asp:BoundField HeaderText="Assigned" DataField="Assigned" SortExpression="Assigned" />
                <asp:BoundField HeaderText="Opened" DataField="Opened" SortExpression="Opened" />
                <asp:BoundField HeaderText="Location" DataField="LocationAddress" />
                <asp:BoundField HeaderText="LocationId" DataField="LocationId" />
            </Columns>
        </asp:GridView>
    </form>

代码背后:

protected void Page_Load(object sender, EventArgs e)
{
    List<CL.SiteStatus> list = new List<CL.SiteStatus>();

    list.Add(new CL.SiteStatus("09/12/2011", "User123", "Dallas TX 75724", "USATX75724"));
    list.Add(new CL.SiteStatus("10/11/2011", "User234", "Houston TX 77724", "USATX77724"));
    list.Add(new CL.SiteStatus("02/30/2011", "User567", "Austin TX 70748", "USATX70748"));
    list.Add(new CL.SiteStatus("03/01/2011", "User1234", "El Paso TX 71711", "USATX71711"));
    list.Add(new CL.SiteStatus("04/02/2011", "User125", "Chicago IL 33456", "USAIL33456"));

    GridView1.DataSource = list.ToList();
    GridView1.DataBind();
}

1 个答案:

答案 0 :(得分:2)

问题看起来与您的SiteStatus类有关。您有属性但没有修饰符,因此没有外部代码可以访问它们。

尝试:

public class SiteStatus
{
    public string Opened { get; set; }
    public string Assigned { get; set; }
    public string LocationAddress { get; set; }
    public string LocationId { get; set; }
    public SiteStatus(string Assigned, string Opened, string LocationAddress, string LocationId)
    {
        this.Assigned = Assigned;
        this.Opened = Opened;
        this.LocationAddress = LocationAddress;
        this.LocationId = LocationId;
    }
}

你的其他标记对我来说很好。