【问题标题】:GridView, It must be either an IListSource, IEnumerable, or IDataSourceGridView,它必须是 IListSource、IEnumerable 或 IDataSource
【发布时间】:2017-02-04 14:48:41
【问题描述】:

这是我的源代码

<asp:GridView ID="gveducationInfo" CssClass="footable" runat="server" AutoGenerateColumns="false" Style="max-width: 600px" ShowHeaderWhenEmpty="true" DataKeyNames="Education_ID" OnRowCommand="gveducationInfo_RowCommand">
    <Columns>
        <asp:BoundField DataField="Education_ID" HeaderText="" ItemStyle-CssClass="hiddenColumn" HeaderStyle-CssClass="hiddenColumn" />
        <asp:BoundField DataField="Degree_Type" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="Year_Of_Passing" HeaderText="Year Of Passing" />
        <asp:BoundField DataField="Institute_Name" HeaderText="Institute Name" />
        <asp:BoundField DataField="State_ID" HeaderText="State" />
        <asp:BoundField DataField="City_ID" HeaderText="City" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <%--<asp:BoundField DataField="Notes" HeaderText="Notes" />--%>
        <asp:ButtonField ButtonType="Image" ImageUrl="~/images/edit16.png" CommandName="EditRow" ItemStyle-CssClass="align-center" />
        <asp:ButtonField ButtonType="Image" ImageUrl="~/images/delete16.png" CommandName="DeleteRow" ItemStyle-CssClass="align-center" />
    </Columns>
</asp:GridView>

这是我的 C# 代码

protected void butSave_ServerClick(object sender, EventArgs e)
{
    Business.ATS.ATS ats = new Business.ATS.ATS();
    Data.Education_Info education_Info = new Data.Education_Info();
    var state = ats.GetStates();
    var city = ats.GetCities();
    education_Info = ats.GetEducationInfo(Applicant_ID).SingleOrDefault();
    education_Info = new Data.Education_Info();
    education_Info.Education_ID = 0;
    education_Info.Applicant_ID = Applicant_ID;
    education_Info.Name = txtdegreename.Value;
    education_Info.Year_Of_Passing = txtyearofpassing.Value;
    education_Info.Institute_Name = txtnameofinstitution.Value;
    education_Info.City_ID = Convert.ToInt32(ddlcity.SelectedValue);
    education_Info.State_ID = Convert.ToInt32(ddlstate.SelectedValue);
    education_Info.Degree_Type = Convert.ToInt32(ddldegreetype.SelectedValue);
    ats.SaveEducationalInfo(education_Info);
    gveducationInfo.DataSource = ats.GetEduInfo().Last();
    gveducationInfo.DataBind();
}

这是我的实体框架数据库代码

public IList<Education_Info> GetEduInfo()
{
    using (Data.ATSEntities dc = new Data.ATSEntities())
    {
        var lsteducationinfo = dc.Education_Info.ToList();
        return lsteducationinfo;
    }
}

当我在网页中填写详细信息并单击保存页面时,我需要获取用户在 gridview 中输入的最后一条记录,但出现错误

数据源是无效类型。它必须是 IListSource, IEnumerable 或 IDataSource。

有人知道怎么搭吗??

【问题讨论】:

    标签: c# asp.net entity-framework gridview


    【解决方案1】:

    Last()的调用是问题所在:

    gveducationInfo.DataSource = ats.GetEduInfo().Last();
    

    像这样删除对Last()的调用:

    gveducationInfo.DataSource = ats.GetEduInfo();
    

    为什么?

    GetEduInfo 方法返回一个IList,一个IList 实现IEnumerable,它将与DataSource 一起使用。但是,当您调用Last() 时,您不再传递IEnumerable,而是传递了序列的最后一项,这是行不通的。

    如果您想将单个项目绑定到您的DataSource,那么您需要将其包装为IEnumerable。您可以使用此Passing a single item as IEnumerable<T> 的任何答案,例如将单个项目包装在数组中:

    gveducationInfo.DataSource = new Education_Info[] {ats.GetEduInfo().Last()};
    

    【讨论】:

    • 我删除了 Last() 但我在网页的网格中获得了完整的表格,但我只需要用户输入的最后一条记录。那可能吗?? @Shaun Luttin
    • @KarthikeyanMuthukumaran 是的。这是可能的。查看修改。
    • 非常感谢@Shaun Luttin
    猜你喜欢
    • 2017-04-25
    • 2011-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多